OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/url_util.h" | 5 #include "net/base/url_util.h" |
6 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "url/gurl.h" | 8 #include "url/gurl.h" |
9 | 9 |
10 namespace net { | 10 namespace net { |
11 namespace { | 11 namespace { |
12 | 12 |
13 TEST(UrlUtilTest, AppendQueryParameter) { | 13 TEST(UrlUtilTest, AppendQueryParameter) { |
14 // Appending a name-value pair to a URL without a query component. | 14 // Appending a name-value pair to a URL without a query component. |
15 EXPECT_EQ("http://example.com/path?name=value", | 15 EXPECT_EQ("http://example.com/path?name=value", |
16 AppendQueryParameter(GURL("http://example.com/path"), | 16 AppendQueryParameter( |
17 "name", "value").spec()); | 17 GURL("http://example.com/path"), "name", "value").spec()); |
18 | 18 |
19 // Appending a name-value pair to a URL with a query component. | 19 // Appending a name-value pair to a URL with a query component. |
20 // The original component should be preserved, and the new pair should be | 20 // The original component should be preserved, and the new pair should be |
21 // appended with '&'. | 21 // appended with '&'. |
22 EXPECT_EQ("http://example.com/path?existing=one&name=value", | 22 EXPECT_EQ("http://example.com/path?existing=one&name=value", |
23 AppendQueryParameter(GURL("http://example.com/path?existing=one"), | 23 AppendQueryParameter(GURL("http://example.com/path?existing=one"), |
24 "name", "value").spec()); | 24 "name", |
| 25 "value").spec()); |
25 | 26 |
26 // Appending a name-value pair with unsafe characters included. The | 27 // Appending a name-value pair with unsafe characters included. The |
27 // unsafe characters should be escaped. | 28 // unsafe characters should be escaped. |
28 EXPECT_EQ("http://example.com/path?existing=one&na+me=v.alue%3D", | 29 EXPECT_EQ("http://example.com/path?existing=one&na+me=v.alue%3D", |
29 AppendQueryParameter(GURL("http://example.com/path?existing=one"), | 30 AppendQueryParameter(GURL("http://example.com/path?existing=one"), |
30 "na me", "v.alue=").spec()); | 31 "na me", |
31 | 32 "v.alue=").spec()); |
32 } | 33 } |
33 | 34 |
34 TEST(UrlUtilTest, AppendOrReplaceQueryParameter) { | 35 TEST(UrlUtilTest, AppendOrReplaceQueryParameter) { |
35 // Appending a name-value pair to a URL without a query component. | 36 // Appending a name-value pair to a URL without a query component. |
36 EXPECT_EQ("http://example.com/path?name=value", | 37 EXPECT_EQ("http://example.com/path?name=value", |
37 AppendOrReplaceQueryParameter(GURL("http://example.com/path"), | 38 AppendOrReplaceQueryParameter( |
38 "name", "value").spec()); | 39 GURL("http://example.com/path"), "name", "value").spec()); |
39 | 40 |
40 // Appending a name-value pair to a URL with a query component. | 41 // Appending a name-value pair to a URL with a query component. |
41 // The original component should be preserved, and the new pair should be | 42 // The original component should be preserved, and the new pair should be |
42 // appended with '&'. | 43 // appended with '&'. |
43 EXPECT_EQ("http://example.com/path?existing=one&name=value", | 44 EXPECT_EQ("http://example.com/path?existing=one&name=value", |
44 AppendOrReplaceQueryParameter( | 45 AppendOrReplaceQueryParameter( |
45 GURL("http://example.com/path?existing=one"), | 46 GURL("http://example.com/path?existing=one"), "name", "value") |
46 "name", "value").spec()); | 47 .spec()); |
47 | 48 |
48 // Appending a name-value pair with unsafe characters included. The | 49 // Appending a name-value pair with unsafe characters included. The |
49 // unsafe characters should be escaped. | 50 // unsafe characters should be escaped. |
50 EXPECT_EQ("http://example.com/path?existing=one&na+me=v.alue%3D", | 51 EXPECT_EQ( |
| 52 "http://example.com/path?existing=one&na+me=v.alue%3D", |
51 AppendOrReplaceQueryParameter( | 53 AppendOrReplaceQueryParameter( |
52 GURL("http://example.com/path?existing=one"), | 54 GURL("http://example.com/path?existing=one"), "na me", "v.alue=") |
53 "na me", "v.alue=").spec()); | 55 .spec()); |
54 | 56 |
55 // Replace value of an existing paramater. | 57 // Replace value of an existing paramater. |
56 EXPECT_EQ("http://example.com/path?existing=one&name=new", | 58 EXPECT_EQ( |
| 59 "http://example.com/path?existing=one&name=new", |
57 AppendOrReplaceQueryParameter( | 60 AppendOrReplaceQueryParameter( |
58 GURL("http://example.com/path?existing=one&name=old"), | 61 GURL("http://example.com/path?existing=one&name=old"), "name", "new") |
59 "name", "new").spec()); | 62 .spec()); |
60 | 63 |
61 // Replace a name-value pair with unsafe characters included. The | 64 // Replace a name-value pair with unsafe characters included. The |
62 // unsafe characters should be escaped. | 65 // unsafe characters should be escaped. |
63 EXPECT_EQ("http://example.com/path?na+me=n.ew%3D&existing=one", | 66 EXPECT_EQ("http://example.com/path?na+me=n.ew%3D&existing=one", |
64 AppendOrReplaceQueryParameter( | 67 AppendOrReplaceQueryParameter( |
65 GURL("http://example.com/path?na+me=old&existing=one"), | 68 GURL("http://example.com/path?na+me=old&existing=one"), |
66 "na me", "n.ew=").spec()); | 69 "na me", |
| 70 "n.ew=").spec()); |
67 | 71 |
68 // Replace the value of first parameter with this name only. | 72 // Replace the value of first parameter with this name only. |
69 EXPECT_EQ("http://example.com/path?name=new&existing=one&name=old", | 73 EXPECT_EQ("http://example.com/path?name=new&existing=one&name=old", |
70 AppendOrReplaceQueryParameter( | 74 AppendOrReplaceQueryParameter( |
71 GURL("http://example.com/path?name=old&existing=one&name=old"), | 75 GURL("http://example.com/path?name=old&existing=one&name=old"), |
72 "name", "new").spec()); | 76 "name", |
| 77 "new").spec()); |
73 | 78 |
74 // Preserve the content of the original params regarless of our failure to | 79 // Preserve the content of the original params regarless of our failure to |
75 // interpret them correctly. | 80 // interpret them correctly. |
76 EXPECT_EQ("http://example.com/path?bar&name=new&left=&" | 81 EXPECT_EQ( |
77 "=right&=&&name=again", | 82 "http://example.com/path?bar&name=new&left=&" |
| 83 "=right&=&&name=again", |
78 AppendOrReplaceQueryParameter( | 84 AppendOrReplaceQueryParameter( |
79 GURL("http://example.com/path?bar&name=old&left=&" | 85 GURL( |
80 "=right&=&&name=again"), | 86 "http://example.com/path?bar&name=old&left=&" |
81 "name", "new").spec()); | 87 "=right&=&&name=again"), |
| 88 "name", |
| 89 "new").spec()); |
82 } | 90 } |
83 | 91 |
84 TEST(UrlUtilTest, GetValueForKeyInQuery) { | 92 TEST(UrlUtilTest, GetValueForKeyInQuery) { |
85 GURL url("http://example.com/path?name=value&boolParam&" | 93 GURL url( |
86 "url=http://test.com/q?n1%3Dv1%26n2"); | 94 "http://example.com/path?name=value&boolParam&" |
| 95 "url=http://test.com/q?n1%3Dv1%26n2"); |
87 std::string value; | 96 std::string value; |
88 | 97 |
89 // False when getting a non-existent query param. | 98 // False when getting a non-existent query param. |
90 EXPECT_FALSE(GetValueForKeyInQuery(url, "non-exist", &value)); | 99 EXPECT_FALSE(GetValueForKeyInQuery(url, "non-exist", &value)); |
91 | 100 |
92 // True when query param exist. | 101 // True when query param exist. |
93 EXPECT_TRUE(GetValueForKeyInQuery(url, "name", &value)); | 102 EXPECT_TRUE(GetValueForKeyInQuery(url, "name", &value)); |
94 EXPECT_EQ("value", value); | 103 EXPECT_EQ("value", value); |
95 | 104 |
96 EXPECT_TRUE(GetValueForKeyInQuery(url, "boolParam", &value)); | 105 EXPECT_TRUE(GetValueForKeyInQuery(url, "boolParam", &value)); |
97 EXPECT_EQ("", value); | 106 EXPECT_EQ("", value); |
98 | 107 |
99 EXPECT_TRUE(GetValueForKeyInQuery(url, "url", &value)); | 108 EXPECT_TRUE(GetValueForKeyInQuery(url, "url", &value)); |
100 EXPECT_EQ("http://test.com/q?n1=v1&n2", value); | 109 EXPECT_EQ("http://test.com/q?n1=v1&n2", value); |
101 } | 110 } |
102 | 111 |
103 TEST(UrlUtilTest, GetValueForKeyInQueryInvalidURL) { | 112 TEST(UrlUtilTest, GetValueForKeyInQueryInvalidURL) { |
104 GURL url("http://%01/?test"); | 113 GURL url("http://%01/?test"); |
105 std::string value; | 114 std::string value; |
106 | 115 |
107 // Always false when parsing an invalid URL. | 116 // Always false when parsing an invalid URL. |
108 EXPECT_FALSE(GetValueForKeyInQuery(url, "test", &value)); | 117 EXPECT_FALSE(GetValueForKeyInQuery(url, "test", &value)); |
109 } | 118 } |
110 | 119 |
111 TEST(UrlUtilTest, ParseQuery) { | 120 TEST(UrlUtilTest, ParseQuery) { |
112 const GURL url("http://example.com/path?name=value&boolParam&" | 121 const GURL url( |
113 "url=http://test.com/q?n1%3Dv1%26n2&" | 122 "http://example.com/path?name=value&boolParam&" |
114 "multikey=value1&multikey=value2&multikey"); | 123 "url=http://test.com/q?n1%3Dv1%26n2&" |
| 124 "multikey=value1&multikey=value2&multikey"); |
115 QueryIterator it(url); | 125 QueryIterator it(url); |
116 | 126 |
117 ASSERT_FALSE(it.IsAtEnd()); | 127 ASSERT_FALSE(it.IsAtEnd()); |
118 EXPECT_EQ("name", it.GetKey()); | 128 EXPECT_EQ("name", it.GetKey()); |
119 EXPECT_EQ("value", it.GetValue()); | 129 EXPECT_EQ("value", it.GetValue()); |
120 EXPECT_EQ("value", it.GetUnescapedValue()); | 130 EXPECT_EQ("value", it.GetUnescapedValue()); |
121 it.Advance(); | 131 it.Advance(); |
122 | 132 |
123 ASSERT_FALSE(it.IsAtEnd()); | 133 ASSERT_FALSE(it.IsAtEnd()); |
124 EXPECT_EQ("boolParam", it.GetKey()); | 134 EXPECT_EQ("boolParam", it.GetKey()); |
(...skipping 29 matching lines...) Expand all Loading... |
154 } | 164 } |
155 | 165 |
156 TEST(UrlUtilTest, ParseQueryInvalidURL) { | 166 TEST(UrlUtilTest, ParseQueryInvalidURL) { |
157 const GURL url("http://%01/?test"); | 167 const GURL url("http://%01/?test"); |
158 QueryIterator it(url); | 168 QueryIterator it(url); |
159 EXPECT_TRUE(it.IsAtEnd()); | 169 EXPECT_TRUE(it.IsAtEnd()); |
160 } | 170 } |
161 | 171 |
162 } // namespace | 172 } // namespace |
163 } // namespace net | 173 } // namespace net |
OLD | NEW |