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 29 import junit.framework.TestCase; 30 31 /** 32 * Tests for the {@link UrlEscapers} class. 33 * 34 * @author David Beaumont 35 */ 36 @GwtCompatible 37 public class UrlEscapersTest extends TestCase { 38 /** 39 * Helper to assert common expected behaviour of uri escapers. You should call 40 * assertBasicUrlEscaper() unless the escaper explicitly does not escape '%'. 41 */ assertBasicUrlEscaperExceptPercent(UnicodeEscaper e)42 static void assertBasicUrlEscaperExceptPercent(UnicodeEscaper e) { 43 // URL escapers should throw null pointer exceptions for null input 44 try { 45 e.escape((String) null); 46 fail("Escaping null string should throw exception"); 47 } catch (NullPointerException x) { 48 // pass 49 } 50 51 // All URL escapers should leave 0-9, A-Z, a-z unescaped 52 assertUnescaped(e, 'a'); 53 assertUnescaped(e, 'z'); 54 assertUnescaped(e, 'A'); 55 assertUnescaped(e, 'Z'); 56 assertUnescaped(e, '0'); 57 assertUnescaped(e, '9'); 58 59 // Unreserved characters used in java.net.URLEncoder 60 assertUnescaped(e, '-'); 61 assertUnescaped(e, '_'); 62 assertUnescaped(e, '.'); 63 assertUnescaped(e, '*'); 64 65 assertEscaping(e, "%00", '\u0000'); // nul 66 assertEscaping(e, "%7F", '\u007f'); // del 67 assertEscaping(e, "%C2%80", '\u0080'); // xx-00010,x-000000 68 assertEscaping(e, "%DF%BF", '\u07ff'); // xx-11111,x-111111 69 assertEscaping(e, "%E0%A0%80", '\u0800'); // xxx-0000,x-100000,x-00,0000 70 assertEscaping(e, "%EF%BF%BF", '\uffff'); // xxx-1111,x-111111,x-11,1111 71 assertUnicodeEscaping(e, "%F0%90%80%80", '\uD800', '\uDC00'); 72 assertUnicodeEscaping(e, "%F4%8F%BF%BF", '\uDBFF', '\uDFFF'); 73 74 assertEquals("", e.escape("")); 75 assertEquals("safestring", e.escape("safestring")); 76 assertEquals("embedded%00null", e.escape("embedded\0null")); 77 assertEquals("max%EF%BF%BFchar", e.escape("max\uffffchar")); 78 } 79 80 // Helper to assert common expected behaviour of uri escapers. assertBasicUrlEscaper(UnicodeEscaper e)81 static void assertBasicUrlEscaper(UnicodeEscaper e) { 82 assertBasicUrlEscaperExceptPercent(e); 83 // The escape character must always be escaped 84 assertEscaping(e, "%25", '%'); 85 } 86 testUrlFormParameterEscaper()87 public void testUrlFormParameterEscaper() { 88 UnicodeEscaper e = (UnicodeEscaper) urlFormParameterEscaper(); 89 // Verify that these are the same escaper (as documented) 90 assertSame(e, urlFormParameterEscaper()); 91 assertBasicUrlEscaper(e); 92 93 /* 94 * Specified as safe by RFC 2396 but not by java.net.URLEncoder. These tests will start failing 95 * when the escaper is made compliant with RFC 2396, but that's a good thing (just change them 96 * to assertUnescaped). 97 */ 98 assertEscaping(e, "%21", '!'); 99 assertEscaping(e, "%28", '('); 100 assertEscaping(e, "%29", ')'); 101 assertEscaping(e, "%7E", '~'); 102 assertEscaping(e, "%27", '\''); 103 104 // Plus for spaces 105 assertEscaping(e, "+", ' '); 106 assertEscaping(e, "%2B", '+'); 107 108 assertEquals("safe+with+spaces", e.escape("safe with spaces")); 109 assertEquals("foo%40bar.com", e.escape("foo@bar.com")); 110 } 111 testUrlPathSegmentEscaper()112 public void testUrlPathSegmentEscaper() { 113 UnicodeEscaper e = (UnicodeEscaper) urlPathSegmentEscaper(); 114 assertPathEscaper(e); 115 assertUnescaped(e, '+'); 116 } 117 assertPathEscaper(UnicodeEscaper e)118 static void assertPathEscaper(UnicodeEscaper e) { 119 assertBasicUrlEscaper(e); 120 121 assertUnescaped(e, '!'); 122 assertUnescaped(e, '\''); 123 assertUnescaped(e, '('); 124 assertUnescaped(e, ')'); 125 assertUnescaped(e, '~'); 126 assertUnescaped(e, ':'); 127 assertUnescaped(e, '@'); 128 129 // Don't use plus for spaces 130 assertEscaping(e, "%20", ' '); 131 132 assertEquals("safe%20with%20spaces", e.escape("safe with spaces")); 133 assertEquals("foo@bar.com", e.escape("foo@bar.com")); 134 } 135 testUrlFragmentEscaper()136 public void testUrlFragmentEscaper() { 137 UnicodeEscaper e = (UnicodeEscaper) urlFragmentEscaper(); 138 assertUnescaped(e, '+'); 139 assertUnescaped(e, '/'); 140 assertUnescaped(e, '?'); 141 142 assertPathEscaper(e); 143 } 144 } 145