1 // Copyright 2020 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 "util/url.h"
6
7 #include "gtest/gtest.h"
8
9 namespace openscreen {
10
TEST(UrlTest,Validity)11 TEST(UrlTest, Validity) {
12 const char* valid_cases[] = {
13 "http://google.com",
14 "unknown://google.com",
15 "http://user:pass@google.com",
16 "http://google.com:12345",
17 "http://google.com/path",
18 "http://google.com//path",
19 "http://google.com?k=v#fragment",
20 "http://user:pass@google.com:12345/path?k=v#fragment",
21 "http:/path",
22 "http:path",
23 };
24 constexpr int valid_case_count = sizeof(valid_cases) / sizeof(valid_cases[0]);
25 for (size_t i = 0; i < valid_case_count; i++) {
26 EXPECT_TRUE(Url(valid_cases[i]).is_valid()) << "Case: " << valid_cases[i];
27 }
28
29 const char* invalid_cases[] = {
30 "http://?k=v", "http:://google.com",
31 "http//google.com", "http://google.com:12three45",
32 "://google.com", "path",
33 };
34 constexpr int invalid_case_count =
35 sizeof(invalid_cases) / sizeof(invalid_cases[0]);
36 for (size_t i = 0; i < invalid_case_count; i++) {
37 EXPECT_FALSE(Url(invalid_cases[i]).is_valid())
38 << "Case: " << invalid_cases[i];
39 }
40 }
41
TEST(UrlTest,Components)42 TEST(UrlTest, Components) {
43 Url url("http://user:pass@google.com:99/foo;bar?q=a#ref");
44
45 EXPECT_TRUE(url.is_valid());
46 EXPECT_TRUE(url.has_host());
47 EXPECT_TRUE(url.has_port());
48 EXPECT_TRUE(url.has_path());
49 EXPECT_TRUE(url.has_query());
50
51 EXPECT_EQ("http", url.scheme());
52 EXPECT_EQ("google.com", url.host());
53 EXPECT_EQ(99, url.port());
54 EXPECT_EQ("/foo;bar", url.path());
55 EXPECT_EQ("q=a", url.query());
56 }
57
TEST(UrlTest,Copy)58 TEST(UrlTest, Copy) {
59 Url url1("http://user:pass@google.com:99/foo;bar?q=a#ref");
60 Url url2(url1);
61
62 EXPECT_TRUE(url1.is_valid());
63 EXPECT_TRUE(url1.has_host());
64 EXPECT_TRUE(url1.has_port());
65 EXPECT_TRUE(url1.has_path());
66 EXPECT_TRUE(url1.has_query());
67
68 EXPECT_EQ("http", url1.scheme());
69 EXPECT_EQ("google.com", url1.host());
70 EXPECT_EQ(99, url1.port());
71 EXPECT_EQ("/foo;bar", url1.path());
72 EXPECT_EQ("q=a", url1.query());
73
74 EXPECT_TRUE(url2.is_valid());
75 EXPECT_TRUE(url2.has_host());
76 EXPECT_TRUE(url2.has_port());
77 EXPECT_TRUE(url2.has_path());
78 EXPECT_TRUE(url2.has_query());
79
80 EXPECT_EQ("http", url2.scheme());
81 EXPECT_EQ("google.com", url2.host());
82 EXPECT_EQ(99, url2.port());
83 EXPECT_EQ("/foo;bar", url2.path());
84 EXPECT_EQ("q=a", url2.query());
85 }
86
TEST(UrlTest,Move)87 TEST(UrlTest, Move) {
88 Url url1("http://user:pass@google.com:99/foo;bar?q=a#ref");
89 Url url2(std::move(url1));
90
91 EXPECT_FALSE(url1.is_valid());
92
93 EXPECT_TRUE(url2.is_valid());
94 EXPECT_TRUE(url2.has_host());
95 EXPECT_TRUE(url2.has_port());
96 EXPECT_TRUE(url2.has_path());
97 EXPECT_TRUE(url2.has_query());
98
99 EXPECT_EQ("http", url2.scheme());
100 EXPECT_EQ("google.com", url2.host());
101 EXPECT_EQ(99, url2.port());
102 EXPECT_EQ("/foo;bar", url2.path());
103 EXPECT_EQ("q=a", url2.query());
104 }
105
TEST(UrlTest,Assign)106 TEST(UrlTest, Assign) {
107 Url url1("http://user:pass@google.com:99/foo;bar?q=a#ref");
108 Url url2("https://example.com");
109 Url url3("https://example.com");
110
111 url2 = url1;
112
113 EXPECT_TRUE(url1.is_valid());
114 EXPECT_TRUE(url1.has_host());
115 EXPECT_TRUE(url1.has_port());
116 EXPECT_TRUE(url1.has_path());
117 EXPECT_TRUE(url1.has_query());
118
119 EXPECT_EQ("http", url1.scheme());
120 EXPECT_EQ("google.com", url1.host());
121 EXPECT_EQ(99, url1.port());
122 EXPECT_EQ("/foo;bar", url1.path());
123 EXPECT_EQ("q=a", url1.query());
124
125 EXPECT_TRUE(url2.is_valid());
126 EXPECT_TRUE(url2.has_host());
127 EXPECT_TRUE(url2.has_port());
128 EXPECT_TRUE(url2.has_path());
129 EXPECT_TRUE(url2.has_query());
130
131 EXPECT_EQ("http", url2.scheme());
132 EXPECT_EQ("google.com", url2.host());
133 EXPECT_EQ(99, url2.port());
134 EXPECT_EQ("/foo;bar", url2.path());
135 EXPECT_EQ("q=a", url2.query());
136
137 url3 = std::move(url1);
138
139 EXPECT_FALSE(url1.is_valid());
140
141 EXPECT_TRUE(url3.is_valid());
142 EXPECT_TRUE(url3.has_host());
143 EXPECT_TRUE(url3.has_port());
144 EXPECT_TRUE(url3.has_path());
145 EXPECT_TRUE(url3.has_query());
146
147 EXPECT_EQ("http", url3.scheme());
148 EXPECT_EQ("google.com", url3.host());
149 EXPECT_EQ(99, url3.port());
150 EXPECT_EQ("/foo;bar", url3.path());
151 EXPECT_EQ("q=a", url3.query());
152 }
153
TEST(UrlTest,Cast)154 TEST(UrlTest, Cast) {
155 std::string cast(
156 "cast:233637DE?capabilities=video_out%2Caudio_out&clientId="
157 "157299995976393660&autoJoinPolicy=tab_and_origin_scoped&"
158 "defaultActionPolicy=create_session&launchTimeout=30000");
159 Url url(cast);
160 EXPECT_TRUE(url.is_valid());
161 EXPECT_TRUE(url.has_query());
162 EXPECT_EQ(url.scheme(), "cast");
163 EXPECT_EQ(url.path(), "233637DE");
164 EXPECT_EQ(url.query(), cast.c_str() + 14);
165 }
166
167 } // namespace openscreen
168