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