blob: ccb7aea0ebdab766a30e0dba1958918b40578ce4 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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
QUICHE team5be974e2020-12-29 18:35:24 -05005#include "quic/tools/quic_url.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -05006
vasilvv872e7a32019-03-12 16:42:44 -07007#include <string>
8
QUICHE team5be974e2020-12-29 18:35:24 -05009#include "quic/platform/api/quic_test.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050010
11namespace quic {
12namespace test {
13namespace {
14
15class QuicUrlTest : public QuicTest {};
16
17TEST_F(QuicUrlTest, Basic) {
18 // No scheme specified.
vasilvvc48c8712019-03-11 13:38:16 -070019 std::string url_str = "www.example.com";
QUICHE teama6ef0a62019-03-07 20:34:33 -050020 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
55TEST_F(QuicUrlTest, DefaultScheme) {
56 // Default scheme to HTTP.
vasilvvc48c8712019-03-11 13:38:16 -070057 std::string url_str = "www.example.com";
QUICHE teama6ef0a62019-03-07 20:34:33 -050058 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
75TEST_F(QuicUrlTest, IsValid) {
vasilvvc48c8712019-03-11 13:38:16 -070076 std::string url_str =
QUICHE teama6ef0a62019-03-07 20:34:33 -050077 "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.
vasilvvc48c8712019-03-11 13:38:16 -070089 std::string host(1024, 'a');
QUICHE teama6ef0a62019-03-07 20:34:33 -050090 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
98TEST_F(QuicUrlTest, HostPort) {
vasilvvc48c8712019-03-11 13:38:16 -070099 std::string url_str = "http://www.example.com/";
QUICHE teama6ef0a62019-03-07 20:34:33 -0500100 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
136TEST_F(QuicUrlTest, PathParamsQuery) {
vasilvvc48c8712019-03-11 13:38:16 -0700137 std::string url_str =
QUICHE teama6ef0a62019-03-07 20:34:33 -0500138 "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