1 #include "node_url.h"
2 #include "node_i18n.h"
3 #include "util-inl.h"
4
5 #include "gtest/gtest.h"
6
7 using node::url::URL;
8 using node::url::URL_FLAGS_FAILED;
9
10 class URLTest : public ::testing::Test {
11 protected:
SetUp()12 void SetUp() override {
13 #if defined(NODE_HAVE_I18N_SUPPORT)
14 std::string icu_data_dir;
15 node::i18n::InitializeICUDirectory(icu_data_dir);
16 #endif
17 }
18
TearDown()19 void TearDown() override {}
20 };
21
TEST_F(URLTest,Simple)22 TEST_F(URLTest, Simple) {
23 URL simple("https://example.org:81/a/b/c?query#fragment");
24
25 EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
26 EXPECT_EQ(simple.protocol(), "https:");
27 EXPECT_EQ(simple.host(), "example.org");
28 EXPECT_EQ(simple.port(), 81);
29 EXPECT_EQ(simple.path(), "/a/b/c");
30 EXPECT_EQ(simple.query(), "query");
31 EXPECT_EQ(simple.fragment(), "fragment");
32 }
33
TEST_F(URLTest,Simple2)34 TEST_F(URLTest, Simple2) {
35 const char* input = "https://example.org:81/a/b/c?query#fragment";
36 URL simple(input, strlen(input));
37
38 EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
39 EXPECT_EQ(simple.protocol(), "https:");
40 EXPECT_EQ(simple.host(), "example.org");
41 EXPECT_EQ(simple.port(), 81);
42 EXPECT_EQ(simple.path(), "/a/b/c");
43 EXPECT_EQ(simple.query(), "query");
44 EXPECT_EQ(simple.fragment(), "fragment");
45 }
46
TEST_F(URLTest,NoBase1)47 TEST_F(URLTest, NoBase1) {
48 URL error("123noscheme");
49 EXPECT_TRUE(error.flags() & URL_FLAGS_FAILED);
50 }
51
TEST_F(URLTest,Base1)52 TEST_F(URLTest, Base1) {
53 URL base("http://example.org/foo/bar");
54 ASSERT_FALSE(base.flags() & URL_FLAGS_FAILED);
55
56 URL simple("../baz", &base);
57 EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
58 EXPECT_EQ(simple.protocol(), "http:");
59 EXPECT_EQ(simple.host(), "example.org");
60 EXPECT_EQ(simple.path(), "/baz");
61 }
62
TEST_F(URLTest,Base2)63 TEST_F(URLTest, Base2) {
64 URL simple("../baz", "http://example.org/foo/bar");
65
66 EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
67 EXPECT_EQ(simple.protocol(), "http:");
68 EXPECT_EQ(simple.host(), "example.org");
69 EXPECT_EQ(simple.path(), "/baz");
70 }
71
TEST_F(URLTest,Base3)72 TEST_F(URLTest, Base3) {
73 const char* input = "../baz";
74 const char* base = "http://example.org/foo/bar";
75
76 URL simple(input, strlen(input), base, strlen(base));
77
78 EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
79 EXPECT_EQ(simple.protocol(), "http:");
80 EXPECT_EQ(simple.host(), "example.org");
81 EXPECT_EQ(simple.path(), "/baz");
82 }
83
TEST_F(URLTest,Base4)84 TEST_F(URLTest, Base4) {
85 const char* input = "\\x";
86 const char* base = "http://example.org/foo/bar";
87
88 URL simple(input, strlen(input), base, strlen(base));
89
90 EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
91 EXPECT_EQ(simple.protocol(), "http:");
92 EXPECT_EQ(simple.host(), "example.org");
93 EXPECT_EQ(simple.path(), "/x");
94 }
95
TEST_F(URLTest,Base5)96 TEST_F(URLTest, Base5) {
97 const char* input = "/x";
98 const char* base = "http://example.org/foo/bar";
99
100 URL simple(input, strlen(input), base, strlen(base));
101
102 EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
103 EXPECT_EQ(simple.protocol(), "http:");
104 EXPECT_EQ(simple.host(), "example.org");
105 EXPECT_EQ(simple.path(), "/x");
106 }
107
TEST_F(URLTest,Base6)108 TEST_F(URLTest, Base6) {
109 const char* input = "\\\\x";
110 const char* base = "http://example.org/foo/bar";
111
112 URL simple(input, strlen(input), base, strlen(base));
113
114 EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
115 EXPECT_EQ(simple.protocol(), "http:");
116 EXPECT_EQ(simple.host(), "x");
117 }
118
TEST_F(URLTest,Base7)119 TEST_F(URLTest, Base7) {
120 const char* input = "//x";
121 const char* base = "http://example.org/foo/bar";
122
123 URL simple(input, strlen(input), base, strlen(base));
124
125 EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
126 EXPECT_EQ(simple.protocol(), "http:");
127 EXPECT_EQ(simple.host(), "x");
128 }
129
TEST_F(URLTest,TruncatedAfterProtocol)130 TEST_F(URLTest, TruncatedAfterProtocol) {
131 char input[2] = { 'q', ':' };
132 URL simple(input, sizeof(input));
133
134 EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
135 EXPECT_EQ(simple.protocol(), "q:");
136 EXPECT_EQ(simple.host(), "");
137 EXPECT_EQ(simple.path(), "/");
138 }
139
TEST_F(URLTest,TruncatedAfterProtocol2)140 TEST_F(URLTest, TruncatedAfterProtocol2) {
141 char input[6] = { 'h', 't', 't', 'p', ':', '/' };
142 URL simple(input, sizeof(input));
143
144 EXPECT_TRUE(simple.flags() & URL_FLAGS_FAILED);
145 EXPECT_EQ(simple.protocol(), "http:");
146 EXPECT_EQ(simple.host(), "");
147 EXPECT_EQ(simple.path(), "");
148 }
149
TEST_F(URLTest,ToFilePath)150 TEST_F(URLTest, ToFilePath) {
151 #define T(url, path) EXPECT_EQ(path, URL(url).ToFilePath())
152 T("http://example.org/foo/bar", "");
153
154 #ifdef _WIN32
155 T("file:///C:/Program%20Files/", "C:\\Program Files\\");
156 T("file:///C:/a/b/c?query#fragment", "C:\\a\\b\\c");
157 T("file://host/path/a/b/c?query#fragment", "\\\\host\\path\\a\\b\\c");
158 #if defined(NODE_HAVE_I18N_SUPPORT)
159 T("file://xn--weird-prdj8vva.com/host/a", "\\\\wͪ͊eiͬ͋rd.com\\host\\a");
160 #else
161 T("file://xn--weird-prdj8vva.com/host/a",
162 "\\\\xn--weird-prdj8vva.com\\host\\a");
163 #endif
164 T("file:///C:/a%2Fb", "");
165 T("file:///", "");
166 T("file:///home", "");
167 #else
168 T("file:///", "/");
169 T("file:///home/user?query#fragment", "/home/user");
170 T("file:///home/user/?query#fragment", "/home/user/");
171 T("file:///home/user/%20space", "/home/user/ space");
172 T("file:///home/us%5Cer", "/home/us\\er");
173 T("file:///home/us%2Fer", "");
174 T("file://host/path", "");
175 #endif
176
177 #undef T
178 }
179
TEST_F(URLTest,FromFilePath)180 TEST_F(URLTest, FromFilePath) {
181 URL file_url;
182 #ifdef _WIN32
183 file_url = URL::FromFilePath("C:\\Program Files\\");
184 EXPECT_EQ("file:", file_url.protocol());
185 EXPECT_EQ("/C:/Program%20Files/", file_url.path());
186
187 file_url = URL::FromFilePath("C:\\a\\b\\c");
188 EXPECT_EQ("file:", file_url.protocol());
189 EXPECT_EQ("/C:/a/b/c", file_url.path());
190
191 file_url = URL::FromFilePath("b:\\a\\%%.js");
192 EXPECT_EQ("file:", file_url.protocol());
193 EXPECT_EQ("/b:/a/%25%25.js", file_url.path());
194 #else
195 file_url = URL::FromFilePath("/");
196 EXPECT_EQ("file:", file_url.protocol());
197 EXPECT_EQ("/", file_url.path());
198
199 file_url = URL::FromFilePath("/a/b/c");
200 EXPECT_EQ("file:", file_url.protocol());
201 EXPECT_EQ("/a/b/c", file_url.path());
202
203 file_url = URL::FromFilePath("/a/%%.js");
204 EXPECT_EQ("file:", file_url.protocol());
205 EXPECT_EQ("/a/%25%25.js", file_url.path());
206 #endif
207 }
208