• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2014 The Chromium OS 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 <brillo/url_utils.h>
6 
7 #include <gtest/gtest.h>
8 
9 namespace brillo {
10 
TEST(UrlUtils,Combine)11 TEST(UrlUtils, Combine) {
12   EXPECT_EQ("http://sample.org/path",
13             url::Combine("http://sample.org", "path"));
14   EXPECT_EQ("http://sample.org/path",
15             url::Combine("http://sample.org/", "path"));
16   EXPECT_EQ("path1/path2", url::Combine("", "path1/path2"));
17   EXPECT_EQ("path1/path2", url::Combine("path1", "path2"));
18   EXPECT_EQ("http://sample.org", url::Combine("http://sample.org", ""));
19   EXPECT_EQ("http://sample.org/path",
20             url::Combine("http://sample.org/", "/path"));
21   EXPECT_EQ("http://sample.org/path",
22             url::Combine("http://sample.org", "//////path"));
23   EXPECT_EQ("http://sample.org/", url::Combine("http://sample.org", "///"));
24   EXPECT_EQ("http://sample.org/obj/path1/path2",
25             url::Combine("http://sample.org/obj", "path1/path2"));
26   EXPECT_EQ("http://sample.org/obj/path1/path2#tag",
27             url::Combine("http://sample.org/obj#tag", "path1/path2"));
28   EXPECT_EQ("http://sample.org/obj/path1/path2?k1=v1&k2=v2",
29             url::Combine("http://sample.org/obj?k1=v1&k2=v2", "path1/path2"));
30   EXPECT_EQ("http://sample.org/obj/path1/path2?k1=v1#k2=v2",
31             url::Combine("http://sample.org/obj/?k1=v1#k2=v2", "path1/path2"));
32   EXPECT_EQ("http://sample.org/obj/path1/path2#tag?",
33             url::Combine("http://sample.org/obj#tag?", "path1/path2"));
34   EXPECT_EQ("path1/path2", url::CombineMultiple("", {"path1", "path2"}));
35   EXPECT_EQ("http://sample.org/obj/part1/part2",
36             url::CombineMultiple("http://sample.org",
37                                  {"obj", "", "/part1/", "part2"}));
38 }
39 
TEST(UrlUtils,GetQueryString)40 TEST(UrlUtils, GetQueryString) {
41   EXPECT_EQ("", url::GetQueryString("http://sample.org", false));
42   EXPECT_EQ("", url::GetQueryString("http://sample.org", true));
43   EXPECT_EQ("", url::GetQueryString("", false));
44   EXPECT_EQ("", url::GetQueryString("", true));
45 
46   EXPECT_EQ("?q=v&b=2#tag?2",
47             url::GetQueryString("http://s.com/?q=v&b=2#tag?2", false));
48   EXPECT_EQ("?q=v&b=2",
49             url::GetQueryString("http://s.com/?q=v&b=2#tag?2", true));
50 
51   EXPECT_EQ("#tag?a=2", url::GetQueryString("http://s.com/#tag?a=2", false));
52   EXPECT_EQ("", url::GetQueryString("http://s.com/#tag?a=2", true));
53 
54   EXPECT_EQ("?a=2&b=2", url::GetQueryString("?a=2&b=2", false));
55   EXPECT_EQ("?a=2&b=2", url::GetQueryString("?a=2&b=2", true));
56 
57   EXPECT_EQ("#s#?d#?f?#s?#d", url::GetQueryString("#s#?d#?f?#s?#d", false));
58   EXPECT_EQ("", url::GetQueryString("#s#?d#?f?#s?#d", true));
59 }
60 
TEST(UrlUtils,GetQueryStringParameters)61 TEST(UrlUtils, GetQueryStringParameters) {
62   auto params = url::GetQueryStringParameters(
63       "http://sample.org/path?k=v&&%3Dkey%3D=val%26&r#blah");
64 
65   EXPECT_EQ(3, params.size());
66   EXPECT_EQ("k", params[0].first);
67   EXPECT_EQ("v", params[0].second);
68   EXPECT_EQ("=key=", params[1].first);
69   EXPECT_EQ("val&", params[1].second);
70   EXPECT_EQ("r", params[2].first);
71   EXPECT_EQ("", params[2].second);
72 }
73 
TEST(UrlUtils,GetQueryStringValue)74 TEST(UrlUtils, GetQueryStringValue) {
75   std::string url = "http://url?key1=val1&&key2=val2";
76   EXPECT_EQ("val1", url::GetQueryStringValue(url, "key1"));
77   EXPECT_EQ("val2", url::GetQueryStringValue(url, "key2"));
78   EXPECT_EQ("", url::GetQueryStringValue(url, "key3"));
79 
80   auto params = url::GetQueryStringParameters(url);
81   EXPECT_EQ("val1", url::GetQueryStringValue(params, "key1"));
82   EXPECT_EQ("val2", url::GetQueryStringValue(params, "key2"));
83   EXPECT_EQ("", url::GetQueryStringValue(params, "key3"));
84 }
85 
TEST(UrlUtils,TrimOffQueryString)86 TEST(UrlUtils, TrimOffQueryString) {
87   std::string url = "http://url?key1=val1&key2=val2#fragment";
88   std::string query = url::TrimOffQueryString(&url);
89   EXPECT_EQ("http://url", url);
90   EXPECT_EQ("?key1=val1&key2=val2#fragment", query);
91 
92   url = "http://url#fragment";
93   query = url::TrimOffQueryString(&url);
94   EXPECT_EQ("http://url", url);
95   EXPECT_EQ("#fragment", query);
96 
97   url = "http://url";
98   query = url::TrimOffQueryString(&url);
99   EXPECT_EQ("http://url", url);
100   EXPECT_EQ("", query);
101 }
102 
TEST(UrlUtils,RemoveQueryString)103 TEST(UrlUtils, RemoveQueryString) {
104   std::string url = "http://url?key1=val1&key2=val2#fragment";
105   EXPECT_EQ("http://url", url::RemoveQueryString(url, true));
106   EXPECT_EQ("http://url#fragment", url::RemoveQueryString(url, false));
107 }
108 
TEST(UrlUtils,AppendQueryParam)109 TEST(UrlUtils, AppendQueryParam) {
110   std::string url = "http://server.com/path";
111   url = url::AppendQueryParam(url, "param", "value");
112   EXPECT_EQ("http://server.com/path?param=value", url);
113   url = url::AppendQueryParam(url, "param2", "v");
114   EXPECT_EQ("http://server.com/path?param=value&param2=v", url);
115 
116   url = "http://server.com/path#fragment";
117   url = url::AppendQueryParam(url, "param", "value");
118   EXPECT_EQ("http://server.com/path?param=value#fragment", url);
119   url = url::AppendQueryParam(url, "param2", "v");
120   EXPECT_EQ("http://server.com/path?param=value&param2=v#fragment", url);
121 
122   url = url::AppendQueryParam("http://server.com/path?", "param", "value");
123   EXPECT_EQ("http://server.com/path?param=value", url);
124 }
125 
TEST(UrlUtils,AppendQueryParams)126 TEST(UrlUtils, AppendQueryParams) {
127   std::string url = "http://server.com/path";
128   url = url::AppendQueryParams(url, {});
129   EXPECT_EQ("http://server.com/path", url);
130   url = url::AppendQueryParams(url, {{"param", "value"}, {"q", "="}});
131   EXPECT_EQ("http://server.com/path?param=value&q=%3D", url);
132   url += "#fr?";
133   url = url::AppendQueryParams(url, {{"p", "1"}, {"s&", "\n"}});
134   EXPECT_EQ("http://server.com/path?param=value&q=%3D&p=1&s%26=%0A#fr?", url);
135 }
136 
TEST(UrlUtils,HasQueryString)137 TEST(UrlUtils, HasQueryString) {
138   EXPECT_FALSE(url::HasQueryString("http://server.com/path"));
139   EXPECT_FALSE(url::HasQueryString("http://server.com/path#blah?v=1"));
140   EXPECT_TRUE(url::HasQueryString("http://server.com/path?v=1#blah"));
141   EXPECT_TRUE(url::HasQueryString("http://server.com/path?v=1"));
142   EXPECT_FALSE(url::HasQueryString(""));
143   EXPECT_TRUE(url::HasQueryString("?ss"));
144 }
145 
146 }  // namespace brillo
147