http_path_template 0.1.0

Parser for the google.api.http path-template grammar
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Integration tests for the public `http_path_template` parsing API.

use http_path_template::{Grammar, ParseError, PathTemplate, Segment};

/// A [`ParseError`] category predicate (e.g. [`ParseError::is_empty_verb`]), used
/// by the table-driven parse-failure tests.
type ParseErrorPredicate = fn(&ParseError) -> bool;

/// Returns the field path and sub-template segments of the [`Segment::Variable`]
/// a template is expected to contain at `index`, or `None` if that segment is
/// not a variable.
fn variable_at<'a>(template: &PathTemplate<'a>, index: usize) -> Option<(Vec<&'a str>, Vec<Segment<'a>>)> {
    match template.segments()[index] {
        Segment::Variable(variable) => Some((variable.field_path().split('.').collect(), variable.segments().collect())),
        _ => None,
    }
}

#[test]
fn parses_simple_literals() {
    let t = PathTemplate::parse("/v1/shelves", Grammar::default()).expect("valid");
    assert_eq!(t.segments(), &[Segment::Literal("v1"), Segment::Literal("shelves")]);
    assert!(t.verb().is_none());
}

#[test]
fn parses_root() {
    let t = PathTemplate::parse("/", Grammar::default()).expect("root is valid");
    assert!(t.segments().is_empty());
    assert!(t.verb().is_none());
}

#[test]
fn parses_single_wildcard_segment() {
    let t = PathTemplate::parse("/v1/*", Grammar::default()).expect("valid");
    assert_eq!(t.segments(), &[Segment::Literal("v1"), Segment::Single]);
}

#[test]
fn parses_variable_shorthand_to_single() {
    let t = PathTemplate::parse("/v1/shelves/{shelf}", Grammar::default()).expect("valid");
    assert_eq!(t.segments()[0], Segment::Literal("v1"));
    assert_eq!(t.segments()[1], Segment::Literal("shelves"));
    let (field_path, segments) = variable_at(&t, 2).expect("segment is a variable");
    assert_eq!(field_path, ["shelf"]);
    assert_eq!(segments, [Segment::Single]);
}

#[test]
fn parses_nested_field_path() {
    let t = PathTemplate::parse("/v1/{shelf.id}", Grammar::default()).expect("valid");
    assert_eq!(t.segments()[0], Segment::Literal("v1"));
    let (field_path, segments) = variable_at(&t, 1).expect("segment is a variable");
    assert_eq!(field_path, ["shelf", "id"]);
    assert_eq!(segments, [Segment::Single]);
}

#[test]
fn parses_variable_subtemplate_and_rest() {
    let t = PathTemplate::parse("/v1/{name=books/**}", Grammar::default()).expect("valid");
    assert_eq!(t.segments()[0], Segment::Literal("v1"));
    let (field_path, segments) = variable_at(&t, 1).expect("segment is a variable");
    assert_eq!(field_path, ["name"]);
    assert_eq!(segments, [Segment::Literal("books"), Segment::Rest]);
}

#[test]
fn parses_trailing_rest() {
    let t = PathTemplate::parse("/v1/files/**", Grammar::default()).expect("valid");
    assert_eq!(t.segments().last(), Some(&Segment::Rest));
}

#[test]
fn variable_accessors_return_field_and_subtemplate() {
    let t = PathTemplate::parse("/v1/{shelf.id=books/*}", Grammar::default()).expect("valid");
    let (field_path, segments) = variable_at(&t, 1).expect("segment is a variable");
    assert_eq!(field_path, ["shelf", "id"]);
    assert_eq!(segments, [Segment::Literal("books"), Segment::Single]);
}

#[test]
fn parses_custom_verb() {
    let t = PathTemplate::parse("/v1/{name=shelves/*}:read", Grammar::default()).expect("valid");
    assert_eq!(t.verb(), Some("read"));
}

#[test]
fn requires_leading_slash() {
    assert!(
        PathTemplate::parse("v1/x", Grammar::default())
            .unwrap_err()
            .is_missing_leading_slash()
    );
}

#[test]
fn rejects_empty_segment() {
    assert!(PathTemplate::parse("/a//b", Grammar::default()).unwrap_err().is_empty_segment());
}

