Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

can-fixture

Package Overview
Dependencies
Maintainers
4
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

can-fixture

Intercept AJAX requests and simulate responses.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.9K
increased by52.82%
Maintainers
4
Weekly downloads
 
Created
Source

Build Status npm version

can-fixture

can-fixture intercepts an AJAX request and simulates the response with a file or function. Use can-fixture to:

  • Develop JavaScript independently of the backend services.
  • Test code that makes AJAX requests without needing a server.
  • Simulate slow responses or difficult to reproduce error conditions.

can-fixture is self contained and can be used without the rest of CanJS.

Install

If you are using Browserify or StealJS, install it with NPM:

npm install can-fixture --save-dev

Then import, require, steal, or define the "can-fixture" module:

var fixture = require("can-fixture");

Basic Use

Use the fixture function to trap settings on a XMLHttpRequest object to a request handler.

The following traps all GET type requests to /service and results in a responseText of "{\"message\":\"Hello World\"}":

fixture({url: "/service", method: "get"}, function(request, response){
  response({message: "Hello World"});
})

The fixture function has a wide variety of signatures that allow more control or easier shorthands. The previous example could be written like:

fixture("GET /service", function(request, response){
  return {message: "Hello World"};
})

Or:

fixture("GET /service", {message: "Hello World"});

You can forward a request to another url:

fixture("GET /service", "/fixtures/service.json");

Multiple fixture rules can be setup at once like:

fixture({
  "GET /service": {message: "Hello World"},
  "POST /service": function(request, response){
    response(401,"{type: 'unauthorized'}");
  }
});

Remove a fixture by calling fixture with null in place of a responseHandler:

fixture("GET /service", null);

Finally, you can create a can-connect like object that simulates a restful service and provides methods to .

var tasksStore = fixture.store([{
				_id : 1,
				name : 'Cheese City',
				slug : 'cheese-city',
			}, {
				_id : 2,
				name : 'Crab Barn',
				slug : 'crab-barn',
			}], );

APIs

fixture(ajaxSettingsOrUrl, requestHandlerOrUrl)

For example:

fixture({url: "/service", method: "get"}, function(){
  
})
fixture(ajaxSettings, function(request, responseHandler, requestHeaders))

fixture(url,

Keywords

FAQs

Package last updated on 18 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc