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

request-mockery

v1.4.0

Published

request-mockery

Downloads

13

Readme

request-mockery

This is a very simple library to help write tests using mockery when testing code that uses the request http module. It lets you pass through the options or it also allows you to set a return value. Current way of setting the return value is a little brittle, and if other folks have any ideas about being able to set the value or return function in a more intuitive way, your input would be very appreciated. Mockery registers stubs at the beginning of a describe block, so I would like to make it be able to supply several different responses each time it is registered. I take a md5 of the request options and that is how I attach a response. I think that the md5 will possibly change often, so that is going to get annoying. Is there another way to design that?

For now, you can either register a response or you can not register a response and the library will just return the options.

I'm currently making a lot of api related libraries, and all of the use request somewhere in the code, but I don't want to actually make requests to the server, so it is nice to be able to see what sort of requests the module would make and also be able to supply a response to see what the module does with it.

npm install request-mockery --save-dev

Usage

var request = require('request-mockery');
request.verbosity(true);

request({
    url: 'http://google.com', 
    method: "GET"
  }, function(err, response, body) { 
    console.log(err, response, body)
    // null { code: 200 } { url: 'http://google.com', method: 'POST', json: { test: 'test' } }
})

request.responses({
  "d45e1f70c855b724fadb07f8695ae3f6": {
    arbitrary: 'value'
  }
})

request({
    url: 'http://google.com', 
    method: "GET"
  }, function(err, response, body) { 
    console.log(err, response, body)
    // null { code: 200 } { arbitrary: 'value' }
})

// if you set a body, status and error key, then it will return those in the proper arity.
request.responses({
  'd45e1f70c855b724fadb07f8695ae3f6': {
    body: {arbitrary: 'value'}, 
    status: {code: 500}, 
    error: {another: 'value'}
  }
})

request({
    url: 'https://google.com', 
    method: "GET"
  }, function(err, response, body) { 
    console.log(err, response, body)
    // { another: 'value' } { code: 500 } { arbitrary: 'value' }
})

In a test

var path    = require('path');
var mockery = require('mockery');
var should  = require('chai').should();
var assert  = require('assert');
var request = require(path.join(__dirname, '..', 'index'));

describe('RequestMockery', function() {
  var FakeModule;

  before(function(){
    mockery.enable({
      warnOnReplace: false,
      warnOnUnregistered: false,
      useCleanCache: true
    });
    // request.verbosity(true);
    request.responses({
      "03684ab85d63ef1261c356d19b1f235e": {"arbitrary": true}
    })
    mockery.registerMock('request', request);
    FakeModule = require(path.join(__dirname, 'fake_module'));
 
  });

  after(function(){
    mockery.disable();
  }); 


  describe('FakeModule', function() {
    describe('test', function() {
      it('invalid should work', function(done) {
        var api = new FakeModule();
        api.exec({}, function(err, resp, body) {
          assert.equal(err, null);
          assert.equal(resp.code, 200);
          assert.equal(body, '{}');
          done();
        })
      });
      it('return response', function(done) {
        var api = new FakeModule();
        api.exec({
          url: 'https://google.com', 
          method: 'GET'
        }, function(err, resp, body) {
          assert.equal(err, null);
          assert.equal(resp.code, 200);
          assert.equal(JSON.parse(body).url, 'https://google.com');
          done();
        })
      });

      it('can set response', function(done) {
        var api = new FakeModule();
        api.exec({
          url: 'https://yahoo.com', 
          method: 'GET'
        }, function(err, resp, body) {
          assert.equal(err, null);
          assert.equal(resp.code, 200);
          assert.equal(JSON.parse(body).arbitrary, true);
          done();
        })
      });
    });
  });
});

Contributing

If you'd like to contribute a feature or bugfix: Thanks! To make sure your fix/feature has a high chance of being included, please read the following guidelines:

  1. Post a pull request.
  2. Make sure there are tests! We will not accept any patch that is not tested. It's a rare time when explicit tests aren't needed. If you have questions about writing tests for paperclip, please open a GitHub issue.

And once there are some contributors, then I would like to thank all of the contributors!

License

It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.

Copyright

© 2018 Scott Ballantyne. See LICENSE for details.