2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.http
;
20 import static org
.junit
.Assert
.assertArrayEquals
;
21 import static org
.junit
.Assert
.assertEquals
;
22 import static org
.junit
.Assert
.assertFalse
;
23 import static org
.junit
.Assert
.assertTrue
;
25 import javax
.servlet
.http
.HttpServletRequest
;
26 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
27 import org
.apache
.hadoop
.hbase
.testclassification
.MiscTests
;
28 import org
.apache
.hadoop
.hbase
.testclassification
.SmallTests
;
29 import org
.junit
.ClassRule
;
30 import org
.junit
.Test
;
31 import org
.junit
.experimental
.categories
.Category
;
32 import org
.mockito
.Mockito
;
34 @Category({MiscTests
.class, SmallTests
.class})
35 public class TestHtmlQuoting
{
37 public static final HBaseClassTestRule CLASS_RULE
=
38 HBaseClassTestRule
.forClass(TestHtmlQuoting
.class);
40 @Test public void testNeedsQuoting() throws Exception
{
41 assertTrue(HtmlQuoting
.needsQuoting("abcde>"));
42 assertTrue(HtmlQuoting
.needsQuoting("<abcde"));
43 assertTrue(HtmlQuoting
.needsQuoting("abc'de"));
44 assertTrue(HtmlQuoting
.needsQuoting("abcde\""));
45 assertTrue(HtmlQuoting
.needsQuoting("&"));
46 assertFalse(HtmlQuoting
.needsQuoting(""));
47 assertFalse(HtmlQuoting
.needsQuoting("ab\ncdef"));
48 assertFalse(HtmlQuoting
.needsQuoting(null));
51 @Test public void testQuoting() throws Exception
{
52 assertEquals("ab<cd", HtmlQuoting
.quoteHtmlChars("ab<cd"));
53 assertEquals("ab>", HtmlQuoting
.quoteHtmlChars("ab>"));
54 assertEquals("&&&", HtmlQuoting
.quoteHtmlChars("&&&"));
55 assertEquals(" '\n", HtmlQuoting
.quoteHtmlChars(" '\n"));
56 assertEquals(""", HtmlQuoting
.quoteHtmlChars("\""));
57 assertEquals(null, HtmlQuoting
.quoteHtmlChars(null));
60 private void runRoundTrip(String str
) throws Exception
{
62 HtmlQuoting
.unquoteHtmlChars(HtmlQuoting
.quoteHtmlChars(str
)));
65 @Test public void testRoundtrip() throws Exception
{
67 runRoundTrip("<>&'\"");
68 runRoundTrip("ab>cd<ef&ghi'\"");
69 runRoundTrip("A string\n with no quotable chars in it!");
71 StringBuilder buffer
= new StringBuilder();
72 for(char ch
=0; ch
< 127; ++ch
) {
75 runRoundTrip(buffer
.toString());
79 public void testRequestQuoting() throws Exception
{
80 HttpServletRequest mockReq
= Mockito
.mock(HttpServletRequest
.class);
81 HttpServer
.QuotingInputFilter
.RequestQuoter quoter
=
82 new HttpServer
.QuotingInputFilter
.RequestQuoter(mockReq
);
84 Mockito
.doReturn("a<b").when(mockReq
).getParameter("x");
85 assertEquals("Test simple param quoting",
86 "a<b", quoter
.getParameter("x"));
88 Mockito
.doReturn(null).when(mockReq
).getParameter("x");
89 assertEquals("Test that missing parameters dont cause NPE",
90 null, quoter
.getParameter("x"));
92 Mockito
.doReturn(new String
[]{"a<b", "b"}).when(mockReq
).getParameterValues("x");
93 assertArrayEquals("Test escaping of an array",
94 new String
[]{"a<b", "b"}, quoter
.getParameterValues("x"));
96 Mockito
.doReturn(null).when(mockReq
).getParameterValues("x");
97 assertArrayEquals("Test that missing parameters dont cause NPE for array",
98 null, quoter
.getParameterValues("x"));