uniapp,h5

test-sync-response.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var assert = require("assert")
  2. , spawn = require('child_process').spawn
  3. , XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
  4. , serverProcess;
  5. // Running a sync XHR and a webserver within the same process will cause a deadlock
  6. serverProcess = spawn(process.argv[0], [__dirname + "/server.js"], { stdio: 'inherit' });
  7. setTimeout(function () {
  8. try {
  9. runTest()
  10. } catch (e) {
  11. throw e
  12. } finally {
  13. serverProcess.kill('SIGINT');
  14. }
  15. }, 100);
  16. function runTest() {
  17. var xhr = new XMLHttpRequest();
  18. var isSync = false;
  19. xhr.onreadystatechange = function () {
  20. if (xhr.readyState === 4) {
  21. assert.equal(xhr.responseText, "Hello world!");
  22. assert.equal(xhr.getResponseHeader('content-type'), 'text/plain')
  23. isSync = true;
  24. }
  25. }
  26. xhr.open("GET", "http://localhost:8888/text", false);
  27. xhr.send();
  28. assert(isSync, "XMLHttpRequest was not synchronous");
  29. xhr = new XMLHttpRequest();
  30. isSync = false;
  31. xhr.onreadystatechange = function () {
  32. if (xhr.readyState === 4) {
  33. assert.equal(xhr.response.toString(), 'Hello world!');
  34. assert.equal(xhr.getResponseHeader('content-type'), 'application/octet-stream')
  35. isSync = true;
  36. }
  37. }
  38. xhr.open("GET", "http://localhost:8888/binary", false);
  39. xhr.send();
  40. assert(isSync, "XMLHttpRequest was not synchronous");
  41. console.log("done");
  42. }