@@ -39,6 +39,26 @@ struct SC::HttpURLParserTest : public SC::TestCase
39
39
{
40
40
testUTF8 ();
41
41
}
42
+ if (test_section (" edgeCases" ))
43
+ {
44
+ testEdgeCases ();
45
+ }
46
+ if (test_section (" protocols" ))
47
+ {
48
+ testProtocols ();
49
+ }
50
+ if (test_section (" queryParams" ))
51
+ {
52
+ testQueryParams ();
53
+ }
54
+ if (test_section (" specialChars" ))
55
+ {
56
+ testSpecialChars ();
57
+ }
58
+ if (test_section (" ipAddresses" ))
59
+ {
60
+ testIPAddresses ();
61
+ }
42
62
}
43
63
44
64
void testFull ();
@@ -48,6 +68,11 @@ struct SC::HttpURLParserTest : public SC::TestCase
48
68
void testIPv6 ();
49
69
void testInvalidPort ();
50
70
void testUTF8 ();
71
+ void testEdgeCases ();
72
+ void testProtocols ();
73
+ void testQueryParams ();
74
+ void testSpecialChars ();
75
+ void testIPAddresses ();
51
76
};
52
77
53
78
void SC::HttpURLParserTest::testFull ()
@@ -172,6 +197,139 @@ void SC::HttpURLParserTest::testUTF8()
172
197
SC_TEST_EXPECT (urlParser.hash == " #frâg" _u8);
173
198
}
174
199
200
+ void SC::HttpURLParserTest::testEdgeCases ()
201
+ {
202
+ HttpURLParser urlParser;
203
+
204
+ // Username only (no password)
205
+ SC_TEST_EXPECT (urlParser.parse (" http://user@example.com/path" ));
206
+ SC_TEST_EXPECT (urlParser.username == " user" );
207
+ SC_TEST_EXPECT (urlParser.password .isEmpty ());
208
+ SC_TEST_EXPECT (urlParser.hostname == " example.com" );
209
+
210
+ // Root path only
211
+ SC_TEST_EXPECT (urlParser.parse (" http://example.com/" ));
212
+ SC_TEST_EXPECT (urlParser.pathname == " /" );
213
+ SC_TEST_EXPECT (urlParser.path == " /" );
214
+
215
+ // No path at all
216
+ SC_TEST_EXPECT (urlParser.parse (" http://example.com" ));
217
+ SC_TEST_EXPECT (urlParser.pathname == " /" );
218
+ SC_TEST_EXPECT (urlParser.path == " /" );
219
+
220
+ // Port 0
221
+ SC_TEST_EXPECT (urlParser.parse (" http://example.com:0/path" ));
222
+ SC_TEST_EXPECT (urlParser.port == 0 );
223
+
224
+ // Very long path
225
+ SC_TEST_EXPECT (urlParser.parse (" http://example.com/very/long/path/with/many/segments" ));
226
+ SC_TEST_EXPECT (urlParser.pathname == " /very/long/path/with/many/segments" );
227
+
228
+ // Path with dots
229
+ SC_TEST_EXPECT (urlParser.parse (" http://example.com/path/./subpath/../other" ));
230
+ SC_TEST_EXPECT (urlParser.pathname == " /path/./subpath/../other" );
231
+ }
232
+
233
+ void SC::HttpURLParserTest::testProtocols ()
234
+ {
235
+ HttpURLParser urlParser;
236
+
237
+ // HTTPS protocol
238
+ SC_TEST_EXPECT (urlParser.parse (" https://example.com" ));
239
+ SC_TEST_EXPECT (urlParser.protocol == " https" );
240
+ SC_TEST_EXPECT (urlParser.port == 443 );
241
+
242
+ // Mixed case protocol
243
+ SC_TEST_EXPECT (urlParser.parse (" Https://example.com" ));
244
+ SC_TEST_EXPECT (urlParser.protocol == " Https" );
245
+
246
+ // Invalid protocol
247
+ SC_TEST_EXPECT (not urlParser.parse (" ftp://example.com" ));
248
+ SC_TEST_EXPECT (not urlParser.parse (" custom://example.com" ));
249
+ }
250
+
251
+ void SC::HttpURLParserTest::testQueryParams ()
252
+ {
253
+ HttpURLParser urlParser;
254
+
255
+ // Multiple query parameters
256
+ SC_TEST_EXPECT (urlParser.parse (" http://example.com/path?key1=value1&key2=value2&key3=value3" ));
257
+ SC_TEST_EXPECT (urlParser.search == " ?key1=value1&key2=value2&key3=value3" );
258
+
259
+ // Query parameter with empty value
260
+ SC_TEST_EXPECT (urlParser.parse (" http://example.com/path?empty=&key=value" ));
261
+ SC_TEST_EXPECT (urlParser.search == " ?empty=&key=value" );
262
+
263
+ // Query parameter with no value
264
+ SC_TEST_EXPECT (urlParser.parse (" http://example.com/path?flag&key=value" ));
265
+ SC_TEST_EXPECT (urlParser.search == " ?flag&key=value" );
266
+
267
+ // Only query parameters, no path
268
+ SC_TEST_EXPECT (urlParser.parse (" http://example.com?query=value" ));
269
+ SC_TEST_EXPECT (urlParser.pathname == " /" );
270
+ SC_TEST_EXPECT (urlParser.search == " ?query=value" );
271
+
272
+ // Query parameters with special characters
273
+ SC_TEST_EXPECT (urlParser.parse (" http://example.com/path?q=hello%20world&special=%2B%2D" ));
274
+ SC_TEST_EXPECT (urlParser.search == " ?q=hello%20world&special=%2B%2D" );
275
+ }
276
+
277
+ void SC::HttpURLParserTest::testSpecialChars ()
278
+ {
279
+ HttpURLParser urlParser;
280
+
281
+ // Path with allowed special characters
282
+ SC_TEST_EXPECT (urlParser.parse (" http://example.com/path_with_underscores-and-dashes" ));
283
+ SC_TEST_EXPECT (urlParser.pathname == " /path_with_underscores-and-dashes" );
284
+
285
+ // Path with numbers
286
+ SC_TEST_EXPECT (urlParser.parse (" http://example.com/path123/456" ));
287
+ SC_TEST_EXPECT (urlParser.pathname == " /path123/456" );
288
+
289
+ // Hostname with numbers
290
+ SC_TEST_EXPECT (urlParser.parse (" http://site123.com/path" ));
291
+ SC_TEST_EXPECT (urlParser.hostname == " site123.com" );
292
+
293
+ // Username with special characters
294
+ SC_TEST_EXPECT (urlParser.parse (" http://user_name@example.com/path" ));
295
+ SC_TEST_EXPECT (urlParser.username == " user_name" );
296
+
297
+ // Invalid: space in path (should fail)
298
+ SC_TEST_EXPECT (not urlParser.parse (" http://example.com/path with space" ));
299
+ }
300
+
301
+ void SC::HttpURLParserTest::testIPAddresses ()
302
+ {
303
+ HttpURLParser urlParser;
304
+
305
+ // IPv4 address
306
+ SC_TEST_EXPECT (urlParser.parse (" http://192.168.1.1/path" ));
307
+ SC_TEST_EXPECT (urlParser.hostname == " 192.168.1.1" );
308
+ SC_TEST_EXPECT (urlParser.port == 80 );
309
+
310
+ // IPv4 with port
311
+ SC_TEST_EXPECT (urlParser.parse (" http://192.168.1.1:8080/path" ));
312
+ SC_TEST_EXPECT (urlParser.hostname == " 192.168.1.1" );
313
+ SC_TEST_EXPECT (urlParser.port == 8080 );
314
+
315
+ // IPv6 localhost
316
+ SC_TEST_EXPECT (urlParser.parse (" http://[::1]/path" ));
317
+ SC_TEST_EXPECT (urlParser.hostname == " [::1]" );
318
+
319
+ // IPv6 with port
320
+ SC_TEST_EXPECT (urlParser.parse (" http://[::1]:8080/path" ));
321
+ SC_TEST_EXPECT (urlParser.hostname == " [::1]" );
322
+ SC_TEST_EXPECT (urlParser.port == 8080 );
323
+
324
+ // IPv6 full address
325
+ SC_TEST_EXPECT (urlParser.parse (" http://[2001:db8::1]/path" ));
326
+ SC_TEST_EXPECT (urlParser.hostname == " [2001:db8::1]" );
327
+
328
+ // IPv6 compressed
329
+ SC_TEST_EXPECT (urlParser.parse (" http://[2001:db8::]/path" ));
330
+ SC_TEST_EXPECT (urlParser.hostname == " [2001:db8::]" );
331
+ }
332
+
175
333
namespace SC
176
334
{
177
335
void runHttpURLParserTest (SC::TestReport& report) { HttpURLParserTest test (report); }
0 commit comments