faux-jax
Intercept and respond to:
- XMLHttpRequest
- XDomainRequest in compatible environments
- Node.js http(s) module
npm install faux-jax --save[-dev]
Browser example
var fauxJax = ; fauxJax; ;fauxJax; // somewhere in your code: { var xhr = ; xhr; xhr; xhr; xhr { console; // 200 console; // {zup: 'bro'} }} // in a test file probably: { request; fauxJax;}
Node.js example
var http = ;var fauxJax = ; fauxJax; ;fauxJax; { http;} { request; fauxJax;}
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
Thanks
Inspiration for this module came from:
- Sinon.js's Fake XMLHttpRequest
- trek's FakeXMLHttpRequest
Many thanks!
Node.js version is using moll/node-mitm.