#[test]
fn rejects_rest_not_last() {
    assert!(PathTemplate::parse("/a/**/b", Grammar::default()).unwrap_err().is_rest_not_last());
}

#[test]
fn rejects_rest_not_last_inside_variable() {
    assert!(
        PathTemplate::parse("/a/{x=**}/b", Grammar::default())
            .unwrap_err()
            .is_rest_not_last()
    );
}

#[test]
fn rejects_nested_variable() {
    assert!(
        PathTemplate::parse("/a/{x={y}}", Grammar::default())
            .unwrap_err()
            .is_nested_variable()
    );
}

#[test]
fn rejects_invalid_field_name() {
    assert!(
        PathTemplate::parse("/a/{1bad}", Grammar::default())
            .unwrap_err()
            .is_invalid_field_name()
    );
}

#[test]
fn rejects_empty_verb() {
    assert!(PathTemplate::parse("/a/b:", Grammar::default()).unwrap_err().is_empty_verb());
}

#[test]
fn rejects_multiple_verbs() {
    assert!(PathTemplate::parse("/a:b:c", Grammar::default()).unwrap_err().is_multiple_verbs());
}

#[test]
fn rejects_verb_containing_slash() {
    assert!(PathTemplate::parse("/v1/a:b/c", Grammar::default()).unwrap_err().is_invalid_verb());
}

#[test]
fn rejects_verb_with_non_literal_characters() {
    // A verb is a LITERAL, so wildcard/brace characters are rejected. Braces are
    // especially important: a stray `{` must not unbalance the colon-depth scan.
    assert!(PathTemplate::parse("/a:bad*", Grammar::default()).unwrap_err().is_invalid_verb());
    assert!(PathTemplate::parse("/a:bad}", Grammar::default()).unwrap_err().is_invalid_verb());
    assert!(
        PathTemplate::parse("/a:bad{:still_bad", Grammar::default())
            .unwrap_err()
            .is_invalid_verb()
    );
}

#[test]
fn rejects_unbalanced_braces() {
    assert!(PathTemplate::parse("/a/{x", Grammar::default()).unwrap_err().is_unbalanced_braces());
}

#[test]
fn parse_error_display_covers_every_kind() {
    let cases: [(&str, ParseErrorPredicate, &str); 11] = [
        (
            "no-leading-slash",
            ParseError::is_missing_leading_slash,
            "template must begin with '/'",
        ),
        ("/a//b", ParseError::is_empty_segment, "template contains an empty path segment"),
        ("/a/{x", ParseError::is_unbalanced_braces, "template contains unbalanced '{' or '}'"),
        (
            "/a/{x={y}}",
            ParseError::is_nested_variable,
            "variable sub-templates may not contain nested variables",
        ),
        ("/a/{=x}", ParseError::is_empty_field_path, "variable has an empty field path"),
        (
            "/a/{1bad}",
            ParseError::is_invalid_field_name,
            "variable field path contains an invalid identifier",
        ),
        ("/a*b", ParseError::is_invalid_literal, "literal segment contains a misplaced '*'"),
        ("/a/**/b", ParseError::is_rest_not_last, "'**' may only appear as the final segment"),
        ("/a/b:", ParseError::is_empty_verb, "custom verb after ':' is empty"),
        (
            "/v1/a:b/c",
            ParseError::is_invalid_verb,
            "custom verb after ':' must be a literal (no '/', '*', '{', or '}')",
        ),
        (
            "/a:b:c",
            ParseError::is_multiple_verbs,
            "template contains more than one ':' verb separator",
        ),
    ];
    for (template, predicate, message) in cases {
        let err = PathTemplate::parse(template, Grammar::default()).unwrap_err();
        assert!(predicate(&err), "kind for {template:?}");
        assert_eq!(err.to_string(), message, "message for {template:?}");
    }
}

