1 /* 2 * Copyright (C) 2009 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.net; 18 19 import static com.google.common.escape.testing.EscaperAsserts.assertEscaping; 20 import static com.google.common.escape.testing.EscaperAsserts.assertUnescaped; 21 import static com.google.common.escape.testing.EscaperAsserts.assertUnicodeEscaping; 22 import static com.google.common.net.UrlEscapers.urlFormParameterEscaper; 23 import static com.google.common.net.UrlEscapers.urlFragmentEscaper; 24 import static com.google.common.net.UrlEscapers.urlPathSegmentEscaper; 25 26 import com.google.common.annotations.GwtCompatible; 27 import com.google.common.escape.UnicodeEscaper; 28 import junit.framework.TestCase; 29 30 /** 31 * Tests for the {@link UrlEscapers} class. 32 * 33 * @author David Beaumont 34 */ 35 @GwtCompatible 36 public class UrlEscapersTest extends TestCase { 37 /** 38 * Helper to assert common expected behaviour of uri escapers. You should call 39 * assertBasicUrlEscaper() unless the escaper explicitly does not escape '%'. 40 */ assertBasicUrlEscaperExceptPercent(UnicodeEscaper e)41 static void assertBasicUrlEscaperExceptPercent(UnicodeEscaper e) { 42 // URL escapers should throw null pointer exceptions for null input 43 try { 44 e.escape((String) null); 45 fail("Escaping null string should throw exception"); 46 } catch (NullPointerException x) { 47 // pass 48 } 49 50 // All URL escapers should leave 0-9, A-Z, a-z unescaped 51 assertUnescaped(e, 'a'); 52 assertUnescaped(e, 'z'); 53 assertUnescaped(e, 'A'); 54 assertUnescaped(e, 'Z'); 55 assertUnescaped(e, '0'); 56 assertUnescaped(e, '9'); 57 58 // Unreserved characters used in java.net.URLEncoder 59 assertUnescaped(e, '-'); 60 assertUnescaped(e, '_'); 61 assertUnescaped(e, '.'); 62 assertUnescaped(e, '*'); 63 64 assertEscaping(e, "%00", '\u0000'); // nul 65 assertEscaping(e, "%7F", '\u007f'); // del 66 assertEscaping(e, "%C2%80", '\u0080'); // xx-00010,x-000000 67 assertEscaping(e, "%DF%BF", '\u07ff'); // xx-11111,x-111111 68 assertEscaping(e, "%E0%A0%80", '\u0800'); // xxx-0000,x-100000,x-00,0000 69 assertEscaping(e, "%EF%BF%BF", '\uffff'); // xxx-1111,x-111111,x-11,1111 70 assertUnicodeEscaping(e, "%F0%90%80%80", '\uD800', '\uDC00'); 71 assertUnicodeEscaping(e, "%F4%8F%BF%BF", '\uDBFF', '\uDFFF'); 72 73 assertEquals("", e.escape("")); 74 assertEquals("safestring", e.escape("safestring")); 75 assertEquals("embedded%00null", e.escape("embedded\0null")); 76 assertEquals("max%EF%BF%BFchar", e.escape("max\uffffchar")); 77 } 78 79 // Helper to assert common expected behaviour of uri escapers. assertBasicUrlEscaper(UnicodeEscaper e)80 static void assertBasicUrlEscaper(UnicodeEscaper e) { 81 assertBasicUrlEscaperExceptPercent(e); 82 // The escape character must always be escaped 83 assertEscaping(e, "%25", '%'); 84 } 85 testUrlFormParameterEscaper()86 public void testUrlFormParameterEscaper() { 87 UnicodeEscaper e = (UnicodeEscaper) urlFormParameterEscaper(); 88 // Verify that these are the same escaper (as documented) 89 assertSame(e, urlFormParameterEscaper()); 90 assertBasicUrlEscaper(e); 91 92 /* 93 * Specified as safe by RFC 2396 but not by java.net.URLEncoder. These tests will start failing 94 * when the escaper is made compliant with RFC 2396, but that's a good thing (just change them 95 * to assertUnescaped). 96 */ 97 assertEscaping(e, "%21", '!'); 98 assertEscaping(e, "%28", '('); 99 assertEscaping(e, "%29", ')'); 100 assertEscaping(e, "%7E", '~'); 101 assertEscaping(e, "%27", '\''); 102 103 // Plus for spaces 104 assertEscaping(e, "+", ' '); 105 assertEscaping(e, "%2B", '+'); 106 107 assertEquals("safe+with+spaces", e.escape("safe with spaces")); 108 assertEquals("foo%40bar.com", e.escape("foo@bar.com")); 109 } 110 testUrlPathSegmentEscaper()111 public void testUrlPathSegmentEscaper() { 112 UnicodeEscaper e = (UnicodeEscaper) urlPathSegmentEscaper(); 113 assertPathEscaper(e); 114 assertUnescaped(e, '+'); 115 } 116 assertPathEscaper(UnicodeEscaper e)117 static void assertPathEscaper(UnicodeEscaper e) { 118 assertBasicUrlEscaper(e); 119 120 assertUnescaped(e, '!'); 121 assertUnescaped(e, '\''); 122 assertUnescaped(e, '('); 123 assertUnescaped(e, ')'); 124 assertUnescaped(e, '~'); 125 assertUnescaped(e, ':'); 126 assertUnescaped(e, '@'); 127 128 // Don't use plus for spaces 129 assertEscaping(e, "%20", ' '); 130 131 assertEquals("safe%20with%20spaces", e.escape("safe with spaces")); 132 assertEquals("foo@bar.com", e.escape("foo@bar.com")); 133 } 134 testUrlFragmentEscaper()135 public void testUrlFragmentEscaper() { 136 UnicodeEscaper e = (UnicodeEscaper) urlFragmentEscaper(); 137 assertUnescaped(e, '+'); 138 assertUnescaped(e, '/'); 139 assertUnescaped(e, '?'); 140 141 assertPathEscaper(e); 142 } 143 } 144