proxygen
ParseURLTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
12 
13 using proxygen::ParseURL;
14 using std::string;
15 
16 void testParseURL(const string& url,
17  const string& expectedScheme,
18  const string& expectedPath,
19  const string& expectedQuery,
20  const string& expectedHost,
21  const uint16_t expectedPort,
22  const string& expectedAuthority,
23  const bool expectedValid = true) {
24  ParseURL u(url);
25 
26  if (expectedValid) {
27  EXPECT_EQ(url, u.url());
28  EXPECT_EQ(expectedScheme, u.scheme());
29  EXPECT_EQ(expectedPath, u.path());
30  EXPECT_EQ(expectedQuery, u.query());
31  EXPECT_EQ(expectedHost, u.host());
32  EXPECT_EQ(expectedPort, u.port());
33  EXPECT_EQ(expectedAuthority, u.authority());
34  EXPECT_EQ(expectedValid, u.valid());
35  } else {
36  // invalid, do not need to test values
37  EXPECT_EQ(expectedValid, u.valid());
38  }
39 }
40 
41 void testHostIsIpAddress(const string& url, const bool expected) {
42  ParseURL u(url);
43  EXPECT_EQ(expected, u.hostIsIPAddress());
44 }
45 
46 TEST(ParseURL, HostNoBrackets) {
47  ParseURL p("/bar");
48 
49  EXPECT_EQ("", p.host());
50  EXPECT_EQ("", p.hostNoBrackets());
51 }
52 
53 TEST(ParseURL, FullyFormedURL) {
54  testParseURL("http://localhost:80/foo?bar#qqq", "http", "/foo", "bar",
55  "localhost", 80, "localhost:80");
56  testParseURL("http://localhost:80/foo?bar", "http", "/foo", "bar",
57  "localhost", 80, "localhost:80");
58  testParseURL("http://localhost:80/foo", "http", "/foo", "",
59  "localhost", 80, "localhost:80");
60  testParseURL("http://localhost:80/", "http", "/", "",
61  "localhost", 80, "localhost:80");
62  testParseURL("http://localhost:80", "http", "", "",
63  "localhost", 80, "localhost:80");
64  testParseURL("http://localhost", "http", "", "",
65  "localhost", 0, "localhost");
66  testParseURL("http://[2401:db00:2110:3051:face:0:3f:0]/", "http", "/", "",
67  "[2401:db00:2110:3051:face:0:3f:0]", 0,
68  "[2401:db00:2110:3051:face:0:3f:0]");
69 }
70 
71 TEST(ParseURL, ValidNonHttpScheme) {
72  testParseURL("https://localhost:80/foo?bar#qqq", "https", "/foo", "bar",
73  "localhost", 80, "localhost:80");
74  testParseURL("rtmp://localhost:80/foo?bar#qqq", "rtmp", "/foo", "bar",
75  "localhost", 80, "localhost:80");
76  testParseURL("ftp://localhost:80/foo?bar#qqq", "ftp", "/foo", "bar",
77  "localhost", 80, "localhost:80");
78  testParseURL("proxygen://localhost:80/foo?bar#qqq", "proxygen", "/foo", "bar",
79  "localhost", 80, "localhost:80");
80  testParseURL("test://localhost:80/foo?bar#qqq", "test", "/foo", "bar",
81  "localhost", 80, "localhost:80");
82 }
83 
84 TEST(ParseURL, InvalidScheme) {
85  testParseURL("test123://localhost:80", "", "", "", "", 0, "", false);
86  testParseURL("test.1://localhost:80", "", "", "", "", 0, "", false);
87  testParseURL("://localhost:80", "", "", "", "", 0, "", false);
88  testParseURL("123://localhost:80", "", "", "", "", 0, "", false);
89  testParseURL("---://localhost:80", "", "", "", "", 0, "", false);
90 }
91 
92 TEST(ParseURL, NoScheme) {
93  testParseURL("localhost:80/foo?bar#qqq", "", "/foo", "bar",
94  "localhost", 80, "localhost:80");
95  testParseURL("localhost:80/foo?bar", "", "/foo", "bar",
96  "localhost", 80, "localhost:80");
97  testParseURL("localhost:80/foo", "", "/foo", "",
98  "localhost", 80, "localhost:80");
99  testParseURL("localhost:80/", "", "/", "",
100  "localhost", 80, "localhost:80");
101  testParseURL("localhost:80", "", "", "",
102  "localhost", 80, "localhost:80");
103  testParseURL("localhost", "", "", "",
104  "localhost", 0, "localhost");
105 }
106 
107 TEST(ParseURL, NoSchemeIP) {
108  testParseURL("1.2.3.4:54321/foo?bar#qqq", "",
109  "/foo", "bar", "1.2.3.4", 54321, "1.2.3.4:54321");
110  testParseURL("[::1]:80/foo?bar", "", "/foo", "bar", "[::1]", 80, "[::1]:80");
111  testParseURL("[::1]/foo?bar", "", "/foo", "bar", "[::1]", 0, "[::1]");
112 }
113 
114 TEST(ParseURL, PathOnly) {
115  testParseURL("/f/o/o?bar#qqq", "", "/f/o/o", "bar", "", 0, "");
116  testParseURL("/f/o/o?bar", "", "/f/o/o", "bar", "", 0, "");
117  testParseURL("/f/o/o", "", "/f/o/o", "", "", 0, "");
118  testParseURL("/", "", "/", "", "", 0, "");
119  testParseURL("?foo=bar", "", "", "foo=bar", "", 0, "");
120  testParseURL("?#", "", "", "", "", 0, "");
121  testParseURL("#/foo/bar", "", "", "", "", 0, "");
122 }
123 
124 TEST(ParseURL, QueryIsURL) {
125  testParseURL("/?ids=http://vlc.afreecodec.com/download/", "",
126  "/", "ids=http://vlc.afreecodec.com/download/", "", 0, "");
127  testParseURL("/plugins/facepile.php?href=http://www.vakan.nl/hotels",
128  "", "/plugins/facepile.php",
129  "href=http://www.vakan.nl/hotels", "", 0, "");
130 }
131 
132 TEST(ParseURL, InvalidURL) {
133  testParseURL("http://tel:198433511/", "", "", "", "", 0, "", false);
134  testParseURL("localhost:80/foo#bar?qqq", "", "", "", "", 0, "", false);
135  testParseURL("#?", "", "", "", "", 0, "", false);
136  testParseURL("#?hello", "", "", "", "", 0, "", false);
137  testParseURL("[::1/foo?bar", "", "", "", "", 0, "", false);
138  testParseURL("", "", "", "", "", 0, "", false);
139  testParseURL("http://tel:198433511/test\n", "", "", "", "", 0, "", false);
140  testParseURL("/test\n", "", "", "", "", 0, "", false);
141 }
142 
143 TEST(ParseURL, IsHostIPAddress) {
144  testHostIsIpAddress("http://127.0.0.1:80", true);
145  testHostIsIpAddress("127.0.0.1", true);
146  testHostIsIpAddress("http://[::1]:80", true);
147  testHostIsIpAddress("[::1]", true);
148  testHostIsIpAddress("[::AB]", true);
149 
150  testHostIsIpAddress("http://localhost:80", false);
151  testHostIsIpAddress("http://localhost", false);
152  testHostIsIpAddress("localhost", false);
153  testHostIsIpAddress("1.2.3.-1", false);
154  testHostIsIpAddress("1.2.3.999", false);
155  testHostIsIpAddress("::1", false);
156  testHostIsIpAddress("[::99999999]", false);
157 
158  // invalid url
159  testHostIsIpAddress("", false);
160  testHostIsIpAddress("127.0.0.1:80/foo#bar?qqq", false);
161 }
std::string authority() const
Definition: ParseURL.h:44
void testParseURL(const string &url, const string &expectedScheme, const string &expectedPath, const string &expectedQuery, const string &expectedHost, const uint16_t expectedPort, const string &expectedAuthority, const bool expectedValid=true)
folly::StringPiece query() const
Definition: ParseURL.h:72
void testHostIsIpAddress(const string &url, const bool expected)
folly::StringPiece url() const
Definition: ParseURL.h:36
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
folly::StringPiece hostNoBrackets()
Definition: ParseURL.h:84
TEST(ParseURL, HostNoBrackets)
folly::StringPiece host() const
Definition: ParseURL.h:52
folly::StringPiece path() const
Definition: ParseURL.h:68
bool valid() const
Definition: ParseURL.h:80
uint16_t port() const
Definition: ParseURL.h:56
const char * string
Definition: Conv.cpp:212
bool hostIsIPAddress()
Definition: ParseURL.cpp:177
folly::StringPiece scheme() const
Definition: ParseURL.h:40