test_accept_header.sjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. function handleRequest(request, response) {
  2. response.setStatusLine(request.httpVersion, "200", "OK");
  3. if (request.queryString == "worker") {
  4. response.setHeader("Content-Type", "application/json", false);
  5. response.write("postMessage(42)");
  6. setState("data", JSON.stringify({type: "worker", accept: request.getHeader("Accept") }));
  7. return;
  8. }
  9. if (request.queryString == "image") {
  10. // A 1x1 PNG image.
  11. // Source: https://commons.wikimedia.org/wiki/File:1x1.png (Public Domain)
  12. const IMAGE = atob("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAA" +
  13. "ACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=");
  14. response.setHeader("Content-Type", "image/png", false);
  15. response.write(IMAGE);
  16. setState("data", JSON.stringify({type: "image", accept: request.getHeader("Accept") }));
  17. return;
  18. }
  19. if (request.queryString == "style") {
  20. response.setHeader("Content-Type", "text/css", false);
  21. response.write("");
  22. setState("data", JSON.stringify({type: "style", accept: request.getHeader("Accept") }));
  23. return;
  24. }
  25. if (request.queryString == "iframe") {
  26. response.setHeader("Content-Type", "text/html", false);
  27. response.write("<h1>Hello world!</h1>");
  28. setState("data", JSON.stringify({type: "iframe", accept: request.getHeader("Accept") }));
  29. return;
  30. }
  31. if (request.queryString == "get") {
  32. response.setHeader("Content-Type", "text/javascript", false);
  33. response.write(getState("data"));
  34. setState("data", "");
  35. return;
  36. }
  37. }