How to test AJAX on the command line?

Suppose I have JavaScript code that uses jQuery.ajax to call the REST server API.

To test this, I would like

  • Use a dummy HTTP server that reads responses from static XML files
  • run the JavaScript "headless", i.e. without a browser;
  • run all of these tests on the command line.

It looks like I can use node.js with a built-in dummy HTTP server. Does this make sense? Do you know any examples of such projects (on github or elsewhere)?

+4
source share
2 answers

I have not done something similar yet, but to be honest, I evaluate the following approaches:

http://blog.stevensanderson.com/2010/03/30/using-htmlunit-on-net-for-headless-browser-automation/

This is really interesting, I hope this link helps you get started.

+1
source

The main problem with using node.js is that it does not provide everything that the browser will provide ... including XMLHttpRequest.

I am developing a library (proprietary, for internal use only) that I am trying to test with node.js.

My work on the lack of XHR support is to use this XHR implementation in conjunction with some Microsoft-provided code to add backward compatibility for standard XHR to old IE .

If you are using jQuery, you probably need to change it, since jQuery adds its own compatibility later for old IE.

The result is as follows:

 var XMLHttpRequest; if (typeof window !== 'undefined') { XMLHttpRequest = window.XMLHttpRequest; } if (typeof XMLHttpRequest === "undefined") { console.log("Undefined"); (function() { try { XMLHttpRequest = new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e) {} try { XMLHttpRequest = new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e) {} try { XMLHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} try { XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; } catch(e) {} //Microsoft.XMLHTTP points to Msxml2.XMLHTTP and is redundant if (typeof XMLHttpRequest === "undefined") { throw new Error("This browser does not support XMLHttpRequest."); } })(); }; 

Also, starting a simple HTTP server in your test suite is a common approach for this kind of problem. I have not yet implemented it, and I'm working on httpd running on a test server.

+1
source

Source: https://habr.com/ru/post/1415843/


All Articles