faux-jax

5.0.6 • Public • Published

faux-jax Version Badge Build Status License Downloads

Browser tests

Intercept and respond to:

npm install faux-jax --save[-dev]

Browser example

var fauxJax = require('faux-jax');
 
fauxJax.install();
 
doRequest();
fauxJax.on('request', respond);
 
// somewhere in your code:
function doRequest() {
  var xhr = new XMLHttpRequest();
 
  xhr.open('POST', '/dawg');
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.send(
    JSON.stringify({
      YAW: 'dawg'
    })
  );
  xhr.onload = function() {
    console.log(xhr.status); // 200
    console.log(xhr.response); // {zup: 'bro'}
  }
}
 
// in a test file probably:
function respond(request) {
  request.respond(
    200, { // status
      'Content-Type': 'application/json' // headers
    },
    '{"zup": "bro?"}' //body
  );
 
  fauxJax.restore();
}

Node.js example

var http = require('http');
var fauxJax = require('faux-jax');
 
fauxJax.install();
 
doRequest();
fauxJax.on('request', respond);
 
function doRequest() {
  http.request('http://www.google.com', function(res) {
    console.log(res.statusCode); // 200
 
    var chunks = [];
    res.on('data', function(chunk) {
      chunks.push(chunk);
    });
 
    res.on('end', function() {
      console.log(Buffer.concat(chunks).toString());
    });
  }).end();
}
 
function respond(request) {
  request.respond(
    200, { // status
      'Content-Type': 'text/plain' // headers
    },
    'Hello Node.js!' //body
  );
 
  fauxJax.restore();
}

API

fauxJax.install([opts])

Replace global XMLHttpRequest and XDomainRequest with mocks.

  • opts.gzip: boolean. Set to true in nodejs to receive gzipped responses.

fauxJax.on('request', cb)

fauxJax is an EventEmitter.

Everytime a new request is made, you will get a request event.

You can listen to it with cb(request).

All requests have the native properties/methods from the spec.

We also added a couple of handy properties/methods for you to ease testing.

fauxJax.waitFor(nbRequests, cb)

Utility to "wait for n requests". Will call cb(err, requests).

request.requestMethod

request.requestURL

request.requestHeaders

Always {} with XDomainRequest.

request.requestBody

request.respond(status[, headers, body])

request.setResponseHeaders(headers)

request.setResponseBody(body[, cb])

fauxJax.restore()

Sets back global XMLHttpRequest and XDomainRequest to native implementations.

fauxJax.support

Object containing various support flags for your tests, used internally by faux-jax.

Errors

Errors will be emitted when:

  • you try to .install() when already installed
  • you try to .restore() without calling .install()
  • a request was intercepted while no listener set

How

tl;dr; We try to be as close as possible to the mocked native environment.

faux-jax uses feature detection to only expose what's relevant for the current environment.

i.e. on Chrome, we do not intercept nor expose XDomainRequest.

Also if the browser only implement some parts of XMLHttpRequest, we mimic it.

Test

npm test

Develop

npm run dev

Go to http://localhost:8080/__zuul.

Tests are written with tape and run through zuul.

Lint

npm run lint

Uses eslint, see .eslintrc.

Thanks

Inspiration for this module came from:

Many thanks!

Node.js version is using moll/node-mitm.

Package Sidebar

Install

npm i faux-jax

Weekly Downloads

718

Version

5.0.6

License

MIT

Last publish

Collaborators

  • louishousiaux
  • antoine.gilles
  • eventexperiences_algolia
  • jsok_algolia
  • bhinchley-algolia
  • hugowit
  • alg-admin
  • aymeric.giraudet
  • gavinwade12
  • bhcastle
  • rishi_algolia
  • jasonberry
  • scyganek-algolia
  • drodriguln
  • abodelot
  • leviwhalen
  • sfaiqh
  • valentindotxyz
  • fluf
  • instantsearch-bot
  • taylorcjohnson_algolia
  • pjankowski5312
  • raed-algolia
  • dylantientcheu
  • catalgolia
  • morgan-algolia
  • algabet
  • andy_ds
  • bengreenbank
  • alg-bgastinne
  • daltondickalgolia
  • jcohonner-algolia
  • robertmogos
  • mariaalungu
  • emmanuel.fortin
  • ejaldorau
  • dhaya.b
  • lukyvj
  • marielaure
  • haroenv
  • rayrutjes
  • jerska
  • ronanlevesque
  • samouss
  • therealwebby
  • francoischalifour
  • jonmontane
  • tkrugg
  • seafoox
  • sylvainh
  • clemfromspace
  • lorrissaintgenez
  • broujo
  • e-krebs
  • kombucha
  • sylvain
  • crawler-team
  • amcdaid106
  • devinalgolia
  • jvenezia
  • otomatik
  • sophiem03
  • sarahdayan
  • dcoates
  • maximehuang
  • guitek
  • matthewbond
  • cyril.descossy
  • tatsuro
  • danajeremy
  • mathougui
  • xavdh
  • agdavid
  • rasemotte
  • plnech
  • shortcuts
  • praagyajoshi
  • loicsay
  • svensoldin
  • alphonseb
  • leodau
  • fabienmotte
  • sarahfranc
  • millotp
  • kumavis
  • vvo