Socket
Socket
Sign inDemoInstall

@gocardless/stubby

Package Overview
Dependencies
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@gocardless/stubby - npm Package Compare versions

Comparing version 0.0.7 to 0.0.8

.nvmrc

47

package.json
{
"name": "@gocardless/stubby",
"description": "AJAX Testing Stub Library",
"version": "0.0.7",
"version": "0.0.8",
"main": "index.js",

@@ -10,3 +10,3 @@ "directories": {

"scripts": {
"test": "./script/test",
"test": "jest",
"build": "./script/build",

@@ -27,14 +27,19 @@ "demo": "./script/demo",

],
"contributors": [{
"name": "Iain Nash"
}, {
"name": "Jack Franklin",
"email": "jack@jackfranklin.net"
}, {
"name": "Walter Carvalho",
"email": "waltervascarvalho@gmail.com"
}, {
"name": "Philip Harrison",
"email": "philip@mailharrison.com"
}],
"contributors": [
{
"name": "Iain Nash"
},
{
"name": "Jack Franklin",
"email": "jack@jackfranklin.net"
},
{
"name": "Walter Carvalho",
"email": "waltervascarvalho@gmail.com"
},
{
"name": "Philip Harrison",
"email": "philip@mailharrison.com"
}
],
"license": "MIT",

@@ -54,9 +59,15 @@ "bugs": {

"babel-eslint": "^4.1.6",
"bower": "^1.6.8",
"browserify": "^10.1.3",
"eslint": "^1.10.3",
"jasmine": "^2.3.1",
"phantomjs": "^1.9.18",
"uglifyjs": "^2.4.10"
"fake-xml-http-request": "^2.0.0",
"jest": "^23.4.0",
"uglifyjs": "^2.4.10",
"url-parse": "^1.4.1"
},
"jest": {
"verbose": true,
"setupFiles": [
"<rootDir>/setupTests.js"
]
}
}

@@ -0,1 +1,4 @@

# This library is legacy and will be modified to help us move away from it, and will not be maintained with the plan to deprecate this library later this year (2018).
## If you require a stubbing library see: [Pretender](https://github.com/trek/pretender)
# Stubby

@@ -2,0 +5,0 @@

@@ -1,13 +0,19 @@

'use strict';
describe('uses chaos money to randomise response status codes', function() {
describe('uses chaos money to randomise response status codes', () => {
var stubby;
beforeEach(function() {
stubby = new window.Stubby();
stubby.addModule(new window.StubbyChaosMonkey());
beforeEach(() => {
const xhrMockClass = () => ({
open: jest.fn(),
send: jest.fn(),
setRequestHeader: jest.fn()
});
global.XMLHttpRequest = jest.fn().mockImplementation(xhrMockClass);
stubby = new global.Stubby();
stubby.addModule(new global.StubbyChaosMonkey());
});
describe('stubbing out normal requests', function() {
it('will ignore requests with no chaos option', function(done) {
describe('stubbing out normal requests', () => {
it('will ignore requests with no chaos option', (done) => {
stubby.stub({

@@ -17,3 +23,3 @@ url: '/test'

window.get('/test', function(xhr) {
window.get('/test', (xhr) => {
expect(xhr.status).toEqual(200);

@@ -25,4 +31,4 @@ expect(JSON.parse(xhr.responseText)).toEqual({ ok: true });

it('will explode if the response port is not 43', function() {
expect(function() {
it('will explode if the response port is not 43', () => {
expect(() => {
stubby.stub({

@@ -39,4 +45,4 @@ url: '/test',

describe('stubbing out chaotic requests', function() {
it('will give a random response within a status range for a proper chaotic request', function(done) {
describe('stubbing out chaotic requests', () => {
it('will give a random response within a status range for a proper chaotic request', (done) => {
stubby.stub({

@@ -49,10 +55,10 @@ url: '/test',

var testResponseCheck = function(xhr) {
expect(xhr.status).toBeGreaterThan(100-1);
expect(xhr.status).toBeLessThan(600);
expect(JSON.parse(xhr.responseText)).toEqual({ ok: false });
};
for (var i = 0; i < 100; i++) {
for (var i = 0; i < 100; i++) {
window.get('/test', testResponseCheck);
window.get('/test', (xhr) => {
expect(xhr.status).toBeGreaterThan(99);
expect(xhr.status).toBeLessThan(600);
expect(JSON.parse(xhr.responseText)).toEqual({ ok: false });
});
if (i === 99) {

@@ -59,0 +65,0 @@ setTimeout(done, 1);

@@ -1,13 +0,17 @@

'use strict';
const schema = require('./test-schema.json');
// [!] URLs are relative to the test runner html file, not this javascript file.
var schemaStr = window.get({url: './modules/test-schema.json', async: false}).responseText;
var schema = JSON.parse(schemaStr);
describe('uses modules to validate json schema', function() {
describe('uses modules to validate json schema', () => {
var stubby;
beforeEach(function() {
stubby = new window.Stubby();
beforeEach(() => {
const xhrMockClass = () => ({
open: jest.fn(),
send: jest.fn(),
setRequestHeader: jest.fn()
});
var validator = new window.StubbySchemaValidator();
global.XMLHttpRequest = jest.fn().mockImplementation(xhrMockClass);
stubby = new global.Stubby();
var validator = new global.StubbySchemaValidator();
validator.addSchema('/', schema);

@@ -18,6 +22,9 @@ stubby.addModule(validator);

describe('stubbing out validated api queries', function() {
it('can send a valid customers list request', function(done) {
describe('stubbing out validated api queries', () => {
it('can send a valid customers list request', (done) => {
stubby.stub({
url: '/customers?limit=11',
url: '/customers',
params: {
limit: 11
},
method: 'GET'

@@ -32,4 +39,4 @@ }).respondWith(200, { meta: {}, customers: [] });

it('can match on GET data', function() {
expect(function() {
it('can match on GET data', () => {
expect(() => {
stubby.stub({

@@ -42,4 +49,4 @@ url: '/payments?undefined=blah&fubar',

it('throws with an invalid request', function() {
expect(function() {
it('throws with an invalid request', () => {
expect(() => {
stubby.stub({

@@ -52,3 +59,3 @@ url: '/payments?age=1,2,3,4',

it('throws an invalid post request', function() {
it('throws an invalid post request', () => {
stubby.stub({

@@ -59,6 +66,6 @@ url: '/customers',

expect(function() {
expect(() => {
window.post({
url: '/customers', data: { invalid: 'data' }
}, function() {
}, () => {
// Should throw.

@@ -70,4 +77,4 @@ });

describe('stubbing out wildcard routed urls', function() {
it('can validate a wildcard url', function(done) {
describe('stubbing out wildcard routed urls', () => {
it('can validate a wildcard url', (done) => {
stubby.stub({

@@ -84,4 +91,4 @@ url: '/customers/234235',

it('fails when not given a valid wildcard url', function() {
expect(function() {
it('fails when not given a valid wildcard url', () => {
expect(() => {
stubby.stub({

@@ -94,4 +101,4 @@ url: '/customers/asdf/asfd/a//a',

it('fails when parameters are given to a parameterless request', function() {
expect(function() {
it('fails when parameters are given to a parameterless request', () => {
expect(() => {
stubby.stub({

@@ -98,0 +105,0 @@ url: '/customers/123?param=fail',

@@ -1,41 +0,50 @@

'use strict';
function _specHelper(scope = {}) {
return ['get', 'post', 'put', 'delete'].forEach(method => {
scope[method] = function(url, cb) {
var options = {};
if (typeof url === 'string') {
options.url = url;
} else {
options = url;
}
'get post put delete'.split(' ').forEach(function(method) {
window[method] = function(url, cb) {
var options = {};
if (typeof url === 'string') {
options.url = url;
} else {
options = url;
}
if (!options.headers) {
options.headers = {};
}
if (options.async === undefined) {
options.async = true;
}
if (!options.headers) {
options.headers = {};
}
if (options.async === undefined) {
options.async = true;
}
var xhr = new XMLHttpRequest();
var xhr = new XMLHttpRequest();
xhr.open(method.toUpperCase(), options.url, !!options.async);
xhr.open(method.toUpperCase(), options.url, !!options.async);
xhr.setRequestHeader('Content-Type', 'application/json');
Object.keys(options.headers).forEach(function(header) {
xhr.setRequestHeader(header, options.headers[header]);
});
xhr.setRequestHeader('Content-Type', 'application/json');
Object.keys(options.headers).forEach(function(header) {
xhr.setRequestHeader(header, options.headers[header]);
});
var postBody = options.data ? JSON.stringify(options.data) : null;
var postBody = options.data ? JSON.stringify(options.data) : null;
if (typeof cb === 'function') {
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
cb(xhr);
}
};
}
if (typeof cb === 'function') {
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
cb(xhr);
}
};
}
xhr.send(postBody);
xhr.send(postBody);
return xhr;
};
});
return xhr;
};
});
}
if (typeof global !== 'undefined') {
_specHelper(global);
}
if (typeof window !== 'undefined') {
_specHelper(window);
}

@@ -1,388 +0,462 @@

'use strict';
describe('stubbing a URL', function() {
describe('create stubby', () => {
var stubby;
beforeEach(function() {
stubby = new window.Stubby();
beforeEach(() => {
stubby = new global.Stubby();
});
it('lets a URL be stubbed', function(done) {
stubby.stub({
url: '/foo'
}).respondWith(200, { foo: 2 });
describe('stubbing a URL', () => {
it('lets a URL be stubbed', done => {
stubby
.stub({
url: '/foo'
})
.respondWith(200, { foo: 2 });
window.get('/foo', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ foo: 2 });
expect(xhr.status).toEqual(200);
done();
global.get('/foo', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ foo: 2 });
expect(xhr.status).toEqual(200);
done();
});
});
});
it('differentiates on query params', function(done) {
stubby.stub({
url: '/foo?a=1'
}).respondWith(200, { a: 1});
it('differentiates on query params', done => {
stubby
.stub({
url: '/foo?a=1'
})
.respondWith(200, { a: 1 });
stubby.stub({
url: '/foo?b=2',
params: { b: 1 }
}).respondWith(200, { b: 1 });
stubby
.stub({
url: '/foo?b=2',
params: { b: 1 }
})
.respondWith(200, { b: 1 });
window.get('/foo?a=1', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
global.get('/foo?a=1', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
});
});
});
it('doesn\'t match a stub with query params against a URL without', function() {
stubby.stub({
url: '/foo?a=1'
}).respondWith(200);
it('doesn\'t match a stub with query params against a URL without', () => {
stubby
.stub({
url: '/foo?a=1'
})
.respondWith(200);
expect(function() {
window.get('/foo', function() {});
}).toThrowError();
});
expect(() => {
global.get('/foo', () => {});
}).toThrowError();
});
it('works with query params in both orders', function(done) {
stubby.stub({
url: '/foo?a=1&b=2'
}).respondWith(200, { a: 1, b: 2 });
it('works with query params in both orders', done => {
stubby
.stub({
url: '/foo?a=1&b=2'
})
.respondWith(200, { a: 1, b: 2 });
stubby.stub({
url: '/foo?b=3'
}).respondWith(200, { b: 3 });
stubby
.stub({
url: '/foo?b=3'
})
.respondWith(200, { b: 3 });
window.get('/foo?b=2&a=1', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1, b: 2 });
done();
global.get('/foo?b=2&a=1', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1, b: 2 });
done();
});
});
});
it('lets you define query params', function(done) {
stubby.stub({
url: '/foo',
params: { a: 1 }
}).respondWith(200, { a: 1 });
it('lets you define query params', done => {
stubby
.stub({
url: '/foo',
params: { a: 1 }
})
.respondWith(200, { a: 1 });
window.get('/foo?a=1', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
global.get('/foo?a=1', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
});
});
});
it('lets you match on headers', function(done) {
stubby.stub({
url: '/foo',
headers: {
foo: 'bar'
}
}).respondWith(200, { a: 1 });
it('lets you match on headers', done => {
stubby
.stub({
url: '/foo',
headers: {
foo: 'bar'
}
})
.respondWith(200, { a: 1 });
window.get({
url: '/foo',
headers: {
foo: 'bar'
}
}, function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
global.get(
{
url: '/foo',
headers: {
foo: 'bar'
}
},
function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
}
);
});
});
it('lets you stub response headers', function(done) {
stubby.stub({
url: '/foo',
}).respondWith(200, { a: 1 }, {
headers: { foo: 'bar' },
});
it('lets you stub response headers', done => {
stubby
.stub({
url: '/foo'
})
.respondWith(
200,
{ a: 1 },
{
headers: { foo: 'bar' }
}
);
window.get({
url: '/foo',
}, function(xhr) {
expect(xhr.getResponseHeader('foo')).toEqual('bar');
done();
global.get(
{
url: '/foo'
},
function(xhr) {
expect(xhr.getResponseHeader('foo')).toEqual('bar');
done();
}
);
});
});
it('lets you match on regex headers', function(done) {
stubby.stub({
url: '/foo',
headers: { a: '/\\w|\\d/g' }
}).respondWith(200, { a: 1 });
it('lets you match on regex headers', done => {
stubby
.stub({
url: '/foo',
headers: { a: '/\\w|\\d/g' }
})
.respondWith(200, { a: 1 });
window.get({
url: '/foo',
headers: {
a: 1
}
}, function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
global.get(
{
url: '/foo',
headers: {
a: 1
}
},
function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
}
);
});
});
it('ignores headers in the request not present in the stub', function(done) {
stubby.stub({
url: '/foo',
headers: { a: 1 }
}).respondWith(200, { a: 1 });
it('ignores headers in the request not present in the stub', done => {
stubby
.stub({
url: '/foo',
headers: { a: 1 }
})
.respondWith(200, { a: 1 });
window.get({
url: '/foo',
headers: {
a: 1,
b: 2
}
}, function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
global.get(
{
url: '/foo',
headers: {
a: 1,
b: 2
}
},
function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
}
);
});
});
it('matches on regex query param values', function(done) {
stubby.stub({
url: '/foo',
params: { a: '/\\w|\\d/g' }
}).respondWith(200, { a: 1 });
it('matches on regex query param values', done => {
stubby
.stub({
url: '/foo',
params: { a: '/\\w|\\d/g' }
})
.respondWith(200, { a: 1 });
window.get('/foo?a=1', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
global.get('/foo?a=1', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
});
});
});
it('all params are matched', function(done) {
stubby.stub({
url: '/foo',
params: { a: '/\\w|\\d/g', c: 1 }
}).respondWith(200, { a: 1 });
it('all params are matched', done => {
stubby
.stub({
url: '/foo',
params: { a: '/\\w|\\d/g', c: 1 }
})
.respondWith(200, { a: 1 });
stubby.stub({
url: '/foo',
params: { b: 1, c: 1, a: '/\\w|\\d/g' }
}).respondWith(200, { is: 'not matched' });
stubby
.stub({
url: '/foo',
params: { b: 1, c: 1, a: '/\\w|\\d/g' }
})
.respondWith(200, { is: 'not matched' });
window.get('/foo?a=1&c=1', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
global.get('/foo?a=1&c=1', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
});
});
});
});
describe('allows plugins on setup and on request', function() {
var stubby;
beforeEach(function() {
stubby = new window.Stubby();
});
describe('creates a new stub', function() {
var spies;
beforeEach(function() {
stubby = new window.Stubby();
spies = {
setup: jasmine.createSpy('setup'),
request: jasmine.createSpy('request'),
routesetup: jasmine.createSpy('routesetup')
};
stubby.addModule({
register: function(stubbyInstance) {
stubbyInstance.on('setup', spies.setup);
stubbyInstance.on('routesetup', spies.routesetup);
stubbyInstance.on('request', spies.request);
}
describe('allows plugins on setup and on request', () => {
describe('creates a new stub', () => {
var spies;
beforeEach(() => {
spies = {
setup: jasmine.createSpy('setup'),
request: jasmine.createSpy('request'),
routesetup: jasmine.createSpy('routesetup')
};
stubby.addModule({
register: function(stubbyInstance) {
stubbyInstance.on('setup', spies.setup);
stubbyInstance.on('routesetup', spies.routesetup);
stubbyInstance.on('request', spies.request);
}
});
stubby
.stub({
url: '/test'
})
.respondWith(200, {});
});
stubby.stub({
url: '/test'
}).respondWith(200, {});
});
it('makes one request when a stub is made for setup', function() {
expect(spies.routesetup).toHaveBeenCalled();
expect(spies.setup).not.toHaveBeenCalled();
expect(spies.request).not.toHaveBeenCalled();
});
it('makes one request to setup and one request to request when set', function(done) {
expect(spies.routesetup).toHaveBeenCalled();
window.get('/test', function() {
expect(spies.setup).toHaveBeenCalled();
expect(spies.request).toHaveBeenCalled();
done();
it('makes one request when a stub is made for setup', () => {
expect(spies.routesetup).toHaveBeenCalled();
expect(spies.setup).not.toHaveBeenCalled();
expect(spies.request).not.toHaveBeenCalled();
});
it('makes one request to setup and one request to request when set', done => {
expect(spies.routesetup).toHaveBeenCalled();
global.get('/test', () => {
expect(spies.setup).toHaveBeenCalled();
expect(spies.request).toHaveBeenCalled();
done();
});
});
});
});
});
describe('verifiying that stubs have been used', function() {
var stubby;
beforeEach(function() {
stubby = new window.Stubby();
});
describe('verifiying that stubs have been used', () => {
it('errors if a stub is not used', () => {
stubby.stub({ url: '/foo' }).respondWith(200, {});
it('errors if a stub is not used', function() {
stubby.stub({ url: '/foo' }).respondWith(200, {});
expect(() => {
stubby.verifyNoOutstandingRequest();
}).toThrowError();
});
expect(function() {
stubby.verifyNoOutstandingRequest();
}).toThrowError();
});
it('doesn\'t error when all stubs are satisfied', done => {
stubby.stub({ url: '/foo' }).respondWith(200, {});
stubby.stub({ url: '/bar' }).respondWith(200, {});
it('doesn\'t error when all stubs are satisfied', function(done) {
stubby.stub({ url: '/foo' }).respondWith(200, {});
stubby.stub({ url: '/bar' }).respondWith(200, {});
window.get('/foo', function() {
window.get('/bar', function() {
try {
expect(function() {
stubby.verifyNoOutstandingRequest();
}).not.toThrow();
done();
} catch (e) {
done(e);
}
global.get('/foo', () => {
global.get('/bar', () => {
try {
expect(() => {
stubby.verifyNoOutstandingRequest();
}).not.toThrow();
done();
} catch (e) {
done(e);
}
});
});
});
});
it('errors if multiple stubs aren\'t used', function() {
stubby.stub({ url: '/foo' }).respondWith(200, {});
stubby.stub({ url: '/foo', method: 'POST' }).respondWith(200, {});
it('errors if multiple stubs aren\'t used', () => {
stubby.stub({ url: '/foo' }).respondWith(200, {});
stubby.stub({ url: '/foo', method: 'POST' }).respondWith(200, {});
expect(function() {
stubby.verifyNoOutstandingRequest();
}).toThrowError('Stub(s) were not called: GET /foo/, POST /foo/');
});
expect(() => {
stubby.verifyNoOutstandingRequest();
}).toThrowError('Stub(s) were not called: GET /foo/, POST /foo/');
});
it('can deal with multiple stubs', function(done) {
stubby.stub({ url: '/foo' }).respondWith(200, {});
stubby.stub({ url: '/bar' }).respondWith(200, {});
it('can deal with multiple stubs', done => {
stubby.stub({ url: '/foo' }).respondWith(200, {});
stubby.stub({ url: '/bar' }).respondWith(200, {});
window.get('/foo', function() {
expect(function() {
stubby.verifyNoOutstandingRequest();
}).toThrowError();
global.get('/foo', () => {
expect(() => {
stubby.verifyNoOutstandingRequest();
}).toThrowError();
done();
done();
});
});
});
it('can deal with query params', function(done) {
stubby.stub({ url: '/foo', params: { a: 1 } }).respondWith(200, {});
stubby.stub({ url: '/foo', params: { b: 1 } }).respondWith(200, {});
it('can deal with query params', done => {
stubby.stub({ url: '/foo', params: { a: 1 } }).respondWith(200, {});
stubby.stub({ url: '/foo', params: { b: 1 } }).respondWith(200, {});
window.get('/foo?a=1', function() {
expect(function() {
stubby.verifyNoOutstandingRequest();
}).toThrowError();
global.get('/foo?a=1', () => {
expect(() => {
stubby.verifyNoOutstandingRequest();
}).toThrowError();
done();
done();
});
});
});
});
describe('stubbing a POST url', function() {
var stubby;
beforeEach(function() {
stubby = new window.Stubby();
});
describe('stubbing a POST url', () => {
it('stubs a post URL', done => {
stubby
.stub({
url: '/foo',
method: 'POST'
})
.respondWith(200, { a: 1 });
it('stubs a post URL', function(done) {
stubby.stub({
url: '/foo',
method: 'POST'
}).respondWith(200, { a: 1 });
window.post('/foo', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
global.post('/foo', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
});
});
});
it('can match on POST data', function(done) {
stubby.stub({
url: '/foo',
data: { b: 2 },
method: 'POST'
}).respondWith(200, { a: 1 });
describe('when stubbing POST data', () => {
it('can match stub data to POST data', done => {
stubby
.stub({
url: '/foo',
data: { b: 2 },
method: 'POST'
})
.respondWith(200, { a: 1 });
window.post({ url: '/foo', data: { b: 2 } }, function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
global.post({ url: '/foo', data: { b: 2 } }, function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
});
});
describe('when POST data is stringified object', () => {
it('can match on POST body', done => {
stubby
.stub({
url: '/foo',
data: { b: 2 },
method: 'POST'
})
.respondWith(200, { a: 1 });
global.post({ url: '/foo', data: JSON.stringify({ b: 2 }) }, function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
});
});
});
describe('when POST data is a string', () => {
it('should throw an error as it can not match', () => {
stubby
.stub({
url: '/foo',
data: JSON.stringify({ b: 2 }),
method: 'POST'
})
.respondWith(200, { a: 1 });
expect(() => {
global.post({ url: '/foo', data: { b: 2 } }, () => {});
}).toThrow();
});
});
});
});
it('matches a stub if the stub has no data but the request does', function(done) {
stubby.stub({
url: '/foo',
method: 'POST'
}).respondWith(200, { a: 1 });
it('matches a stub if the stub has no data but the request does', done => {
stubby
.stub({
url: '/foo',
method: 'POST'
})
.respondWith(200, { a: 1 });
window.post({ url: '/foo', data: { b: 2 } }, function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
global.post({ url: '/foo', data: { b: 2 } }, function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ a: 1 });
done();
});
});
});
it('can differentiate between POST and PUT data', function(done) {
stubby.stub({
url: '/foobar',
method: 'POST'
}).respondWith(200, { method: 'get' });
it('can differentiate between POST and PUT data', done => {
stubby
.stub({
url: '/foobar',
method: 'POST'
})
.respondWith(200, { method: 'get' });
stubby.stub({
url: '/foobar',
method: 'PUT'
}).respondWith(200, { method: 'put' });
stubby
.stub({
url: '/foobar',
method: 'PUT'
})
.respondWith(200, { method: 'put' });
window.put('/foobar', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ method: 'put' });
done();
global.put('/foobar', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ method: 'put' });
done();
});
});
});
it('can differentiate between a GET and PUT', function(done) {
stubby.stub({
url: '/foobar',
method: 'PUT'
}).respondWith(200, { method: 'put' });
stubby.stub({
url: '/foobar',
method: 'GET'
}).respondWith(200, { method: 'get' });
it('can differentiate between a GET and PUT', done => {
stubby
.stub({
url: '/foobar',
method: 'PUT'
})
.respondWith(200, { method: 'put' });
stubby
.stub({
url: '/foobar',
method: 'GET'
})
.respondWith(200, { method: 'get' });
window.get('/foobar', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ method: 'get' });
done();
global.get('/foobar', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ method: 'get' });
done();
});
});
});
});
describe('stubbing the same URL twice', function() {
var stubby;
beforeEach(function() {
stubby = new window.Stubby();
});
describe('stubbing the same URL twice', () => {
it('fails when a matching stub is redeclared', () => {
stubby.stub({ url: '/foo' }).respondWith(200, { first: true });
it('fails when a matching stub is redeclared', function() {
stubby.stub({ url: '/foo' }).respondWith(200, { first: true });
expect(() => {
stubby.stub({ url: '/foo' }).respondWith(200, { first: false });
}).toThrow();
});
expect(function() {
stubby.stub({ url: '/foo' }).respondWith(200, { first: false });
}).toThrow();
});
it('lets you override if you pass the overrideStub param', done => {
stubby.stub({ url: '/foo' }).respondWith(200, { first: true });
it('lets you override if you pass the overrideStub param', function(done) {
stubby.stub({ url: '/foo' }).respondWith(200, { first: true });
expect(() => {
stubby.stub({ url: '/foo', overrideStub: true }).respondWith(200, { first: false });
}).not.toThrow();
expect(function() {
stubby.stub({ url: '/foo', overrideStub: true }).respondWith(200, { first: false });
}).not.toThrow();
window.get('/foo', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ first: false });
done();
global.get('/foo', function(xhr) {
expect(JSON.parse(xhr.responseText)).toEqual({ first: false });
done();
});
});
});
});

@@ -77,3 +77,2 @@ 'use strict';

var method = request.method;
var data = request.data;

@@ -106,5 +105,18 @@ this.emit('setup', stub, request);

var stubbedRequestData = stub.request.data;
var requestData = request.data;
// if no stub data was given, we just say that we matched
if (!_.isEmpty(stub.request.data)) {
dataRequestMatch = _.isEqual(stub.request.data, data);
if (!_.isEmpty(stubbedRequestData)) {
// if the data is a string we assume it is JSON string
if (typeof requestData === 'string') {
try {
var parsedRequestData = JSON.parse(requestData);
dataRequestMatch = _.isEqual(stubbedRequestData, parsedRequestData);
} catch (e) {
dataRequestMatch = _.isEqual(stubbedRequestData, requestData);
}
} else {
dataRequestMatch = _.isEqual(stubbedRequestData, requestData);
}
} else {

@@ -322,2 +334,1 @@ dataRequestMatch = true;

}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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