npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jasmine-http-server-spy

v0.5.0

Published

Creates jasmine spy objects backed by a http server.

Downloads

879

Readme

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.4.2

Update dependencies.

0.4.1

  • Return 500 if spy returns undefined.
  • Return 500 if spy returns a rejected Promise.

0.4.0

Support JSON sub-types like application/scim+json

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