test_pmi_validation.html (6704B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1389418 5 --> 6 <head> 7 <meta charset="utf-8"> 8 <title>Test for PaymentRequest API payment method identifier validation</title> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 10 <script src="/tests/SimpleTest/SimpleTest.js"></script> 11 <script type="application/javascript"> 12 13 "use strict"; 14 SimpleTest.waitForExplicitFinish(); 15 16 var gUrl = SimpleTest.getTestFileURL('PMIValidationChromeScript.js'); 17 var gScript = SpecialPowers.loadChromeScript(gUrl); 18 19 function testFailHandler(message) { 20 ok(false, message); 21 } 22 gScript.addMessageListener("test-fail", testFailHandler); 23 24 const defaultMethods = [{ 25 supportedMethods: "basic-card", 26 }]; 27 28 const defaultDetails = { 29 total: { 30 label: "total", 31 amount: { 32 currency: "usd", 33 value: "1.00", 34 }, 35 }, 36 }; 37 38 const validPMIs = [ 39 "https://wpt", 40 "https://wpt.fyi/", 41 "https://wpt.fyi/payment", 42 "https://wpt.fyi/payment-request", 43 "https://wpt.fyi/payment-request?", 44 "https://wpt.fyi/payment-request?this=is", 45 "https://wpt.fyi/payment-request?this=is&totally", 46 "https://wpt.fyi:443/payment-request?this=is&totally", 47 "https://wpt.fyi:443/payment-request?this=is&totally#fine", 48 "https://:@wpt.fyi:443/payment-request?this=is&totally#👍", 49 " \thttps://wpt\n ", 50 "https://xn--c1yn36f", 51 "https://點看", 52 "e", 53 "n6jzof05mk2g4lhxr-u-q-w1-c-i-pa-ty-bdvs9-ho-ae7-p-md8-s-wq3-h-qd-e-q-sa", 54 "a-b-q-n-s-pw0", 55 "m-u", 56 "s-l5", 57 "k9-f", 58 "m-l", 59 "u4-n-t", 60 "i488jh6-g18-fck-yb-v7-i", 61 "x-x-t-t-c34-o", 62 "basic-card", 63 ]; 64 65 const invalidPMIs = [ 66 "https://:password@example.com", 67 "https://username@example.com", 68 "https://username:password@example.com/pay", 69 "http://username:password@example.com/pay", 70 "https://:@example.com:100000000/pay", 71 "https://foo.com:100000000/pay", 72 "basic-💳", 73 "not-https://wpt.fyi/payment-request", 74 "../realitive/url", 75 "/absolute/../path?", 76 "https://", 77 "¡basic-*-card!", 78 "Basic-Card", 79 "0", 80 "-", 81 "--", 82 "a--b", 83 "-a--b", 84 "a-b-", 85 "0-", 86 "0-a", 87 "a0--", 88 "A-", 89 "A-B", 90 "A-b", 91 "a-0", 92 "a-0b", 93 " a-b", 94 "\t\na-b", 95 "a-b ", 96 "a-b\n\t", 97 ]; 98 99 function testWithValidPMIs() { 100 return new Promise((resolve, reject) => { 101 for (const validPMI of validPMIs) { 102 try { 103 const validMethods = [{supportedMethods: validPMI},]; 104 const payRequest = new PaymentRequest(validMethods, defaultDetails); 105 resolve(); 106 } catch (e) { 107 ok(false, "Unexpected error '" + e.name + "'."); 108 resolve(); 109 } 110 } 111 }); 112 } 113 114 function testWithInvalidPMIs() { 115 return new Promise((resolve, reject) => { 116 for (const invalidPMI of invalidPMIs) { 117 try { 118 const invalidMethods = [{supportedMethods: invalidPMI},]; 119 const payRequest = new PaymentRequest(invalidMethods, defaultDetails); 120 ok(false, "Expected throw 'RangeError', but got resolved"); 121 resolve(); 122 } catch (e) { 123 is(e.name, "RangeError", "Expected 'RangeError'."); 124 resolve(); 125 } 126 } 127 }); 128 } 129 130 function testUpdateWithValidPMI() { 131 const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true); 132 133 gScript.sendAsyncMessage("set-ui-service"); 134 return new Promise((resolve, reject) => { 135 const payRequest = new PaymentRequest(defaultMethods, defaultDetails); 136 payRequest.addEventListener("shippingoptionchange", event => { 137 const validDetails = { 138 total: { 139 label: "total", 140 amount: { 141 currency: "USD", 142 value: "1.00", 143 }, 144 }, 145 modifiers: [{ 146 supportedMethods: "https://example.com", 147 total: { 148 label: "total", 149 amount: { 150 currency: "USD", 151 value: "1.00", 152 }, 153 } 154 },], 155 } 156 event.updateWith(validDetails); 157 }); 158 payRequest.show().then((response) => { 159 response.complete("success").then(() => { 160 resolve(); 161 }).catch((e) => { 162 ok(false, "Unexpected error '" + e.name + "'."); 163 resolve(); 164 }); 165 }).catch((e) => { 166 ok(false, "Unexpected error '" + e.name + "'."); 167 resolve(); 168 }).finally(handler.destruct); 169 }); 170 } 171 172 function testUpdateWithInvalidPMI() { 173 const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true); 174 175 gScript.sendAsyncMessage("set-ui-service"); 176 return new Promise((resolve, reject) => { 177 const payRequest = new PaymentRequest(defaultMethods, defaultDetails); 178 payRequest.addEventListener("shippingoptionchange", event => { 179 const invalidDetails = { 180 total: { 181 label: "total", 182 amount: { 183 currency: "USD", 184 value: "1.00", 185 }, 186 }, 187 modifiers: [{ 188 supportedMethods: "https://username:password@example.com", 189 total: { 190 label: "total", 191 amount: { 192 currency: "USD", 193 value: "1.00", 194 }, 195 }, 196 },], 197 } 198 event.updateWith(invalidDetails); 199 }); 200 payRequest.show().then((result) => { 201 ok(false, "Expected throw 'RangeError', but got resolved."); 202 resolve(); 203 }).catch((e) => { 204 is(e.name, "RangeError", "Expected 'RangeError'."); 205 resolve(); 206 }).finally(handler.destruct); 207 }); 208 } 209 210 function teardown() { 211 gScript.addMessageListener("teardown-complete", function teardownCompleteHandler() { 212 gScript.removeMessageListener("teardown-complete", teardownCompleteHandler); 213 gScript.removeMessageListener("test-fail", testFailHandler) 214 gScript.destroy(); 215 SimpleTest.finish(); 216 }); 217 gScript.sendAsyncMessage("teardown"); 218 } 219 220 function runTests() { 221 testWithValidPMIs() 222 .then(testWithInvalidPMIs) 223 .then(testUpdateWithValidPMI) 224 .then(testUpdateWithInvalidPMI) 225 .then(teardown) 226 .catch( e => { 227 ok(false, "Unexpected error: " + e.name); 228 SimpleTest.finish(); 229 }); 230 } 231 232 window.addEventListener('load', function() { 233 SpecialPowers.pushPrefEnv({ 234 'set': [ 235 ['dom.payments.request.enabled', true], 236 ] 237 }, runTests); 238 }); 239 240 </script> 241 </head> 242 <body> 243 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1389418">Mozilla Bug 1389418</a> 244 </body> 245 </html>