summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/urlpattern/resources/urlpatterntests.js
blob: f774699bff99c3b78e342bf4a8da9207aa8c84dd (plain)
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
const kComponents = [
  'protocol',
  'username',
  'password',
  'hostname',
  'port',
  'password',
  'pathname',
  'search',
  'hash',
];

function runTests(data) {
  for (let entry of data) {
    test(function() {
      if (entry.expected_obj === 'error') {
        assert_throws_js(TypeError, _ => new URLPattern(...entry.pattern),
                         'URLPattern() constructor');
        return;
      }

      const pattern = new URLPattern(...entry.pattern);

      // If the expected_obj property is not present we will automatically
      // fill it with the most likely expected values.
      entry.expected_obj = entry.expected_obj || {};

      // The compiled URLPattern object should have a property for each
      // component exposing the compiled pattern string.
      for (let component of kComponents) {
        // If the test case explicitly provides an expected pattern string,
        // then use that.  This is necessary in cases where the original
        // construction pattern gets canonicalized, etc.
        let expected = entry.expected_obj[component];

        // If there is no explicit expected pattern string, then compute
        // the expected value based on the URLPattern constructor args.
        if (expected == undefined) {
          // First determine if there is a baseURL present in the pattern
          // input.  A baseURL can be the source for many component patterns.
          let baseURL = null;
          if (entry.pattern.length > 0 && entry.pattern[0].baseURL) {
            baseURL = new URL(entry.pattern[0].baseURL);
          } else if (entry.pattern.length > 1 &&
                     typeof entry.pattern[1] === 'string') {
            baseURL = new URL(entry.pattern[1]);
          }

          // We automatically populate the expected pattern string using
          // the following options in priority order:
          //
          //  1. If the original input explicitly provided a pattern, then
          //     echo that back as the expected value.
          //  2. If the baseURL exists and provides a component value then
          //     use that for the expected pattern.
          //  3. Otherwise fall back on the default pattern of `*` for an
          //     empty component pattern.
          if (entry.exactly_empty_components &&
              entry.exactly_empty_components.includes(component)) {
            expected = '';
          } else if (typeof entry.pattern[0] === 'object' &&
              entry.pattern[0][component]) {
            expected = entry.pattern[0][component];
          } else if (baseURL) {
            let base_value = baseURL[component];
            // Unfortunately some URL() getters include separator chars; e.g.
            // the trailing `:` for the protocol.  Strip those off if necessary.
            if (component === 'protocol')
              base_value = base_value.substring(0, base_value.length - 1);
            else if (component === 'search' || component === 'hash')
              base_value = base_value.substring(1, base_value.length);
            expected = base_value;
          } else {
            expected = '*';
          }
        }

        // Finally, assert that the compiled object property matches the
        // expected property.
        assert_equals(pattern[component], expected,
                      `compiled pattern property '${component}'`);
      }

      if (entry.expected_match === 'error') {
        assert_throws_js(TypeError, _ => pattern.test(...entry.inputs),
                         'test() result');
        assert_throws_js(TypeError, _ => pattern.exec(...entry.inputs),
                         'exec() result');
        return;
      }

      // First, validate the test() method by converting the expected result to
      // a truthy value.
      assert_equals(pattern.test(...entry.inputs), !!entry.expected_match,
                    'test() result');

      // Next, start validating the exec() method.
      const exec_result = pattern.exec(...entry.inputs);

      // On a failed match exec() returns null.
      if (!entry.expected_match || typeof entry.expected_match !== "object") {
        assert_equals(exec_result, entry.expected_match, 'exec() failed match result');
        return;
      }

      if (!entry.expected_match.inputs)
        entry.expected_match.inputs = entry.inputs;

      // Next verify the result.input is correct.  This may be a structured
      // URLPatternInit dictionary object or a URL string.
      assert_equals(exec_result.inputs.length,
                    entry.expected_match.inputs.length,
                    'exec() result.inputs.length');
      for (let i = 0; i < exec_result.inputs.length; ++i) {
        const input = exec_result.inputs[i];
        const expected_input = entry.expected_match.inputs[i];
        if (typeof input === 'string') {
          assert_equals(input, expected_input, `exec() result.inputs[${i}]`);
          continue;
        }
        for (let component of kComponents) {
          assert_equals(input[component], expected_input[component],
                        `exec() result.inputs[${i}][${component}]`);
        }
      }

      // Next we will compare the URLPatternComponentResult for each of these
      // expected components.
      for (let component of kComponents) {
        let expected_obj = entry.expected_match[component];

        // If the test expectations don't include a component object, then
        // we auto-generate one.  This is convenient for the many cases
        // where the pattern has a default wildcard or empty string pattern
        // for a component and the input is essentially empty.
        if (!expected_obj) {
          expected_obj = { input: '', groups: {} };

          // Next, we must treat default wildcards differently than empty string
          // patterns.  The wildcard results in a capture group, but the empty
          // string pattern does not.  The expectation object must list which
          // components should be empty instead of wildcards in
          // |exactly_empty_components|.
          if (!entry.exactly_empty_components ||
              !entry.exactly_empty_components.includes(component)) {
            expected_obj.groups['0'] = '';
          }
        }
        // JSON does not allow us to use undefined directly, so the data file
        // contains null instead.  Translate to the expected undefined value
        // here.
        for (const key in expected_obj.groups) {
          if (expected_obj.groups[key] === null) {
            expected_obj.groups[key] = undefined;
          }
        }
        assert_object_equals(exec_result[component], expected_obj,
                             `exec() result for ${component}`);
      }
    }, `Pattern: ${JSON.stringify(entry.pattern)} ` +
       `Inputs: ${JSON.stringify(entry.inputs)}`);
  }
}

promise_test(async function() {
  const response = await fetch('resources/urlpatterntestdata.json');
  const data = await response.json();
  runTests(data);
}, 'Loading data...');