test_accept_header.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Accept header</title>
  5. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  6. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  7. </head>
  8. <body>
  9. <script>
  10. // All the requests are sent to test_accept_header.sjs which will return
  11. // different content based on the queryString. When the queryString is 'get',
  12. // test_accept_header.sjs returns a JSON object with the latest request and its
  13. // accept header value.
  14. function test_last_request_and_continue(query, expected) {
  15. fetch("test_accept_header.sjs?get").then(r => r.json()).then(json => {
  16. is(json.type, query, "Expected: " + query);
  17. is(json.accept, expected, "Accept header: " + expected);
  18. next();
  19. });
  20. }
  21. function test_iframe() {
  22. let observer = new PerformanceObserver(function(list, obj) {
  23. obj.disconnect();
  24. list.getEntries().forEach(entry => {
  25. if (entry.name.endsWith("test_accept_header.sjs?iframe")) {
  26. obj.disconnect();
  27. test_last_request_and_continue("iframe", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  28. }
  29. });
  30. });
  31. observer.observe({entryTypes: ["resource"]});
  32. let ifr = document.createElement("iframe");
  33. ifr.src = "test_accept_header.sjs?iframe";
  34. document.body.appendChild(ifr);
  35. }
  36. function test_image() {
  37. let i = new Image();
  38. i.src = "test_accept_header.sjs?image";
  39. i.onload = function() {
  40. // Fetch spec says we should have: "image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5"
  41. test_last_request_and_continue("image", "image/webp,image/png,image/*;q=0.8,*/*;q=0.5");
  42. }
  43. }
  44. function test_style() {
  45. let observer = new PerformanceObserver(function(list, obj) {
  46. obj.disconnect();
  47. list.getEntries().forEach(entry => {
  48. if (entry.name.endsWith("test_accept_header.sjs?style")) {
  49. obj.disconnect();
  50. test_last_request_and_continue("style", "text/css,*/*;q=0.1");
  51. }
  52. });
  53. });
  54. observer.observe({entryTypes: ["resource"]});
  55. let head = document.getElementsByTagName("head")[0];
  56. let link = document.createElement("link");
  57. link.rel = "stylesheet";
  58. link.type = "text/css";
  59. link.href = "test_accept_header.sjs?style";
  60. head.appendChild(link);
  61. }
  62. function test_worker() {
  63. let w = new Worker("test_accept_header.sjs?worker");
  64. w.onmessage = function() {
  65. test_last_request_and_continue("worker", "*/*");
  66. }
  67. }
  68. let tests = [
  69. test_iframe,
  70. test_image,
  71. test_style,
  72. test_worker,
  73. ];
  74. function next() {
  75. if (tests.length == 0) {
  76. SimpleTest.finish();
  77. return;
  78. }
  79. let test = tests.shift();
  80. test();
  81. }
  82. SimpleTest.waitForExplicitFinish();
  83. SpecialPowers.pushPrefEnv({ "set": [
  84. [ "dom.enable_performance_observer", true ]
  85. ]}, next);
  86. </script>
  87. </body>
  88. </html>