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 "quiche/quic/tools/quic_url.h"
6
7 #include <string>
8
9 #include "quiche/quic/platform/api/quic_test.h"
10
11 namespace quic {
12 namespace test {
13 namespace {
14
15 class QuicUrlTest : public QuicTest {};
16
TEST_F(QuicUrlTest,Basic)17 TEST_F(QuicUrlTest, Basic) {
18 // No scheme specified.
19 std::string url_str = "www.example.com";
20 QuicUrl url(url_str);
21 EXPECT_FALSE(url.IsValid());
22
23 // scheme is HTTP.
24 url_str = "http://www.example.com";
25 url = QuicUrl(url_str);
26 EXPECT_TRUE(url.IsValid());
27 EXPECT_EQ("http://www.example.com/", url.ToString());
28 EXPECT_EQ("http", url.scheme());
29 EXPECT_EQ("www.example.com", url.HostPort());
30 EXPECT_EQ("/", url.PathParamsQuery());
31 EXPECT_EQ(80u, url.port());
32
33 // scheme is HTTPS.
34 url_str = "https://www.example.com:12345/path/to/resource?a=1&campaign=2";
35 url = QuicUrl(url_str);
36 EXPECT_TRUE(url.IsValid());
37 EXPECT_EQ("https://www.example.com:12345/path/to/resource?a=1&campaign=2",
38 url.ToString());
39 EXPECT_EQ("https", url.scheme());
40 EXPECT_EQ("www.example.com:12345", url.HostPort());
41 EXPECT_EQ("/path/to/resource?a=1&campaign=2", url.PathParamsQuery());
42 EXPECT_EQ(12345u, url.port());
43
44 // scheme is FTP.
45 url_str = "ftp://www.example.com";
46 url = QuicUrl(url_str);
47 EXPECT_TRUE(url.IsValid());
48 EXPECT_EQ("ftp://www.example.com/", url.ToString());
49 EXPECT_EQ("ftp", url.scheme());
50 EXPECT_EQ("www.example.com", url.HostPort());
51 EXPECT_EQ("/", url.PathParamsQuery());
52 EXPECT_EQ(21u, url.port());
53 }
54
TEST_F(QuicUrlTest,DefaultScheme)55 TEST_F(QuicUrlTest, DefaultScheme) {
56 // Default scheme to HTTP.
57 std::string url_str = "www.example.com";
58 QuicUrl url(url_str, "http");
59 EXPECT_EQ("http://www.example.com/", url.ToString());
60 EXPECT_EQ("http", url.scheme());
61
62 // URL already has a scheme specified.
63 url_str = "http://www.example.com";
64 url = QuicUrl(url_str, "https");
65 EXPECT_EQ("http://www.example.com/", url.ToString());
66 EXPECT_EQ("http", url.scheme());
67
68 // Default scheme to FTP.
69 url_str = "www.example.com";
70 url = QuicUrl(url_str, "ftp");
71 EXPECT_EQ("ftp://www.example.com/", url.ToString());
72 EXPECT_EQ("ftp", url.scheme());
73 }
74
TEST_F(QuicUrlTest,IsValid)75 TEST_F(QuicUrlTest, IsValid) {
76 std::string url_str =
77 "ftp://www.example.com:12345/path/to/resource?a=1&campaign=2";
78 EXPECT_TRUE(QuicUrl(url_str).IsValid());
79
80 // Invalid characters in host name.
81 url_str = "https://www%.example.com:12345/path/to/resource?a=1&campaign=2";
82 EXPECT_FALSE(QuicUrl(url_str).IsValid());
83
84 // Invalid characters in scheme.
85 url_str = "%http://www.example.com:12345/path/to/resource?a=1&campaign=2";
86 EXPECT_FALSE(QuicUrl(url_str).IsValid());
87
88 // Host name too long.
89 std::string host(1024, 'a');
90 url_str = "https://" + host;
91 EXPECT_FALSE(QuicUrl(url_str).IsValid());
92
93 // Invalid port number.
94 url_str = "https://www..example.com:123456/path/to/resource?a=1&campaign=2";
95 EXPECT_FALSE(QuicUrl(url_str).IsValid());
96 }
97
TEST_F(QuicUrlTest,HostPort)98 TEST_F(QuicUrlTest, HostPort) {
99 std::string url_str = "http://www.example.com/";
100 QuicUrl url(url_str);
101 EXPECT_EQ("www.example.com", url.HostPort());
102 EXPECT_EQ("www.example.com", url.host());
103 EXPECT_EQ(80u, url.port());
104
105 url_str = "http://www.example.com:80/";
106 url = QuicUrl(url_str);
107 EXPECT_EQ("www.example.com", url.HostPort());
108 EXPECT_EQ("www.example.com", url.host());
109 EXPECT_EQ(80u, url.port());
110
111 url_str = "http://www.example.com:81/";
112 url = QuicUrl(url_str);
113 EXPECT_EQ("www.example.com:81", url.HostPort());
114 EXPECT_EQ("www.example.com", url.host());
115 EXPECT_EQ(81u, url.port());
116
117 url_str = "https://192.168.1.1:443/";
118 url = QuicUrl(url_str);
119 EXPECT_EQ("192.168.1.1", url.HostPort());
120 EXPECT_EQ("192.168.1.1", url.host());
121 EXPECT_EQ(443u, url.port());
122
123 url_str = "http://[2001::1]:80/";
124 url = QuicUrl(url_str);
125 EXPECT_EQ("[2001::1]", url.HostPort());
126 EXPECT_EQ("2001::1", url.host());
127 EXPECT_EQ(80u, url.port());
128
129 url_str = "http://[2001::1]:81/";
130 url = QuicUrl(url_str);
131 EXPECT_EQ("[2001::1]:81", url.HostPort());
132 EXPECT_EQ("2001::1", url.host());
133 EXPECT_EQ(81u, url.port());
134 }
135
TEST_F(QuicUrlTest,PathParamsQuery)136 TEST_F(QuicUrlTest, PathParamsQuery) {
137 std::string url_str =
138 "https://www.example.com:12345/path/to/resource?a=1&campaign=2";
139 QuicUrl url(url_str);
140 EXPECT_EQ("/path/to/resource?a=1&campaign=2", url.PathParamsQuery());
141 EXPECT_EQ("/path/to/resource", url.path());
142
143 url_str = "https://www.example.com/?";
144 url = QuicUrl(url_str);
145 EXPECT_EQ("/?", url.PathParamsQuery());
146 EXPECT_EQ("/", url.path());
147
148 url_str = "https://www.example.com/";
149 url = QuicUrl(url_str);
150 EXPECT_EQ("/", url.PathParamsQuery());
151 EXPECT_EQ("/", url.path());
152 }
153
154 } // namespace
155 } // namespace test
156 } // namespace quic
157