Make touch-resizing windows to screen edge possible
[chromium-blink-merge.git] / net / base / url_util_unittest.cc
blob8d1efde43a06e6c022bdd4748aa5ed7e7e78a395
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
3 // found in the LICENSE file.
5 #include "net/base/url_util.h"
7 #include "googleurl/src/gurl.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace net {
11 namespace {
13 TEST(UrlUtilTest, AppendQueryParameter) {
14 // Appending a name-value pair to a URL without a query component.
15 EXPECT_EQ("http://example.com/path?name=value",
16 AppendQueryParameter(GURL("http://example.com/path"),
17 "name", "value").spec());
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
21 // appended with '&'.
22 EXPECT_EQ("http://example.com/path?existing=one&name=value",
23 AppendQueryParameter(GURL("http://example.com/path?existing=one"),
24 "name", "value").spec());
26 // Appending a name-value pair with unsafe characters included. The
27 // unsafe characters should be escaped.
28 EXPECT_EQ("http://example.com/path?existing=one&na+me=v.alue%3D",
29 AppendQueryParameter(GURL("http://example.com/path?existing=one"),
30 "na me", "v.alue=").spec());
34 TEST(UrlUtilTest, AppendOrReplaceQueryParameter) {
35 // Appending a name-value pair to a URL without a query component.
36 EXPECT_EQ("http://example.com/path?name=value",
37 AppendOrReplaceQueryParameter(GURL("http://example.com/path"),
38 "name", "value").spec());
40 // 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 // appended with '&'.
43 EXPECT_EQ("http://example.com/path?existing=one&name=value",
44 AppendOrReplaceQueryParameter(
45 GURL("http://example.com/path?existing=one"),
46 "name", "value").spec());
48 // Appending a name-value pair with unsafe characters included. The
49 // unsafe characters should be escaped.
50 EXPECT_EQ("http://example.com/path?existing=one&na+me=v.alue%3D",
51 AppendOrReplaceQueryParameter(
52 GURL("http://example.com/path?existing=one"),
53 "na me", "v.alue=").spec());
55 // Replace value of an existing paramater.
56 EXPECT_EQ("http://example.com/path?existing=one&name=new",
57 AppendOrReplaceQueryParameter(
58 GURL("http://example.com/path?existing=one&name=old"),
59 "name", "new").spec());
61 // Replace a name-value pair with unsafe characters included. The
62 // unsafe characters should be escaped.
63 EXPECT_EQ("http://example.com/path?na+me=n.ew%3D&existing=one",
64 AppendOrReplaceQueryParameter(
65 GURL("http://example.com/path?na+me=old&existing=one"),
66 "na me", "n.ew=").spec());
68 // Replace the value of first parameter with this name only.
69 EXPECT_EQ("http://example.com/path?name=new&existing=one&name=old",
70 AppendOrReplaceQueryParameter(
71 GURL("http://example.com/path?name=old&existing=one&name=old"),
72 "name", "new").spec());
74 // Preserve the content of the original params regarless of our failure to
75 // interpret them correctly.
76 EXPECT_EQ("http://example.com/path?bar&name=new&left=&"
77 "=right&=&&name=again",
78 AppendOrReplaceQueryParameter(
79 GURL("http://example.com/path?bar&name=old&left=&"
80 "=right&=&&name=again"),
81 "name", "new").spec());
84 TEST(UrlUtilTest, GetValueForKeyInQuery) {
85 GURL url("http://example.com/path?name=value&boolParam&"
86 "url=http://test.com/q?n1%3Dv1%26n2");
87 std::string value;
89 // False when getting a non-existent query param.
90 EXPECT_FALSE(GetValueForKeyInQuery(url, "non-exist", &value));
92 // True when query param exist.
93 EXPECT_TRUE(GetValueForKeyInQuery(url, "name", &value));
94 EXPECT_EQ("value", value);
96 EXPECT_TRUE(GetValueForKeyInQuery(url, "boolParam", &value));
97 EXPECT_EQ("", value);
99 EXPECT_TRUE(GetValueForKeyInQuery(url, "url", &value));
100 EXPECT_EQ("http://test.com/q?n1=v1&n2", value);
103 } // namespace
104 } // namespace net