OLD | NEW |
| (Empty) |
1 // Copyright (c) 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. | |
4 | |
5 #include "chrome/common/net/url_util.h" | |
6 | |
7 #include "googleurl/src/gurl.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 namespace chrome_common_net { | |
11 | |
12 TEST(UrlUtilTest, AppendQueryParameter) { | |
13 // Appending a name-value pair to a URL without a query component. | |
14 EXPECT_EQ("http://example.com/path?name=value", | |
15 AppendQueryParameter(GURL("http://example.com/path"), | |
16 "name", "value").spec()); | |
17 | |
18 // Appending a name-value pair to a URL with a query component. | |
19 // The original component should be preserved, and the new pair should be | |
20 // appended with '&'. | |
21 EXPECT_EQ("http://example.com/path?existing=one&name=value", | |
22 AppendQueryParameter(GURL("http://example.com/path?existing=one"), | |
23 "name", "value").spec()); | |
24 | |
25 // Appending a name-value pair with unsafe characters included. The | |
26 // unsafe characters should be escaped. | |
27 EXPECT_EQ("http://example.com/path?existing=one&na+me=v.alue%3D", | |
28 AppendQueryParameter(GURL("http://example.com/path?existing=one"), | |
29 "na me", "v.alue=").spec()); | |
30 | |
31 } | |
32 | |
33 TEST(UrlUtilTest, AppendOrReplaceQueryParameter) { | |
34 // Appending a name-value pair to a URL without a query component. | |
35 EXPECT_EQ("http://example.com/path?name=value", | |
36 AppendOrReplaceQueryParameter(GURL("http://example.com/path"), | |
37 "name", "value").spec()); | |
38 | |
39 // Appending a name-value pair to a URL with a query component. | |
40 // The original component should be preserved, and the new pair should be | |
41 // appended with '&'. | |
42 EXPECT_EQ("http://example.com/path?existing=one&name=value", | |
43 AppendOrReplaceQueryParameter( | |
44 GURL("http://example.com/path?existing=one"), | |
45 "name", "value").spec()); | |
46 | |
47 // Appending a name-value pair with unsafe characters included. The | |
48 // unsafe characters should be escaped. | |
49 EXPECT_EQ("http://example.com/path?existing=one&na+me=v.alue%3D", | |
50 AppendOrReplaceQueryParameter( | |
51 GURL("http://example.com/path?existing=one"), | |
52 "na me", "v.alue=").spec()); | |
53 | |
54 // Replace value of an existing paramater. | |
55 EXPECT_EQ("http://example.com/path?existing=one&name=new", | |
56 AppendOrReplaceQueryParameter( | |
57 GURL("http://example.com/path?existing=one&name=old"), | |
58 "name", "new").spec()); | |
59 | |
60 // Replace a name-value pair with unsafe characters included. The | |
61 // unsafe characters should be escaped. | |
62 EXPECT_EQ("http://example.com/path?na+me=n.ew%3D&existing=one", | |
63 AppendOrReplaceQueryParameter( | |
64 GURL("http://example.com/path?na+me=old&existing=one"), | |
65 "na me", "n.ew=").spec()); | |
66 | |
67 // Replace the value of first parameter with this name only. | |
68 EXPECT_EQ("http://example.com/path?name=new&existing=one&name=old", | |
69 AppendOrReplaceQueryParameter( | |
70 GURL("http://example.com/path?name=old&existing=one&name=old"), | |
71 "name", "new").spec()); | |
72 | |
73 // Preserve the content of the original params regarless of our failure to | |
74 // interpret them correctly. | |
75 EXPECT_EQ("http://example.com/path?bar&name=new&left=&" | |
76 "=right&=&&name=again", | |
77 AppendOrReplaceQueryParameter( | |
78 GURL("http://example.com/path?bar&name=old&left=&" | |
79 "=right&=&&name=again"), | |
80 "name", "new").spec()); | |
81 } | |
82 | |
83 TEST(BrowserUrlUtilTest, GetValueForKeyInQuery) { | |
84 GURL url("http://example.com/path?name=value&boolParam&" | |
85 "url=http://test.com/q?n1%3Dv1%26n2"); | |
86 std::string value; | |
87 | |
88 // False when getting a non-existent query param. | |
89 EXPECT_FALSE(GetValueForKeyInQuery(url, "non-exist", &value)); | |
90 | |
91 // True when query param exist. | |
92 EXPECT_TRUE(GetValueForKeyInQuery(url, "name", &value)); | |
93 EXPECT_EQ("value", value); | |
94 | |
95 EXPECT_TRUE(GetValueForKeyInQuery(url, "boolParam", &value)); | |
96 EXPECT_EQ("", value); | |
97 | |
98 EXPECT_TRUE(GetValueForKeyInQuery(url, "url", &value)); | |
99 EXPECT_EQ("http://test.com/q?n1=v1&n2", value); | |
100 } | |
101 | |
102 } // namespace chrome_common_net. | |
OLD | NEW |