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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
|
<!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", "shippingOptions", "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 shipping options:
const defaultShippingOption = Object.freeze({
id: "default",
label: "",
amount: defaultAmount,
selected: false,
});
const defaultShippingOptions = Object.freeze([
Object.assign({}, defaultShippingOption),
]);
test(() => {
smokeTest();
for (const amount of invalidAmounts) {
const invalidAmount = Object.assign({}, defaultAmount, {
value: amount,
});
const invalidShippingOption = Object.assign({}, defaultShippingOption, {
amount: invalidAmount,
});
const details = Object.assign({}, defaultDetails, {
shippingOptions: [invalidShippingOption],
});
assert_throws_js(
TypeError,
() => {
new PaymentRequest(defaultMethods, details, { requestShipping: true });
},
`Expected TypeError for option.amount.value: "${amount}"`
);
}
}, `For each option in details.shippingOptions: if option.amount.value is not a valid decimal monetary value, then throw a TypeError`);
test(() => {
smokeTest();
const shippingOptions = [defaultShippingOption];
const details = Object.assign({}, defaultDetails, { shippingOptions });
const request = new PaymentRequest(defaultMethods, details);
assert_equals(
request.shippingOption,
null,
"shippingOption must be null, as requestShipping is missing"
);
// defaultDetails lacks shipping options
const request2 = new PaymentRequest(defaultMethods, defaultDetails, {
requestShipping: true,
});
assert_equals(
request2.shippingOption,
null,
`request2.shippingOption must be null`
);
}, "If there is no selected shipping option, then PaymentRequest.shippingOption remains null");
test(() => {
smokeTest();
const selectedOption = Object.assign({}, defaultShippingOption, {
selected: true,
id: "the-id",
});
const shippingOptions = [selectedOption];
const details = Object.assign({}, defaultDetails, { shippingOptions });
const requestNoShippingRequested1 = new PaymentRequest(
defaultMethods,
details
);
assert_equals(
requestNoShippingRequested1.shippingOption,
null,
"Must be null when no shipping is requested (defaults to false)"
);
const requestNoShippingRequested2 = new PaymentRequest(
defaultMethods,
details,
{ requestShipping: false }
);
assert_equals(
requestNoShippingRequested2.shippingOption,
null,
"Must be null when requestShipping is false"
);
const requestWithShipping = new PaymentRequest(defaultMethods, details, {
requestShipping: "truthy value",
});
assert_equals(
requestWithShipping.shippingOption,
"the-id",
"Selected option must be 'the-id'"
);
}, "If there is a selected shipping option, and requestShipping is set, then that option becomes synchronously selected");
test(() => {
smokeTest();
const failOption1 = Object.assign({}, defaultShippingOption, {
selected: true,
id: "FAIL1",
});
const failOption2 = Object.assign({}, defaultShippingOption, {
selected: false,
id: "FAIL2",
});
const passOption = Object.assign({}, defaultShippingOption, {
selected: true,
id: "the-id",
});
const shippingOptions = [failOption1, failOption2, passOption];
const details = Object.assign({}, defaultDetails, { shippingOptions });
const requestNoShipping = new PaymentRequest(defaultMethods, details, {
requestShipping: false,
});
assert_equals(
requestNoShipping.shippingOption,
null,
"shippingOption must be null, as requestShipping is false"
);
const requestWithShipping = new PaymentRequest(defaultMethods, details, {
requestShipping: true,
});
assert_equals(
requestWithShipping.shippingOption,
"the-id",
"selected option must 'the-id"
);
}, "If requestShipping is set, and if there is a multiple selected shipping options, only the last is selected.");
test(() => {
smokeTest();
const selectedOption = Object.assign({}, defaultShippingOption, {
selected: true,
});
const unselectedOption = Object.assign({}, defaultShippingOption, {
selected: false,
});
const shippingOptions = [selectedOption, unselectedOption];
const details = Object.assign({}, defaultDetails, { shippingOptions });
const requestNoShipping = new PaymentRequest(defaultMethods, details);
assert_equals(
requestNoShipping.shippingOption,
null,
"shippingOption must be null, because requestShipping is false"
);
assert_throws_js(
TypeError,
() => {
new PaymentRequest(defaultMethods, details, { requestShipping: true });
},
"Expected to throw a TypeError because duplicate IDs"
);
}, "If there are any duplicate shipping option ids, and shipping is requested, then throw a TypeError");
test(() => {
smokeTest();
const dupShipping1 = Object.assign({}, defaultShippingOption, {
selected: true,
id: "DUPLICATE",
label: "Fail 1",
});
const dupShipping2 = Object.assign({}, defaultShippingOption, {
selected: false,
id: "DUPLICATE",
label: "Fail 2",
});
const shippingOptions = [dupShipping1, defaultShippingOption, dupShipping2];
const details = Object.assign({}, defaultDetails, { shippingOptions });
const requestNoShipping = new PaymentRequest(defaultMethods, details);
assert_equals(
requestNoShipping.shippingOption,
null,
"shippingOption must be null, because requestShipping is false"
);
assert_throws_js(
TypeError,
() => {
new PaymentRequest(defaultMethods, details, { requestShipping: true });
},
"Expected to throw a TypeError because duplicate IDs"
);
}, "Throw when there are duplicate shippingOption ids, even if other values are different");
// 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");
//Setting ShippingType attribute during construction
test(() => {
smokeTest();
assert_throws_js(TypeError, () => {
new PaymentRequest(defaultMethods, defaultDetails, {
shippingType: "invalid",
});
});
}, "Shipping type should be valid");
test(() => {
smokeTest();
const request = new PaymentRequest(defaultMethods, defaultDetails, {});
assert_equals(request.shippingAddress, null, "must be null");
}, "PaymentRequest.shippingAddress must initially be null");
test(() => {
smokeTest();
const request1 = new PaymentRequest(defaultMethods, defaultDetails, {});
assert_equals(request1.shippingType, null, "must be null");
const request2 = new PaymentRequest(defaultMethods, defaultDetails, {
requestShipping: false,
});
assert_equals(request2.shippingType, null, "must be null");
}, "If options.requestShipping is not set, then request.shippingType attribute is null.");
test(() => {
smokeTest();
// option.shippingType defaults to 'shipping'
const request1 = new PaymentRequest(defaultMethods, defaultDetails, {
requestShipping: true,
});
assert_equals(request1.shippingType, "shipping", "must be shipping");
const request2 = new PaymentRequest(defaultMethods, defaultDetails, {
requestShipping: true,
shippingType: "delivery",
});
assert_equals(request2.shippingType, "delivery", "must be delivery");
}, "If options.requestShipping is true, request.shippingType will be options.shippingType.");
</script>
|