summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/payment-request/payment-request-constructor.https.sub.html
blob: c1ecc225838b3b663c40cef417ba081d9a2805ef (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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<!DOCTYPE html>
<!-- Copyright © 2017 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<meta charset="utf-8">
<title>Test for PaymentRequest Constructor</title>
<link rel="help" href="https://w3c.github.io/browser-payment-api/#constructor">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
"use strict";
const testMethod = Object.freeze({
  supportedMethods: "https://{{domains[nonexistent]}}/payment-request",
});
const defaultMethods = Object.freeze([testMethod]);
const defaultAmount = Object.freeze({
  currency: "USD",
  value: "1.0",
});
const defaultNumberAmount = Object.freeze({
  currency: "USD",
  value: 1.0,
});
const defaultTotal = Object.freeze({
  label: "Default Total",
  amount: defaultAmount,
});
const defaultNumberTotal = Object.freeze({
  label: "Default Number Total",
  amount: defaultNumberAmount,
});
const defaultDetails = Object.freeze({
  total: defaultTotal,
  displayItems: [
    {
      label: "Default Display Item",
      amount: defaultAmount,
    },
  ],
});
const defaultNumberDetails = Object.freeze({
  total: defaultNumberTotal,
  displayItems: [
    {
      label: "Default Display Item",
      amount: defaultNumberAmount,
    },
  ],
});

// Avoid false positives, this should always pass
function smokeTest() {
  new PaymentRequest(defaultMethods, defaultDetails);
  new PaymentRequest(defaultMethods, defaultNumberDetails);
}
test(() => {
  smokeTest();
  const request = new PaymentRequest(defaultMethods, defaultDetails);
  assert_true(Boolean(request.id), "must be some truthy value");
}, "If details.id is missing, assign an identifier");

test(() => {
  smokeTest();
  const request1 = new PaymentRequest(defaultMethods, defaultDetails);
  const request2 = new PaymentRequest(defaultMethods, defaultDetails);
  assert_not_equals(request1.id, request2.id, "UA generated ID must be unique");
  const seen = new Set();
  // Let's try creating lots of requests, and make sure they are all unique
  for (let i = 0; i < 1024; i++) {
    const request = new PaymentRequest(defaultMethods, defaultDetails);
    assert_false(
      seen.has(request.id),
      `UA generated ID must be unique, but got duplicate! (${request.id})`
    );
    seen.add(request.id);
  }
}, "If details.id is missing, assign a unique identifier");

test(() => {
  smokeTest();
  const newDetails = Object.assign({}, defaultDetails, { id: "test123" });
  const request1 = new PaymentRequest(defaultMethods, newDetails);
  const request2 = new PaymentRequest(defaultMethods, newDetails);
  assert_equals(request1.id, newDetails.id, `id must be ${newDetails.id}`);
  assert_equals(request2.id, newDetails.id, `id must be ${newDetails.id}`);
  assert_equals(request1.id, request2.id, "ids need to be the same");
}, "If the same id is provided, then use it");

test(() => {
  smokeTest();
  const newDetails = Object.assign({}, defaultDetails, {
    id: "".padStart(1024, "a"),
  });
  const request = new PaymentRequest(defaultMethods, newDetails);
  assert_equals(
    request.id,
    newDetails.id,
    `id must be provided value, even if very long and contain spaces`
  );
}, "Use ids even if they are strange");

test(() => {
  smokeTest();
  const request = new PaymentRequest(
    defaultMethods,
    Object.assign({}, defaultDetails, { id: "foo" })
  );
  assert_equals(request.id, "foo");
}, "Use provided request ID");

test(() => {
  smokeTest();
  assert_throws_js(TypeError, () => new PaymentRequest([], defaultDetails));
}, "If the length of the methodData sequence is zero, then throw a TypeError");

test(() => {
  smokeTest();
  const duplicateMethods = [
    {
      supportedMethods: "https://{{domains[nonexistent]}}/payment-request",
    },
    {
      supportedMethods: "https://{{domains[nonexistent]}}/payment-request",
    },
  ];
  assert_throws_js(RangeError, () => new PaymentRequest(duplicateMethods, defaultDetails));
}, "If payment method is duplicate, then throw a RangeError");

test(() => {
  smokeTest();
  const JSONSerializables = [[], { object: {} }];
  for (const data of JSONSerializables) {
    try {
      const methods = [
        {
          supportedMethods: "https://{{domains[nonexistent]}}/payment-request",
          data,
        },
      ];
      new PaymentRequest(methods, defaultDetails);
    } catch (err) {
      assert_unreached(
        `Unexpected error parsing stringifiable JSON: ${JSON.stringify(
          data
        )}: ${err.message}`
      );
    }
  }
}, "Modifier method data must be JSON-serializable object");

test(() => {
  smokeTest();
  const recursiveDictionary = {};
  recursiveDictionary.foo = recursiveDictionary;
  assert_throws_js(TypeError, () => {
    const methods = [
      {
        supportedMethods: "https://{{domains[nonexistent]}}/payment-request",
        data: recursiveDictionary,
      },
    ];
    new PaymentRequest(methods, defaultDetails);
  });
  assert_throws_js(TypeError, () => {
    const methods = [
      {
        supportedMethods: "https://{{domains[nonexistent]}}/payment-request",
        data: "a string",
      },
    ];
    new PaymentRequest(methods, defaultDetails);
  });
  assert_throws_js(
    TypeError,
    () => {
      const methods = [
        {
          supportedMethods: "https://{{domains[nonexistent]}}/payment-request",
          data: null,
        },
      ];
      new PaymentRequest(methods, defaultDetails);
    },
    "Even though null is JSON-serializable, it's not type 'Object' per ES spec"
  );
}, "Rethrow any exceptions of JSON-serializing paymentMethod.data into a string");

// process total
const invalidAmounts = [
  "-",
  "notdigits",
  "ALSONOTDIGITS",
  "10.",
  ".99",
  "-10.",
  "-.99",
  "10-",
  "1-0",
  "1.0.0",
  "1/3",
  "",
  null,
  " 1.0  ",
  " 1.0 ",
  "1.0 ",
  "USD$1.0",
  "$1.0",
  {
    toString() {
      return " 1.0";
    },
  },
];
const invalidTotalAmounts = invalidAmounts.concat([
  "-1",
  "-1.0",
  "-1.00",
  "-1000.000",
  -10,
]);
test(() => {
  smokeTest();
  for (const invalidAmount of invalidTotalAmounts) {
    const invalidDetails = {
      total: {
        label: "",
        amount: {
          currency: "USD",
          value: invalidAmount,
        },
      },
    };
    assert_throws_js(
      TypeError,
      () => {
        new PaymentRequest(defaultMethods, invalidDetails);
      },
      `Expect TypeError when details.total.amount.value is ${invalidAmount}`
    );
  }
}, `If details.total.amount.value is not a valid decimal monetary value, then throw a TypeError`);

test(() => {
  smokeTest();
  for (const prop in ["displayItems", "modifiers"]) {
    try {
      const details = Object.assign({}, defaultDetails, { [prop]: [] });
      new PaymentRequest(defaultMethods, details);
      assert_unreached(`PaymentDetailsBase.${prop} can be zero length`);
    } catch (err) {}
  }
}, `PaymentDetailsBase members can be 0 length`);

test(() => {
  smokeTest();
  assert_throws_js(TypeError, () => {
    new PaymentRequest(defaultMethods, {
      total: {
        label: "",
        amount: {
          currency: "USD",
          value: "-1.00",
        },
      },
    });
  });
}, "If the first character of details.total.amount.value is U+002D HYPHEN-MINUS, then throw a TypeError");

test(() => {
  smokeTest();
  for (const invalidAmount of invalidAmounts) {
    const invalidDetails = {
      total: defaultAmount,
      displayItems: [
        {
          label: "",
          amount: {
            currency: "USD",
            value: invalidAmount,
          },
        },
      ],
    };
    assert_throws_js(
      TypeError,
      () => {
        new PaymentRequest(defaultMethods, invalidDetails);
      },
      `Expected TypeError when item.amount.value is "${invalidAmount}"`
    );
  }
}, `For each item in details.displayItems: if item.amount.value is not a valid decimal monetary value, then throw a TypeError`);

test(() => {
  smokeTest();
  try {
    new PaymentRequest(
      [
        {
          supportedMethods: "https://{{domains[nonexistent]}}/payment-request",
        },
      ],
      {
        total: defaultTotal,
        displayItems: [
          {
            label: "",
            amount: {
              currency: "USD",
              value: "-1000",
            },
          },
          {
            label: "",
            amount: {
              currency: "AUD",
              value: "-2000.00",
            },
          },
        ],
      }
    );
  } catch (err) {
    assert_unreached(
      `shouldn't throw when given a negative value: ${err.message}`
    );
  }
}, "Negative values are allowed for displayItems.amount.value, irrespective of total amount");

test(() => {
  smokeTest();
  const largeMoney = "1".repeat(510);
  try {
    new PaymentRequest(defaultMethods, {
      total: {
        label: "",
        amount: {
          currency: "USD",
          value: `${largeMoney}.${largeMoney}`,
        },
      },
      displayItems: [
        {
          label: "",
          amount: {
            currency: "USD",
            value: `-${largeMoney}`,
          },
        },
        {
          label: "",
          amount: {
            currency: "AUD",
            value: `-${largeMoney}.${largeMoney}`,
          },
        },
      ],
    });
  } catch (err) {
    assert_unreached(
      `shouldn't throw when given absurd monetary values: ${err.message}`
    );
  }
}, "it handles high precision currency values without throwing");

// Process payment details modifiers:
test(() => {
  smokeTest();
  for (const invalidTotal of invalidTotalAmounts) {
    const invalidModifier = {
      supportedMethods: "https://{{domains[nonexistent]}}/payment-request",
      total: {
        label: "",
        amount: {
          currency: "USD",
          value: invalidTotal,
        },
      },
    };
    assert_throws_js(
      TypeError,
      () => {
        new PaymentRequest(defaultMethods, {
          modifiers: [invalidModifier],
          total: defaultTotal,
        });
      },
      `Expected TypeError for modifier.total.amount.value: "${invalidTotal}"`
    );
  }
}, `Throw TypeError if modifier.total.amount.value is not a valid decimal monetary value`);

test(() => {
  smokeTest();
  for (const invalidAmount of invalidAmounts) {
    const invalidModifier = {
      supportedMethods: "https://{{domains[nonexistent]}}/payment-request",
      total: defaultTotal,
      additionalDisplayItems: [
        {
          label: "",
          amount: {
            currency: "USD",
            value: invalidAmount,
          },
        },
      ],
    };
    assert_throws_js(
      TypeError,
      () => {
        new PaymentRequest(defaultMethods, {
          modifiers: [invalidModifier],
          total: defaultTotal,
        });
      },
      `Expected TypeError when given bogus modifier.additionalDisplayItems.amount of "${invalidModifier}"`
    );
  }
}, `If amount.value of additionalDisplayItems is not a valid decimal monetary value, then throw a TypeError`);

test(() => {
  smokeTest();
  const modifiedDetails = Object.assign({}, defaultDetails, {
    modifiers: [
      {
        supportedMethods: "https://{{domains[nonexistent]}}/payment-request",
        data: ["some-data"],
      },
    ],
  });
  try {
    new PaymentRequest(defaultMethods, modifiedDetails);
  } catch (err) {
    assert_unreached(
      `Unexpected exception thrown when given a list: ${err.message}`
    );
  }
}, "Modifier data must be JSON-serializable object (an Array in this case)");

test(() => {
  smokeTest();
  const modifiedDetails = Object.assign({}, defaultDetails, {
    modifiers: [
      {
        supportedMethods: "https://{{domains[nonexistent]}}/payment-request",
        data: {
          some: "data",
        },
      },
    ],
  });
  try {
    new PaymentRequest(defaultMethods, modifiedDetails);
  } catch (err) {
    assert_unreached(
      `shouldn't throw when given an object value: ${err.message}`
    );
  }
}, "Modifier data must be JSON-serializable object (an Object in this case)");

test(() => {
  smokeTest();
  const recursiveDictionary = {};
  recursiveDictionary.foo = recursiveDictionary;
  const modifiedDetails = Object.assign({}, defaultDetails, {
    modifiers: [
      {
        supportedMethods: "https://{{domains[nonexistent]}}/payment-request",
        data: recursiveDictionary,
      },
    ],
  });
  assert_throws_js(TypeError, () => {
    new PaymentRequest(defaultMethods, modifiedDetails);
  });
}, "Rethrow any exceptions of JSON-serializing modifier.data");

</script>