test-exceptions.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var sys = require("util")
  2. , assert = require("assert")
  3. , XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
  4. , xhr = new XMLHttpRequest();
  5. // Test request methods that aren't allowed
  6. try {
  7. xhr.open("TRACK", "http://localhost:8000/");
  8. console.log("ERROR: TRACK should have thrown exception");
  9. } catch(e) {}
  10. try {
  11. xhr.open("TRACE", "http://localhost:8000/");
  12. console.log("ERROR: TRACE should have thrown exception");
  13. } catch(e) {}
  14. try {
  15. xhr.open("CONNECT", "http://localhost:8000/");
  16. console.log("ERROR: CONNECT should have thrown exception");
  17. } catch(e) {}
  18. // Test valid request method
  19. try {
  20. xhr.open("GET", "http://localhost:8000/");
  21. } catch(e) {
  22. console.log("ERROR: Invalid exception for GET", e);
  23. }
  24. // Test forbidden headers
  25. var forbiddenRequestHeaders = [
  26. "accept-charset",
  27. "accept-encoding",
  28. "access-control-request-headers",
  29. "access-control-request-method",
  30. "connection",
  31. "content-length",
  32. "content-transfer-encoding",
  33. "cookie",
  34. "cookie2",
  35. "date",
  36. "expect",
  37. "host",
  38. "keep-alive",
  39. "origin",
  40. "referer",
  41. "te",
  42. "trailer",
  43. "transfer-encoding",
  44. "upgrade",
  45. "via"
  46. ];
  47. for (var i in forbiddenRequestHeaders) {
  48. if(xhr.setRequestHeader(forbiddenRequestHeaders[i], "Test") !== false) {
  49. console.log("ERROR: " + forbiddenRequestHeaders[i] + " should have thrown exception");
  50. }
  51. }
  52. // Try valid header
  53. xhr.setRequestHeader("X-Foobar", "Test");
  54. console.log("Done");