GithubHelp home page GithubHelp logo

isabella232 / jasmine-http-server-spy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from atlassian/jasmine-http-server-spy

0.0 0.0 0.0 54 KB

Creates jasmine spy objects backed by a http server.

License: Apache License 2.0

CoffeeScript 94.51% TypeScript 5.49%

jasmine-http-server-spy's Introduction

jasmine-http-server-spy

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.

Install

$ npm install --save jasmine-http-server-spy

API

Server API by example

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's expected output

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'}}

Handler's input

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:

body

JS Object representing JSON body of a request. This object defaults to {}.

query

Object containing all query parameters used. This object defaults to {}.

originalUrl

Requested original URL. For example request to http://localhost:8082/mockService/users?something end up as /mockService/users?something in originalUrl

headers

Object containing all headers provided with request.

params

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 {}.

Changelog

0.3.1

start now accepts hostname as optional second parameter start(8082, '127.0.0.1')

0.3.0

start and stop function return promise now. Use of jasmine done callback is now optional.

Contribute

Feel free to fork it here https://bitbucket.org/atlassian/jasmine-http-server-spy/fork and make a pull request. Issues and suggestions can be added here https://bitbucket.org/atlassian/jasmine-http-server-spy/issues

jasmine-http-server-spy's People

Contributors

doverlock avatar jbatchelor-atlassian avatar jbunton-atlassian avatar pnguyen-atlassian avatar soswow avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.