1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import <Foundation/Foundation.h>
7 #include "base/mac/scoped_nsobject.h"
8 #include "base/strings/sys_string_conversions.h"
9 #import "net/base/mac/url_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/gtest_mac.h"
16 class URLConversionTest : public ::testing::Test {
18 void SetUp() override {
19 testData_.reset([[NSArray alloc]
21 // Simple URL with protocol
22 @"https://www.google.com/", @"https://www.google.com/",
24 // Simple URL with protocol and query string
25 @"https://www.google.com/search?q=gtest",
26 @"https://www.google.com/search?q=gtest",
28 // Simple URL with protocol and query string multiple params
29 @"https://www.google.com/search?hl=en&q=gtest",
30 @"https://www.google.com/search?hl=en&q=gtest",
32 // Simple URL with protocol and query string and fragment
33 @"https://www.google.com/search?q=gtest#123",
34 @"https://www.google.com/search?q=gtest#123",
37 @"http://www.mysite.com/~user", @"http://www.mysite.com/~user",
40 @"http://www.mysite.com/#123456", @"http://www.mysite.com/#123456",
42 // URL with # before ?
43 @"http://www.mysite.com/test#test?test",
44 @"http://www.mysite.com/test#test?test",
46 // URL with ? before #
47 @"http://www.mysite.com/test?test#test",
48 @"http://www.mysite.com/test?test#test",
51 @"http://www.mysite.com/test#test#test",
52 @"http://www.mysite.com/test#test%23test",
55 @"http://www.mysite.com/test?test?test",
56 @"http://www.mysite.com/test?test?test",
58 // URL with pattern ? # ?
59 @"http://www.mysite.com/test?test#test?test",
60 @"http://www.mysite.com/test?test#test?test",
62 // URL with pattern # ? #
63 @"http://www.mysite.com/test#test?test#test",
64 @"http://www.mysite.com/test#test?test%23test",
67 @"http://www.mysite.com/%123", @"http://www.mysite.com/%123",
70 @"http://www.mysite.com/[123", @"http://www.mysite.com/%5B123",
73 @"http://www.mysite.com/]123", @"http://www.mysite.com/%5D123",
76 @"http://www.mysite.com/`123", @"http://www.mysite.com/%60123",
79 @"http://www.mysite.com/^123", @"http://www.mysite.com/%5E123",
81 // URL with backslash (GURL canonicallizes unescaped \ to /)
82 @"http://www.mysite.com/\\123", @"http://www.mysite.com//123",
85 @"http://www.mysite.com/~user name",
86 @"http://www.mysite.com/~user%20name",
89 @"http://www.mysite.com/123<456",
90 @"http://www.mysite.com/123%3C456",
93 @"http://www.mysite.com/456>123",
94 @"http://www.mysite.com/456%3E123",
97 @"http://www.mysite.com/|123", @"http://www.mysite.com/%7C123",
100 @"http://www.mysite.com/!123", @"http://www.mysite.com/!123",
103 @"http://www.mysite.com/~user", @"http://www.mysite.com/~user",
106 @"http://www.mysite.com/&123", @"http://www.mysite.com/&123",
109 @"http://www.mysite.com/'user", @"http://www.mysite.com/'user",
112 @"http://www.mysite.com/\"user", @"http://www.mysite.com/%22user",
115 @"http://www.mysite.com/(123", @"http://www.mysite.com/(123",
118 @"http://www.mysite.com/)123", @"http://www.mysite.com/)123",
121 @"http://www.mysite.com/+123", @"http://www.mysite.com/+123",
124 @"http://www.mysite.com/*123", @"http://www.mysite.com/*123",
127 @"http://www.mysite.com/user name",
128 @"http://www.mysite.com/user%20name",
130 // URL with unescaped European accented characters
131 @"http://fr.news.yahoo.com/bactérie-e-coli-ajouter-vinaigre-leau-"
132 "rinçage-légumes-061425535.html",
133 @"http://fr.news.yahoo.com/bact%C3%A9rie-e-coli-ajouter-vinaigre-"
134 "leau-rin%C3%A7age-l%C3%A9gumes-061425535.html",
136 // URL with mix of unescaped European accented characters
137 @"http://fr.news.yahoo.com/bactérie-e-coli-ajouter-vinaigre-leau-"
138 "rinçage-l%C3%A9gumes-061425535.html",
139 @"http://fr.news.yahoo.com/bact%C3%A9rie-e-coli-ajouter-vinaigre-"
140 "leau-rin%C3%A7age-l%C3%A9gumes-061425535.html",
142 // URL with unescaped Asian unicode characters
143 @"http://www.baidu.com/s?cl=3&fr=tb01000&wd=鍜嬩箞鍠傞",
144 @"http://www.baidu.com/s?cl=3&fr=tb01000&wd="
145 "%E9%8D%9C%E5%AC%A9%E7%AE%9E%E9%8D%A0%E5%82%9E",
147 // URL containing every character in the range 20->7F
148 @"http://google.com/ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM"
149 "NOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
150 @"http://google.com/%20!%22#$%25&'()*+,-./0123456789:;%3C=%3E?@ABCD"
151 "EFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz"
154 // URL containing every accented character from the range 80->FF
155 @"http://google.com/¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìí"
156 "îïðñòóôõö÷øùúûüýþÿ",
157 @"http://google.com/%C2%BF%C3%80%C3%81%C3%82%C3%83%C3%84%C3%85%C3"
158 "%86%C3%87%C3%88%C3%89%C3%8A%C3%8B%C3%8C%C3%8D%C3%8E%C3%8F%C3%90"
159 "%C3%91%C3%92%C3%93%C3%94%C3%95%C3%96%C3%97%C3%98%C3%99%C3%9A%C3"
160 "%9B%C3%9C%C3%9D%C3%9E%C3%9F%C3%A0%C3%A1%C3%A2%C3%A3%C3%A4%C3%A5"
161 "%C3%A6%C3%A7%C3%A8%C3%A9%C3%AA%C3%AB%C3%AC%C3%AD%C3%AE%C3%AF%C3"
162 "%B0%C3%B1%C3%B2%C3%B3%C3%B4%C3%B5%C3%B6%C3%B7%C3%B8%C3%B9%C3%BA"
163 "%C3%BB%C3%BC%C3%BD%C3%BE%C3%BF",
165 // URL containing every character in the range 20->7F repeated twice
166 @"http://google.com/ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM"
167 "NOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !\"#$%&'()*+,-"
168 "./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
170 @"http://google.com/%20!%22#$%25&'()*+,-./0123456789:;%3C=%3E?@ABCD"
171 "EFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz"
172 "%7B%7C%7D~%20!%22%23$%25&'()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJ"
173 "KLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C"
176 // Special case 7F. the weird out of place control character for DEL
177 @"data:,\177", @"data:,%7F",
179 // URL with some common control characters.
180 // GURL simply removes most control characters.
181 @"data:,\a\b\t\r\n", @"data:,",
183 // All control characters but \000.
184 @"data:,\001\002\003\004\005\006\007\010\011\012\013\014\015\016"
185 "\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036"
189 // Square brackets shouldn't be escaped in IPv6 literal.
190 @"http://[::1]/path/", @"http://[::1]/path/",
192 // Test all possible features of a URL.
193 @"http://foobar:nicate@example.com:8080/some/path/"
194 "file.html;params-here?foo=bar#baz",
195 @"http://foobar:nicate@example.com:8080/some/path/"
196 "file.html;params-here?foo=bar#baz",
198 // Test a username and password that require escaping.
199 @"http://john!<]:bar[@foo.com/path",
200 @"http://john!%3C%5D:bar%5B@foo.com/path",
205 // NSArray of NSString pairs used for running the tests.
206 // Each pair is in the form <input value, expected result>.
207 base::scoped_nsobject<NSArray> testData_;
210 TEST_F(URLConversionTest, TestNSURLCreationFromStrings) {
211 for (NSUInteger i = 0; i < [testData_ count]; i += 2) {
212 NSString* inputStr = [testData_ objectAtIndex:i];
213 NSString* expected = [testData_ objectAtIndex:(i + 1)];
214 NSURL* url = net::NSURLWithGURL(GURL(base::SysNSStringToUTF8(inputStr)));
215 EXPECT_NSEQ(expected, [url absoluteString]);
219 TEST_F(URLConversionTest, TestURLWithStringDoesNotModifyAlreadyEscapedURLs) {
220 for (NSUInteger i = 0; i < [testData_ count]; i += 2) {
221 NSString* inputStr = [testData_ objectAtIndex:i + 1];
222 NSURL* url = net::NSURLWithGURL(GURL(base::SysNSStringToUTF8(inputStr)));
223 NSString* expected = [testData_ objectAtIndex:i + 1];
224 // Test the expected URL is created.
225 EXPECT_NSEQ(expected, [url absoluteString]);
229 } // anonymous namespace