test_sandbox_fetch.html 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Fetch in JS Sandbox</title>
  5. <script src="/tests/SimpleTest/SimpleTest.js"></script>
  6. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"></link>
  7. <script src="test_fetch_basic.js"></script>
  8. </head>
  9. <body>
  10. <script type="application/javascript">
  11. SimpleTest.waitForExplicitFinish();
  12. function testHttpFetch(url) {
  13. info('fetch: ' + url);
  14. return fetch(new Request(url, { method: 'GET' }))
  15. .then(response => {
  16. is(response.status, 200, 'Response is 200');
  17. is(response.url, url, 'Response URL matches');
  18. });
  19. }
  20. function runSandboxTest(testFunc, argString) {
  21. is(typeof testFunc, 'function');
  22. var resolvePromise;
  23. var testPromise = new Promise(r => resolvePromise = r);
  24. var finishFuncName = 'finish_' + testFunc.name;
  25. SpecialPowers.Cu.exportFunction(_ => resolvePromise(), sb,
  26. { defineAs: finishFuncName });
  27. SpecialPowers.Cu.evalInSandbox('(' + testFunc.toSource() + ')' +
  28. '(' + argString + ')' +
  29. '.then(' + finishFuncName + ');', sb);
  30. return testPromise;
  31. }
  32. var origin = 'https://example.com';
  33. var properties = ['fetch', 'Blob', 'URL'];
  34. var sb = new SpecialPowers.Cu.Sandbox(origin,
  35. { wantGlobalProperties: properties });
  36. sb.ok = SpecialPowers.Cu.exportFunction(ok, sb);
  37. sb.is = SpecialPowers.Cu.exportFunction(is, sb);
  38. sb.info = SpecialPowers.Cu.exportFunction(info, sb);
  39. Promise.resolve()
  40. .then(_ => runSandboxTest(testHttpFetch, '"' + origin + window.location.pathname + '"'))
  41. .then(_ => runSandboxTest(testAboutURL))
  42. .then(_ => runSandboxTest(testDataURL))
  43. .then(_ => runSandboxTest(testSameOriginBlobURL))
  44. .then(_ => SimpleTest.finish());
  45. </script>
  46. </body>
  47. </html>