#[test]
fn each_kind_predicate_matches_only_its_own_kind() {
    // One representative template per error kind, paired with the predicate that
    // must recognise it.
    let cases: [(&str, ParseErrorPredicate); 11] = [
        ("no-leading-slash", ParseError::is_missing_leading_slash),
        ("/a//b", ParseError::is_empty_segment),
        ("/a/{x", ParseError::is_unbalanced_braces),
        ("/a/{x={y}}", ParseError::is_nested_variable),
        ("/a/{=x}", ParseError::is_empty_field_path),
        ("/a/{1bad}", ParseError::is_invalid_field_name),
        ("/a*b", ParseError::is_invalid_literal),
        ("/a/**/b", ParseError::is_rest_not_last),
        ("/a/b:", ParseError::is_empty_verb),
        ("/v1/a:b/c", ParseError::is_invalid_verb),
        ("/a:b:c", ParseError::is_multiple_verbs),
    ];
    let all_predicates: [ParseErrorPredicate; 11] = cases.map(|(_, predicate)| predicate);

    for (template, predicate) in cases {
        let err = PathTemplate::parse(template, Grammar::default()).unwrap_err();
        // The kind's own predicate holds, and no other predicate does — so each
        // predicate is exclusive to its kind (a predicate hard-wired to `true`
        // would make some other kind's error match two predicates).
        assert!(predicate(&err), "own predicate for {template:?}");
        let matches = all_predicates.iter().filter(|check| check(&err)).count();
        assert_eq!(matches, 1, "exactly one predicate matches {template:?}");
    }
}

#[test]
fn colon_inside_braces_is_not_a_verb() {
    // The `:` sits inside the variable's braces, so it must not be read as a
    // trailing custom verb.
    let t = PathTemplate::parse("/a/{b=c:d}", Grammar::default()).expect("valid");
    assert!(t.verb().is_none());
    let (field_path, segments) = variable_at(&t, 1).expect("segment is a variable");
    assert_eq!(field_path, ["b"]);
    assert_eq!(segments, [Segment::Literal("c:d")]);
}

#[test]
fn accepts_underscore_led_field_name() {
    // Covers `_` as both the leading and a subsequent identifier character.
    let t = PathTemplate::parse("/v1/{_my_id}", Grammar::default()).expect("underscore field name is valid");
    let (field_path, _) = variable_at(&t, 1).expect("segment is a variable");
    assert_eq!(field_path, ["_my_id"]);
}

#[test]
fn rejects_stray_brace_in_literal() {
    assert!(
        PathTemplate::parse("/x/a{b}", Grammar::default())
            .unwrap_err()
            .is_unbalanced_braces()
    );
}

#[test]
fn rejects_trailing_chars_after_variable_close() {
    assert!(
        PathTemplate::parse("/v1/{a}b", Grammar::default())
            .unwrap_err()
            .is_unbalanced_braces()
    );
}

#[test]
fn rejects_unbalanced_closing_brace() {
    assert!(PathTemplate::parse("/a/}", Grammar::default()).unwrap_err().is_unbalanced_braces());
}

#[test]
fn rejects_empty_subtemplate() {
    assert!(PathTemplate::parse("/a/{x=}", Grammar::default()).unwrap_err().is_empty_segment());
}

#[test]
fn rejects_empty_subtemplate_segment() {
    assert!(
        PathTemplate::parse("/a/{x=a//b}", Grammar::default())
            .unwrap_err()
            .is_empty_segment()
    );
}

#[test]
fn rejects_wildcard_in_subtemplate_literal() {
    assert!(
        PathTemplate::parse("/a/{x=a*b}", Grammar::default())
            .unwrap_err()
            .is_invalid_literal()
    );
}

#[test]
fn display_renders_canonical_template() {
    let cases = [
        // Root template with no segments and no verb.
        ("/", "/"),
        // Root template carrying only a custom verb.
        ("/:ping", "/:ping"),
        // Literals, `*`, and a trailing `**`.
        ("/v1/shelves/*/**", "/v1/shelves/*/**"),
        // Variable shorthand stays `{field}`, dotted field paths are preserved.
        ("/v1/shelves/{shelf.id}", "/v1/shelves/{shelf.id}"),
        // Explicit `{field=*}` collapses to the `{field}` shorthand.
        ("/v1/{name=*}", "/v1/{name}"),
        // Variable sub-templates with literals, `*`, and `**`.
        ("/v1/{name=books/*}", "/v1/{name=books/*}"),
        ("/v1/{name=books/**}", "/v1/{name=books/**}"),
        // A `:` inside braces is part of a literal, not a verb.
        ("/a/{b=c:d}", "/a/{b=c:d}"),
        // Full template combining variables, a `**` sub-template, and a verb.
        (
            "/shelves/{shelf}/books/{book=**}:archive",
            "/shelves/{shelf}/books/{book=**}:archive",
        ),
    ];
    for (template, expected) in cases {
        let rendered = PathTemplate::parse(template, Grammar::default()).expect("valid").to_string();
        assert_eq!(rendered, expected, "display for {template:?}");
    }
}

