Giter VIP home page Giter VIP logo

request-mocha's Introduction

request-mocha Build status

Request utilities for Mocha.

This was built as a repetitive test utility to request results and assert later on.

Getting Started

Install the module with: npm install request-mocha

// In your test suite
var request = require('request');
var httpUtils = require('request-mocha')(request);
describe('A server receiving a request', function () {
  before(startServer);

  // Make request and save results to `this.err`, `this.res`, and `this.body`
  httpUtils.save('http://localhost:8080/');

  // Assert against mocha's `this` context
  it('responded with "Hello World!"', function () {
    expect(this.err).to.equal(null);
    expect(this.res.statusCode).to.equal(200);
    expect(this.body).to.equal('Hello World!');
  });
});

Documentation

request-mocha provides a function, requestMocha, as its module.exports.

requestMocha(request)

Create a set of utilities bound to a specific version of request.

This interface is necessary to prevent cross-version conflicts (e.g. jar problems)

  • request Request - request library to use for utility functions

requestMocha returns an object which we will refer to as httpUtils.

httpUtils.save(options)

Make a request to a server via request inside of a mocha before/setup block.

  • options Object - Parameters to pass through to request's request function

Results will be saved to mocha's this context. The same this context is shared between all mocha before, beforeEach, after, afterEach, and it invocations.

  • this.err Error|null - Error if one occurred while making the request (e.g. ECONNREFUSED)
  • this.res Response - Response from the server
  • this.body String - Response body from the server (alias for res.body)

httpUtils._save(options)

Invoke request/save mechanism without before/setup wrapper.

The parameters are the same as httpUtils.save.

The returned value is a function with a signature of (done). When invoked, it will write to this.err, this.res, and this.body as done in httpUtils.save.

It is expected that you invoke the returned function via a .call or .apply to an asynchronous before context with its callback. This is practical when there is data locked into a this context that needs to be used for a request.

// Prepare some `this` data
before(function () {
  this.credentials = {
    username: 'todd',
    password: 'keyboardcat'
  };
});

// In an asynchronous `before` block
before(function (done) {
  // Prepare the save call
  httpUtils._save({
    method: 'POST',
    url: 'http://localhost:8080/login',
    form: this.credentials
  // Invoke on the current context with the current callback
  }).call(this, done);
});

The alternative is to use var's outside of before blocks. Unfortunately, those cannot be re-used while a before function can be.

Examples

Making a POST request inside of tests

httpUtils.save({
  method: 'POST',
  url: 'http://localhost:8080/',
  form: {
    my: 'data'
  }
});

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via grunt and test via npm test.

License

Copyright (c) 2014 Uber

Licensed under the MIT license.

request-mocha's People

Contributors

twolfson avatar

Stargazers

Chris Wang avatar qianxing avatar

Watchers

Danny Yuan avatar Jeff Wolski avatar Tony Cosentini avatar Ryan McKillen avatar Jason Lai avatar Chirayu Krishnappa avatar Caleb Mingle avatar Casper S. Jensen avatar  avatar Joshua T Corbin avatar  avatar Nico Belmonte avatar Eugene Yaroslavtsev avatar Ignas Mikalajūnas avatar Aiden Scandella avatar Phil Wang avatar  avatar Ben Metcalfe avatar  avatar Sunil Garg avatar Braden Allchin avatar Willem Spruijt avatar Andrii Yasinetsky avatar Jason Bowman avatar Brent Goldman avatar Kyle Ivey avatar Zheng Shao avatar Nick Rabinowitz avatar Charles Ma avatar Eskil Heyn Olsen avatar Pedram Keyani avatar Anthony Tran avatar Guillaume Guillaume avatar Hao Truong avatar Kevin Novak avatar Sam Marcellus avatar Daniel Chen avatar  avatar Steven Kish avatar  avatar  avatar  avatar  avatar Hilary avatar Justin Muller avatar David avatar Ryan Sokol avatar Kurt Spindler avatar hao yan avatar Sameer Sayed avatar Paweł avatar  avatar  avatar Dom Narducci avatar Vikas Gupta avatar  avatar Mingjie Lai avatar Ed West avatar Dan Busch avatar  avatar  avatar Yixin Zhu avatar Esco Obong avatar Jeff Winner avatar  avatar Kevin Wang avatar Joakim Recht avatar Collin Greene avatar  avatar Haider Sabri avatar Jake Verbaten avatar  avatar Richard Tom avatar CHAD XU avatar Arun Nagarajan avatar Praveen avatar Brian Attwell avatar Martha Kelly Schumann avatar Amos Barreto avatar Anton Bulyonov (Bulyenov) avatar Margaret-Ann avatar jeff hu avatar Dustin Weatherford avatar Thomas Kielbus avatar Yifu Diao avatar Andreas Sæbjørnsen avatar Casey Lawler avatar Ashley avatar Will Hughes avatar  avatar Bryan Stitt avatar Ning Li avatar Chang Cheng avatar  avatar Xingzhong avatar Madan Thangavelu avatar Kevin Roth avatar David Hughes avatar  avatar Govind Kabra avatar

Forkers

twolfson

request-mocha's Issues

Delete `this.` bindings in `after` block

To prevent cross-test contamination, we should clean up our this. bindings inside of an after call.

after(function () {
  delete this.err;
  delete this.res;
  delete this.body;
});

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.