faux-server
Advanced tools
Comparing version 0.2.0 to 0.2.1
24
index.js
@@ -6,2 +6,4 @@ var store = require('store') | ||
var qs = require('qs'); | ||
module.exports = function(options) { | ||
@@ -21,6 +23,9 @@ options = options || {}; | ||
var url = request.requestURL; | ||
var urlPieces = request.requestURL.split('?'); | ||
var url = urlPieces[0]; | ||
var method = request.requestMethod.toLowerCase(); | ||
var jsonBody = JSON.parse(request.requestBody); | ||
var queryParams = qs.parse(urlPieces[1]); | ||
var respond = request.respond; | ||
@@ -72,4 +77,19 @@ request.respond = function(/* arguments */) { | ||
return request.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(response)); | ||
if(Object.keys(queryParams).length > 0) { | ||
var filteredResponse = response.filter(function(item) { | ||
var keep = true; | ||
Object.keys(queryParams).forEach(function(query) { | ||
if(item[query].toString() !== queryParams[query]) { | ||
keep = false; | ||
} | ||
}); | ||
return keep; | ||
}); | ||
return request.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(filteredResponse)); | ||
} else { | ||
return request.respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(response)); | ||
} | ||
} | ||
@@ -76,0 +96,0 @@ |
{ | ||
"name": "faux-server", | ||
"author": "Storcery", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Intercept requests to RESTful endpoints and replace them with requests against the client store.", | ||
@@ -29,2 +29,3 @@ "keywords": [ | ||
"nock": "^5.2.1", | ||
"qs": "^6.0.2", | ||
"store": "^1.3.20", | ||
@@ -31,0 +32,0 @@ "uuid": "^2.0.1" |
46
test.js
@@ -24,2 +24,6 @@ var axios = require('axios'); | ||
beforeEach(function() { | ||
global.localStorage.clear(); | ||
}); | ||
describe('when creating an object', function() { | ||
@@ -39,3 +43,3 @@ it('returns the object to be created and appends an id, createdAt, and updatedAt', function() { | ||
describe('when the object exists', function() { | ||
before(function() { | ||
beforeEach(function() { | ||
return axios.post(URL, { foo: 'bar' }).then(function(data) { | ||
@@ -65,12 +69,12 @@ id = data.data.id; | ||
describe('when reading from a collection endpoint', function() { | ||
before(function() { | ||
return axios.post(URL, { foo: 'bar' }) | ||
beforeEach(function() { | ||
return axios.post(URL, { foo: 'bar' }); | ||
}); | ||
it('returns an array with all the objects in that collection', function() { | ||
return axios.get(URL).catch(function(data) { | ||
expect(data.length).to.eq(1); | ||
expect(data[0].status).to.eq(201); | ||
expect(data[0].data).to.have.keys('foo', 'id'); | ||
expect(data[0].data.foo).to.eq('bar'); | ||
return axios.get(URL).then(function(data) { | ||
expect(data.status).to.eq(200); | ||
expect(data.data.length).to.eq(1); | ||
expect(data.data[0]).to.have.keys('foo', 'id', 'createdAt', 'updatedAt'); | ||
expect(data.data[0].foo).to.eq('bar'); | ||
}) | ||
@@ -80,2 +84,24 @@ }); | ||
describe('when using query params against a collection endpoint', function() { | ||
beforeEach(function() { | ||
return axios.post(URL, { name: 'foo', age: 34 }).then(function() { | ||
return axios.post(URL, { name: 'bar', age: 23 }); | ||
}).then(function() { | ||
return axios.post(URL, { name: 'biz', age: 32 }); | ||
}).then(function() { | ||
return axios.post(URL, { name: 'baz', age: 18 }); | ||
}); | ||
}); | ||
it('returns an array with all the objects in that collection', function() { | ||
return axios.get(URL + '?name=biz&age=32').then(function(data) { | ||
expect(data.status).to.eq(200); | ||
expect(data.data.length).to.eq(1); | ||
expect(data.data[0]).to.have.keys('name', 'age', 'id', 'createdAt', 'updatedAt'); | ||
expect(data.data[0].name).to.eq('biz'); | ||
expect(data.data[0].age).to.eq(32); | ||
}) | ||
}); | ||
}); | ||
describe('when deleting an object by id', function() { | ||
@@ -85,3 +111,3 @@ var id = null; | ||
describe('when the object exists', function() { | ||
before(function() { | ||
beforeEach(function() { | ||
return axios.post(URL, { foo: 'bar' }).then(function(data) { | ||
@@ -116,3 +142,3 @@ id = data.data.id; | ||
describe('when the object exists', function() { | ||
before(function() { | ||
beforeEach(function() { | ||
return axios.post(URL, { foo: 'bar' }).then(function(data) { | ||
@@ -119,0 +145,0 @@ id = data.data.id; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11298
235
7
+ Addedqs@^6.0.2
+ Addedobject-inspect@1.13.3(transitive)
+ Addedqs@6.13.1(transitive)
+ Addedside-channel@1.0.6(transitive)