#[test]
fn display_round_trips_through_parse() {
    let templates = [
        "/",
        "/:ping",
        "/v1/shelves/*/**",
        "/v1/shelves/{shelf.id}",
        "/v1/{name=*}",
        "/v1/{name=books/*}",
        "/v1/{name=books/**}",
        "/a/{b=c:d}",
        "/shelves/{shelf}/books/{book=**}:archive",
    ];
    for template in templates {
        let parsed = PathTemplate::parse(template, Grammar::default()).expect("valid");
        let rendered = parsed.to_string();
        let reparsed = PathTemplate::parse(&rendered, Grammar::default()).expect("rendered template is valid");
        assert_eq!(parsed, reparsed, "round trip for {template:?}");
    }
}

#[test]
fn extended_parses_suffix_prefix_and_both() {
    // Suffix only.
    assert_eq!(
        PathTemplate::parse("/files/{name}.json", Grammar::default().with_segment_affixes())
            .expect("valid")
            .segments()[1],
        Segment::Affix {
            prefix: "",
            name: "name",
            suffix: ".json",
        }
    );
    // Prefix only.
    assert_eq!(
        PathTemplate::parse("/v{version}/x", Grammar::default().with_segment_affixes())
            .expect("valid")
            .segments()[0],
        Segment::Affix {
            prefix: "v",
            name: "version",
            suffix: "",
        }
    );
    // Prefix and suffix, dotted field name.
    assert_eq!(
        PathTemplate::parse("/img-{img.id}.png", Grammar::default().with_segment_affixes())
            .expect("valid")
            .segments()[0],
        Segment::Affix {
            prefix: "img-",
            name: "img.id",
            suffix: ".png",
        }
    );
}

#[test]
fn strict_rejects_extended_syntax() {
    PathTemplate::parse("/files/{name}.json", Grammar::default()).expect_err("rejected");
    PathTemplate::parse("/v{version}/x", Grammar::default()).expect_err("rejected");
    PathTemplate::parse("/img-{id}.png", Grammar::default()).expect_err("rejected");
}

#[test]
fn extended_still_accepts_strict_templates() {
    let strict = "/v1/shelves/{shelf}/books/{book=**}:archive";
    assert_eq!(
        PathTemplate::parse(strict, Grammar::default()).expect("valid"),
        PathTemplate::parse(strict, Grammar::default().with_segment_affixes()).expect("valid"),
    );
}

#[test]
fn extended_rejects_two_params_in_one_segment() {
    PathTemplate::parse("/a{x}b{y}c", Grammar::default().with_segment_affixes()).expect_err("rejected");
}

#[test]
fn extended_rejects_wildcard_in_affix() {
    PathTemplate::parse("/a*{x}", Grammar::default().with_segment_affixes()).expect_err("rejected");
    PathTemplate::parse("/{x}*b", Grammar::default().with_segment_affixes()).expect_err("rejected");
}

#[test]
fn extended_rejects_empty_or_invalid_affix_field() {
    PathTemplate::parse("/a{}b", Grammar::default().with_segment_affixes()).expect_err("rejected");
    PathTemplate::parse("/a{1bad}b", Grammar::default().with_segment_affixes()).expect_err("rejected");
}

#[test]
fn extended_affix_round_trips_through_display() {
    for template in ["/files/{name}.json", "/v{version}/x", "/img-{img.id}.png"] {
        let parsed = PathTemplate::parse(template, Grammar::default().with_segment_affixes()).expect("valid");
        assert_eq!(parsed.to_string(), template, "display for {template:?}");
        let rendered = parsed.to_string();
        let reparsed = PathTemplate::parse(&rendered, Grammar::default().with_segment_affixes()).expect("valid");
        assert_eq!(parsed, reparsed, "round trip for {template:?}");
    }
}

#[test]
fn extended_rejects_closing_brace_before_opening() {
    // A `}` appearing before the `{` (one of each) is malformed, not an affix.
    PathTemplate::parse("/a}b{c", Grammar::default().with_segment_affixes()).expect_err("rejected");
}