All Projects → algolia → Faux Jax

algolia / Faux Jax

Licence: mit
NO MORE MAINTAINED: Intercept and respond to requests in the browser (AJAX) and Node.js (http(s) module)

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Faux Jax

stub-server
Stub server for REST APIs
Stars: ✭ 14 (-84.95%)
Mutual labels:  mock, test
Mastermind
Man in the middle testing
Stars: ✭ 341 (+266.67%)
Mutual labels:  test, mock
Hippolyte
HTTP Stubbing in Swift
Stars: ✭ 109 (+17.2%)
Mutual labels:  mock, test
better-mock
Forked from Mockjs, Generate random data & Intercept ajax request. Support miniprogram.
Stars: ✭ 140 (+50.54%)
Mutual labels:  mock, ajax
Smocker
Smocker is a simple and efficient HTTP mock server and proxy.
Stars: ✭ 465 (+400%)
Mutual labels:  test, mock
local-data-api
Data API for local, you can write unittest for AWS Aurora Serverless's Data API
Stars: ✭ 99 (+6.45%)
Mutual labels:  mock, test
Mockito
HTTP mocking for Rust!
Stars: ✭ 335 (+260.22%)
Mutual labels:  test, mock
dextool
Suite of C/C++ tooling built on LLVM/Clang
Stars: ✭ 81 (-12.9%)
Mutual labels:  mock, test
Shellspec
A full-featured BDD unit testing framework for bash, ksh, zsh, dash and all POSIX shells
Stars: ✭ 375 (+303.23%)
Mutual labels:  test, mock
Mimic
Seamless client side mocking
Stars: ✭ 380 (+308.6%)
Mutual labels:  test, mock
node-mock-examples
Examples of tests that mock Node system APIs: fs, http, child_process, timers
Stars: ✭ 38 (-59.14%)
Mutual labels:  mock, test
Mockswift
MockSwift is a Mock library written in Swift.
Stars: ✭ 56 (-39.78%)
Mutual labels:  test, mock
laika
Log, test, intercept and modify Apollo Client's operations
Stars: ✭ 99 (+6.45%)
Mutual labels:  mock, test
jest-launchdarkly-mock
Easily unit test LaunchDarkly feature flagged components with jest
Stars: ✭ 14 (-84.95%)
Mutual labels:  mock, test
htest
htest is a http-test package
Stars: ✭ 24 (-74.19%)
Mutual labels:  mock, test
zmock
zmock--http接口的mock平台
Stars: ✭ 98 (+5.38%)
Mutual labels:  mock, test
mockingbird
🐦 Decorator Powered TypeScript Library for Creating Mocks
Stars: ✭ 70 (-24.73%)
Mutual labels:  mock, test
instant-mock
Quick and Easy web API mock server.
Stars: ✭ 27 (-70.97%)
Mutual labels:  mock, test
Fakerest
Patch fetch/XMLHttpRequest to fake a REST API server in the browser, based on JSON data.
Stars: ✭ 350 (+276.34%)
Mutual labels:  test, mock
Backbone Faux Server
A framework for mocking up server-side persistence / processing for Backbone.js
Stars: ✭ 55 (-40.86%)
Mutual labels:  test, mock

MESSAGE FROM MAINTAINERS: This module is not maintained, you can fork it still or there are maybe better solutions nowadays.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].