summaryrefslogtreecommitdiffstats
path: root/net/base/url_util_unittest.cc
diff options
context:
space:
mode:
authortfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-31 17:41:01 +0000
committertfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-31 17:41:01 +0000
commitca93c2aaee10dacd98b96313390126ba3a526b03 (patch)
treeb56777e2fdd2e649f72d553da3502f6e4bdce698 /net/base/url_util_unittest.cc
parent811f3cdb481f280a40ca6c3a4df38c78007fc654 (diff)
downloadchromium_src-ca93c2aaee10dacd98b96313390126ba3a526b03.zip
chromium_src-ca93c2aaee10dacd98b96313390126ba3a526b03.tar.gz
chromium_src-ca93c2aaee10dacd98b96313390126ba3a526b03.tar.bz2
google_apis: Move AppendQueryParameter() etc. from common/net/url_util.h to
net/base/url_util.h Which removes one more dependency on chrome/common/ BUG=146989 TEST=net_unittests --gtest_filter=UrlUtilTest* R=satorux@chromium.org,mmenke@chromium.org TBR=darin@chromium.org Review URL: https://chromiumcodereview.appspot.com/12069004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@179902 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/url_util_unittest.cc')
-rw-r--r--net/base/url_util_unittest.cc104
1 files changed, 104 insertions, 0 deletions
diff --git a/net/base/url_util_unittest.cc b/net/base/url_util_unittest.cc
new file mode 100644
index 0000000..8d1efde
--- /dev/null
+++ b/net/base/url_util_unittest.cc
@@ -0,0 +1,104 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/url_util.h"
+
+#include "googleurl/src/gurl.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace net {
+namespace {
+
+TEST(UrlUtilTest, AppendQueryParameter) {
+ // Appending a name-value pair to a URL without a query component.
+ EXPECT_EQ("http://example.com/path?name=value",
+ AppendQueryParameter(GURL("http://example.com/path"),
+ "name", "value").spec());
+
+ // Appending a name-value pair to a URL with a query component.
+ // The original component should be preserved, and the new pair should be
+ // appended with '&'.
+ EXPECT_EQ("http://example.com/path?existing=one&name=value",
+ AppendQueryParameter(GURL("http://example.com/path?existing=one"),
+ "name", "value").spec());
+
+ // Appending a name-value pair with unsafe characters included. The
+ // unsafe characters should be escaped.
+ EXPECT_EQ("http://example.com/path?existing=one&na+me=v.alue%3D",
+ AppendQueryParameter(GURL("http://example.com/path?existing=one"),
+ "na me", "v.alue=").spec());
+
+}
+
+TEST(UrlUtilTest, AppendOrReplaceQueryParameter) {
+ // Appending a name-value pair to a URL without a query component.
+ EXPECT_EQ("http://example.com/path?name=value",
+ AppendOrReplaceQueryParameter(GURL("http://example.com/path"),
+ "name", "value").spec());
+
+ // Appending a name-value pair to a URL with a query component.
+ // The original component should be preserved, and the new pair should be
+ // appended with '&'.
+ EXPECT_EQ("http://example.com/path?existing=one&name=value",
+ AppendOrReplaceQueryParameter(
+ GURL("http://example.com/path?existing=one"),
+ "name", "value").spec());
+
+ // Appending a name-value pair with unsafe characters included. The
+ // unsafe characters should be escaped.
+ EXPECT_EQ("http://example.com/path?existing=one&na+me=v.alue%3D",
+ AppendOrReplaceQueryParameter(
+ GURL("http://example.com/path?existing=one"),
+ "na me", "v.alue=").spec());
+
+ // Replace value of an existing paramater.
+ EXPECT_EQ("http://example.com/path?existing=one&name=new",
+ AppendOrReplaceQueryParameter(
+ GURL("http://example.com/path?existing=one&name=old"),
+ "name", "new").spec());
+
+ // Replace a name-value pair with unsafe characters included. The
+ // unsafe characters should be escaped.
+ EXPECT_EQ("http://example.com/path?na+me=n.ew%3D&existing=one",
+ AppendOrReplaceQueryParameter(
+ GURL("http://example.com/path?na+me=old&existing=one"),
+ "na me", "n.ew=").spec());
+
+ // Replace the value of first parameter with this name only.
+ EXPECT_EQ("http://example.com/path?name=new&existing=one&name=old",
+ AppendOrReplaceQueryParameter(
+ GURL("http://example.com/path?name=old&existing=one&name=old"),
+ "name", "new").spec());
+
+ // Preserve the content of the original params regarless of our failure to
+ // interpret them correctly.
+ EXPECT_EQ("http://example.com/path?bar&name=new&left=&"
+ "=right&=&&name=again",
+ AppendOrReplaceQueryParameter(
+ GURL("http://example.com/path?bar&name=old&left=&"
+ "=right&=&&name=again"),
+ "name", "new").spec());
+}
+
+TEST(UrlUtilTest, GetValueForKeyInQuery) {
+ GURL url("http://example.com/path?name=value&boolParam&"
+ "url=http://test.com/q?n1%3Dv1%26n2");
+ std::string value;
+
+ // False when getting a non-existent query param.
+ EXPECT_FALSE(GetValueForKeyInQuery(url, "non-exist", &value));
+
+ // True when query param exist.
+ EXPECT_TRUE(GetValueForKeyInQuery(url, "name", &value));
+ EXPECT_EQ("value", value);
+
+ EXPECT_TRUE(GetValueForKeyInQuery(url, "boolParam", &value));
+ EXPECT_EQ("", value);
+
+ EXPECT_TRUE(GetValueForKeyInQuery(url, "url", &value));
+ EXPECT_EQ("http://test.com/q?n1=v1&n2", value);
+}
+
+} // namespace
+} // namespace net