sorug 0.6.2

Ultra-high-performance, zero-copy, WHATWG-compliant URL parser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Positive and negative coverage for [`sorug::PathSegmentsMut`].
//!
//! Oracle: servo/`url` 2.5 where the APIs are intended to match. Opaque-path
//! rejection and encoding of `/`, `%`, and special-scheme `\` are locked in.

use sorug::Url;
use url::Url as ServoUrl;

fn servo(input: &str) -> ServoUrl {
    ServoUrl::parse(input).unwrap_or_else(|e| panic!("servo parse {input:?}: {e}"))
}

fn sorug(input: &str) -> Url<'static> {
    Url::parse(input)
        .unwrap_or_else(|e| panic!("sorug parse {input:?}: {e:?}"))
        .into_owned()
}

fn assert_match(label: &str, input: &str, mutate: impl Fn(&mut Url<'_>, &mut ServoUrl)) {
    let mut s = sorug(input);
    let mut v = servo(input);
    mutate(&mut s, &mut v);
    assert_eq!(
        s.as_str(),
        v.as_str(),
        "{label}: input={input:?}\n  sorug={}\n  servo={}",
        s.as_str(),
        v.as_str()
    );
    assert_eq!(s.query(), v.query(), "{label} query");
    assert_eq!(s.fragment(), v.fragment(), "{label} fragment");
    assert_eq!(s.path(), v.path(), "{label} path");
}

// ---------------------------------------------------------------------------
// Negative: cannot-be-a-base
// ---------------------------------------------------------------------------

#[test]
fn negative_opaque_paths_reject_mut() {
    for input in [
        "mailto:me@example.com",
        "data:text/plain,hi",
        "javascript:alert(1)",
        "blob:https://example.com/uuid",
        "about:blank",
    ] {
        let mut url = Url::parse(input).unwrap_or_else(|e| panic!("{input}: {e:?}"));
        assert!(url.cannot_be_a_base(), "{input} should be cannot-be-a-base");
        assert!(
            url.path_segments_mut().is_err(),
            "{input} must reject path_segments_mut"
        );
        assert!(
            url.path_segments().is_none(),
            "{input} must yield no path_segments iterator"
        );
    }
}

#[test]
fn negative_dot_segments_are_ignored_not_traversed() {
    assert_match("ignore dots", "https://example.com/a", |s, v| {
        s.path_segments_mut().unwrap().extend([".", "..", "..."]);
        v.path_segments_mut().unwrap().extend([".", "..", "..."]);
    });
    // `...` is a real segment (not ignored); `.` / `..` alone are skipped.
    let mut url = sorug("https://example.com/a");
    url.path_segments_mut().unwrap().extend([".", "..", "..."]);
    assert_eq!(url.as_str(), "https://example.com/a/...");
}

#[test]
fn negative_root_pop_and_pop_if_empty_are_noops() {
    assert_match("root pop", "https://example.com/", |s, v| {
        s.path_segments_mut().unwrap().pop();
        v.path_segments_mut().unwrap().pop();
    });
    assert_match("root pop_if_empty", "https://example.com/", |s, v| {
        s.path_segments_mut().unwrap().pop_if_empty();
        v.path_segments_mut().unwrap().pop_if_empty();
    });
    assert_match("root clear", "https://example.com/", |s, v| {
        s.path_segments_mut().unwrap().clear();
        v.path_segments_mut().unwrap().clear();
    });
}

#[test]
fn negative_empty_nonspecial_pop_is_noop() {
    assert_match("empty pop", "foo://host", |s, v| {
        s.path_segments_mut().unwrap().pop();
        v.path_segments_mut().unwrap().pop();
    });
    assert_match("empty pop_if_empty", "foo://host", |s, v| {
        s.path_segments_mut().unwrap().pop_if_empty();
        v.path_segments_mut().unwrap().pop_if_empty();
    });
}

// ---------------------------------------------------------------------------
// Positive: rust-url docs + encoding
// ---------------------------------------------------------------------------

#[test]
fn positive_docs_examples() {
    assert_match(
        "docs pop push encode",
        "http://example.net/foo/index.html",
        |s, v| {
            s.path_segments_mut()
                .unwrap()
                .pop()
                .push("img")
                .push("2/100%.png");
            v.path_segments_mut()
                .unwrap()
                .pop()
                .push("img")
                .push("2/100%.png");
        },
    );

    assert_match(
        "docs clear push",
        "https://github.com/servo/rust-url/",
        |s, v| {
            s.path_segments_mut().unwrap().clear().push("logout");
            v.path_segments_mut().unwrap().clear().push("logout");
        },
    );

    assert_match(
        "docs pop_if_empty push",
        "https://github.com/servo/rust-url/",
        |s, v| {
            s.path_segments_mut().unwrap().pop_if_empty().push("pulls");
            v.path_segments_mut().unwrap().pop_if_empty().push("pulls");
        },
    );

    assert_match(
        "docs extend skip dots",
        "https://github.com/servo",
        |s, v| {
            s.path_segments_mut()
                .unwrap()
                .extend(["..", "rust-url", ".", "pulls"]);
            v.path_segments_mut()
                .unwrap()
                .extend(["..", "rust-url", ".", "pulls"]);
        },
    );
}

#[test]
fn positive_trailing_slash_semantics() {
    // Without pop_if_empty, trailing slash yields a double slash before push.
    assert_match("trail push double", "https://example.com/a/b/", |s, v| {
        s.path_segments_mut().unwrap().push("c");
        v.path_segments_mut().unwrap().push("c");
    });
    assert_match(
        "trail pop_if_empty push",
        "https://example.com/a/b/",
        |s, v| {
            s.path_segments_mut().unwrap().pop_if_empty().push("c");
            v.path_segments_mut().unwrap().pop_if_empty().push("c");
        },
    );
    assert_match("trail pop", "https://example.com/a/b/", |s, v| {
        s.path_segments_mut().unwrap().pop();
        v.path_segments_mut().unwrap().pop();
    });
}

#[test]
fn positive_empty_path_nonspecial() {
    assert_match("empty push", "foo://host", |s, v| {
        s.path_segments_mut().unwrap().push("a");
        v.path_segments_mut().unwrap().push("a");
    });
    assert_match("empty clear push", "foo://host", |s, v| {
        s.path_segments_mut().unwrap().clear().push("x");
        v.path_segments_mut().unwrap().clear().push("x");
    });
}

#[test]
fn positive_encoding_slash_percent_backslash() {
    assert_match("encode slash+percent", "https://example.com/", |s, v| {
        s.path_segments_mut().unwrap().push("2/100%.png");
        v.path_segments_mut().unwrap().push("2/100%.png");
    });
    assert_match(
        "encode backslash special",
        "https://example.com/",
        |s, v| {
            s.path_segments_mut().unwrap().push("a\\b");
            v.path_segments_mut().unwrap().push("a\\b");
        },
    );
    // Non-special: `\` stays literal (not in path-segment encode set).
    assert_match("literal backslash nonspecial", "foo://host/", |s, v| {
        s.path_segments_mut().unwrap().push("a\\b");
        v.path_segments_mut().unwrap().push("a\\b");
    });
}

#[test]
fn positive_unicode_and_empty_segment() {
    assert_match("unicode café", "https://example.com/", |s, v| {
        s.path_segments_mut().unwrap().push("café");
        v.path_segments_mut().unwrap().push("café");
    });
    assert_match("push empty segment", "https://example.com/a", |s, v| {
        s.path_segments_mut().unwrap().push("");
        v.path_segments_mut().unwrap().push("");
    });
}

#[test]
fn positive_query_fragment_preserved() {
    assert_match(
        "push keeps q+f",
        "https://example.com/a?q=1#frag",
        |s, v| {
            s.path_segments_mut().unwrap().push("b");
            v.path_segments_mut().unwrap().push("b");
        },
    );
    assert_match(
        "clear keeps query",
        "https://example.com/a/b?q=1",
        |s, v| {
            s.path_segments_mut().unwrap().clear();
            v.path_segments_mut().unwrap().clear();
        },
    );
    assert_match(
        "pop keeps fragment only",
        "https://example.com/a/b#f",
        |s, v| {
            s.path_segments_mut().unwrap().pop();
            v.path_segments_mut().unwrap().pop();
        },
    );
    assert_match(
        "clear then push keeps both",
        "https://example.com/a?x=1#y",
        |s, v| {
            s.path_segments_mut().unwrap().clear().push("z");
            v.path_segments_mut().unwrap().clear().push("z");
        },
    );
}

#[test]
fn positive_file_scheme() {
    assert_match("file pop push", "file:///tmp/x", |s, v| {
        s.path_segments_mut().unwrap().pop().push("y");
        v.path_segments_mut().unwrap().pop().push("y");
    });
    assert_match("file clear push", "file:///tmp/x", |s, v| {
        s.path_segments_mut().unwrap().clear().push("z");
        v.path_segments_mut().unwrap().clear().push("z");
    });
}

/// WHATWG Windows drive letter: first `file:` path segment normalizes `|` → `:`.
#[test]
fn positive_file_windows_drive_pipe_normalized() {
    let mut url = sorug("file:");
    url.path_segments_mut().unwrap().push("h|");
    assert_eq!(url.as_str(), "file:///h:");
    assert_eq!(Url::parse(url.as_str()).unwrap().as_str(), "file:///h:");

    let mut url = sorug("file:///x");
    url.path_segments_mut().unwrap().clear().push("D|");
    assert_eq!(url.as_str(), "file:///D:");

    // Later segments keep `|`.
    let mut url = sorug("file:///a");
    url.path_segments_mut().unwrap().push("h|");
    assert_eq!(url.as_str(), "file:///a/h|");
}

#[test]
fn positive_extend_chain_and_joinlike() {
    assert_match("extend multi", "https://example.com/", |s, v| {
        s.path_segments_mut()
            .unwrap()
            .extend(["org", "repo", "issues", "188"]);
        v.path_segments_mut()
            .unwrap()
            .extend(["org", "repo", "issues", "188"]);
    });
    assert_match(
        "joinlike pop extend",
        "https://example.com/dir/page",
        |s, v| {
            s.path_segments_mut().unwrap().pop().extend(["other"]);
            v.path_segments_mut().unwrap().pop().extend(["other"]);
        },
    );
}

#[test]
fn positive_chained_ops_idempotent_drop() {
    // Drop must restore query/fragment exactly once even after many mutations.
    let mut url = sorug("https://example.com/a/b/c?keep=1#hash");
    {
        let mut segs = url.path_segments_mut().unwrap();
        segs.pop().pop().push("x").push("y");
    }
    assert_eq!(url.as_str(), "https://example.com/a/x/y?keep=1#hash");
    assert_eq!(url.query(), Some("keep=1"));
    assert_eq!(url.fragment(), Some("hash"));
    assert_eq!(
        url.path_segments().unwrap().collect::<Vec<_>>(),
        ["a", "x", "y"]
    );
}

#[test]
fn positive_path_segments_iterator_after_mut() {
    let mut url = sorug("https://example.com/");
    url.path_segments_mut().unwrap().extend(["one", "two", ""]);
    assert_eq!(
        url.path_segments().unwrap().collect::<Vec<_>>(),
        ["one", "two", ""]
    );
    assert_eq!(url.path(), "/one/two/");
}

#[test]
fn positive_space_and_question_hash_in_segment() {
    assert_match("space encoded", "https://example.com/", |s, v| {
        s.path_segments_mut().unwrap().push("a b");
        v.path_segments_mut().unwrap().push("a b");
    });
    assert_match("question encoded", "https://example.com/", |s, v| {
        s.path_segments_mut().unwrap().push("a?b");
        v.path_segments_mut().unwrap().push("a?b");
    });
    assert_match("hash encoded", "https://example.com/", |s, v| {
        s.path_segments_mut().unwrap().push("a#b");
        v.path_segments_mut().unwrap().push("a#b");
    });
}

#[test]
fn positive_ws_scheme_special_backslash() {
    assert_match("ws backslash", "ws://example.com/", |s, v| {
        s.path_segments_mut().unwrap().push("a\\b");
        v.path_segments_mut().unwrap().push("a\\b");
    });
}

#[test]
fn negative_mutate_does_not_change_scheme_host_port() {
    let mut url = sorug("https://user:pass@example.com:8443/old?q=1#f");
    url.path_segments_mut().unwrap().clear().push("new");
    assert_eq!(url.scheme(), "https");
    assert_eq!(url.username(), "user");
    assert_eq!(url.password(), "pass");
    assert_eq!(url.host(), Some("example.com"));
    assert_eq!(url.port_u16(), Some(8443));
    assert_eq!(url.query(), Some("q=1"));
    assert_eq!(url.fragment(), Some("f"));
    assert_eq!(url.as_str(), "https://user:pass@example.com:8443/new?q=1#f");
}

#[test]
fn positive_ipv6_host_unaffected() {
    assert_match("ipv6 push", "http://[::1]/foo", |s, v| {
        s.path_segments_mut().unwrap().pop().push("bar");
        v.path_segments_mut().unwrap().pop().push("bar");
    });
    let mut url = sorug("http://[2001:db8::1]:8080/x");
    url.path_segments_mut().unwrap().clear().push("y");
    assert_eq!(url.host(), Some("[2001:db8::1]"));
    assert_eq!(url.port_u16(), Some(8080));
    assert_eq!(url.as_str(), "http://[2001:db8::1]:8080/y");
}

#[test]
fn positive_repeated_mut_sessions() {
    let mut url = sorug("https://example.com/a?q=1#h");
    url.path_segments_mut().unwrap().push("b");
    assert_eq!(url.as_str(), "https://example.com/a/b?q=1#h");
    url.path_segments_mut().unwrap().pop_if_empty().push("c");
    assert_eq!(url.as_str(), "https://example.com/a/b/c?q=1#h");
    url.path_segments_mut().unwrap().clear().extend(["z"]);
    assert_eq!(url.as_str(), "https://example.com/z?q=1#h");
}

#[test]
fn positive_only_dots_extend_leaves_path() {
    assert_match("only dots", "https://example.com/keep", |s, v| {
        s.path_segments_mut().unwrap().extend([".", "..", "."]);
        v.path_segments_mut().unwrap().extend([".", "..", "."]);
    });
}

#[test]
fn negative_blob_opaque_inner_still_rejects() {
    // Outer blob URL is cannot-be-a-base even when the path looks like a URL.
    let mut url = Url::parse("blob:https://example.com/uuid").unwrap();
    assert!(url.cannot_be_a_base());
    assert!(url.path_segments_mut().is_err());
}

#[test]
fn positive_ftp_special_encoding() {
    assert_match("ftp slash percent", "ftp://example.com/dir", |s, v| {
        s.path_segments_mut().unwrap().push("a/b%c");
        v.path_segments_mut().unwrap().push("a/b%c");
    });
}