Creates jasmine spy objects backed by a http server.
Homepage Repository npm JavaScript Download
npm install jasmine-http-server-spy@0.5.0
Creates jasmine spy objects backed by a http server. Designed to help you write integration tests where your code makes real http requests to a server, where the server is controlled via the familiar jasmine spy api.
npm install --save jasmine-http-server-spy
var jasmineHttpServerSpy = require('jasmine-http-server-spy');
describe('Test', function() {
beforeAll(function(done) {
this.httpSpy = jasmineHttpServerSpy.createSpyObj('mockServer', [
{
method: 'get',
url: '/some-url-to-mock',
handlerName: 'getSomeUrlToMock'
}
]);
this.httpSpy.server.start(8082, done);
// you can pass jasmine 'done' function as a callback, or use returned promise
// this.httpSpy.server.start(8082).then(done, done.fail);
// you can also specify the hostname to start the server on:
// this.httpSpy.server.start(8082, '127.0.0.1').then(done, done.fail);
// this is useful if you need to test multiple servers listening on the same port
});
afterAll(function(done) {
this.httpSpy.server.stop(done)
// you can pass jasmine 'done' function as a callback, or use returned promise:
// this.httpSpy.server.stop().then(done, done.fail);
});
afterEach(function() {
this.httpSpy.getSomeUrlToMock.calls.reset();
});
it('all the things', function() {
// 1. Define what mock server would return
this.httpSpy.getSomeUrlToMock.and.returnValue({
statusCode: 200,
body: {
data: 10
}
});
// 2. ... calls to main service that uses 'http://localhost:8082/some-url-to-mock'
// 3. Assert mock server has been called as expected
expect(this.httpSpy.getSomeUrlToMock).toHaveBeenCalled();
// or
expect(this.httpSpy.getSomeUrlToMock).toHaveBeenCalledWith(jasmine.objectContaining({
body: {
data: "something"
}
}));
});
it('can accept promise as handler returnValue', function() {
var deferred = q.defer();
this.httpSpy.getSomeUrlToMock.and.returnValue(deferred.promise);
setTimeout(function(){
deferred.resolve({
statusCode: 200,
body: {
data: 10
}
});
});
});
});
Handler function result will end up in the http response mock server gives back.
You can define code
, body
and headers
at the moment:
httpSpy.getSomeUrlToMock.and.returnValue {code: 200, body: {data: []}}
httpSpy.getSomeUrlToMock.and.returnValue {code: 200, body: '<xml>...</xml>', headers: {'Content-Type' : 'application/xml'}}
httpSpy.getSomeUrlToMock.and.returnValue {code: 401, body: {message: 'Please login first'}}
While handlers are jasmine spy objects, you can define a callback function to make response dynamic. For example:
httpSpy.getAnswerForANumber.and.callFake (req) ->
code: 200
body:
if req.body.number is 42
{answer: 'The answer to the ultimate question of life, the universe and everything'}
else
{answer: "I don't know"}
You can expect following properties in the first argument of this callback:
JS Object representing JSON body of a request. This object defaults to {}
.
Object containing all query parameters used. This object defaults to {}
.
Requested original URL. For example request to http://localhost:8082/mockService/users?something
end up as
/mockService/users?something
in originalUrl
Object containing all headers provided with request.
An object containing properties mapped to the named route "parameters".
For example, if you have the route /user/:name
, then the "name" property is available as req.params.name
.
This object defaults to {}
.
Update dependencies.
Support JSON sub-types like application/scim+json
start
now accepts hostname as optional second parameter start(8082, '127.0.0.1')
start
and stop
function return promise now. Use of jasmine done
callback is now optional.
Feel free to fork and make pull requests: https://bitbucket.org/atlassian/jasmine-http-server-spy/fork
Issues and suggestions can be added here: https://bitbucket.org/atlassian/jasmine-http-server-spy/issues