hippie

Simple end-to-end API testing

Thin request wrapper that enables powerful and intuitive API testing.

  • Intuitive and consistent API
  • Built-in and custom expectations
  • Support for custom parsers and serializers
  • Easy to extend
  • Middlewares
  • Works great with any test runner
var api = require('hippie');
hippie()
.header("User-Agent", "hippie")
.json()
.get('https://api.github.com/users/vesln')
.expectStatus(200)
.end(function(errresbody) {
  if (err) throw err;
});
hippie()
.json()
.base('http://localhost:1234')
.get('/users/vesln')
.expectStatus(200)
.expectHeader('Content-Type', 'application/json; charset=utf-8')
.expectValue('username', 'vesln')
.expectValue('repos[0].name', 'jsmd')
.expectBody({
  username: 'vesln',
  repos: [
    { name: 'jsmd' },
    { name: 'hippie' },
  ]
})
.expectBody(/vesln/g)
.end(function(errresbody) {
  if (err) throw err;
  process.exit(0);
});
hippie()
.json()
.use(function(optionsnext) {
  // modify the options for `request` here 
  next(options);
})
.get('https://api.github.com/users/vesln')
.end(function(errresbody) {
  if (err) throw err;
});

With middlewares you can modify the options passed to request. Here is an example how you could persist the cookies across multiple requests:

hippie(app)
.get('/')
.use(persistCookies)
.end(function() {});
 
function persistCookies(optsnext) {
  opts.jar = true;
  next(opts);
}
var xml = require('my-xml-library');
 
hippie()
.serializer(function(paramsfn) {
  var err = new Error('Things went wrong');
  var res = xml.objectToXml(params);
  fn(err, res);
})
.parser(function(bodyfn) {
  var err = new Error('Things went wrong');
  var res = xml.xmlToObject(body);
  fn(err, res);
})
.get('https://api.github.com/users/vesln.xml')
.expectStatus(200)
.end(function(errresbody) {
  if (err) throw err;
});

Similar to Chai.js and other frameworks, you can enable showDiff.

var hippie = require('hippie');
hippie.assert.showDiff = true;

Since most of the time your test setup is going to be the same, you can simply create a helper function for your tests that will take care of the repetitive setup:

var hippie = require('hippie');
 
function api() {
  return hippie()
    .json()
    .serializer(customSerializer)
    .parser(customParser)
    .use(somethingSpecial)
    .base('http://localhost:3000/api/v1')
    .auth('user', 'pass')
    .expect(somethingRepetable);
}

Later on:

test('my awesome api', function(done) {
  api()
  .get('/users')
  .expectStatus(200)
  .end();
});

Configure a timeout for the HTTP request.

hippie()
.timeout(1000)
.end(fn);

Convert an object to query string values:

hippie()
.qs({ foo: 'bar' })
.end(fn);

Configure a base URL, useful when testing the same API endpoint.

hippie()
.base('https://api.github.com')
.get('/users/vesln')
.end(fn);

Set the URL for the HTTP request. Used internally by get, put etc. and it should be used in combination with method.

hippie()
.url('https://api.github.com')
.method('GET')
.end(fn);

Configure the HTTP method. Used internally by get, put etc.

hippie()
.url('https://api.github.com')
.method('OPTIONS')
.end(fn);

Set a request header.

hippie()
.header('Content-Type', 'application/json')
.send({ some: 'data' })
.end(fn);

Helper method for:

  • Content-Type: application/json
  • Accept: application/json
  • Serializer: json
  • Parser: json
hippie()
.json()
.get('https://github.com/vesln.json', fn);

Helper method for:

  • Content-Type: application/x-www-form-urlencoded
  • Serializer: urlencoded
hippie()
.form()
.patch('https://api.mindbloom.com/users/vesln');
.send({ timezone: 'UTC' }
.end();

Configure a request body serializer.

hippie()
.serializer(function(paramsfn) {
  var err = new Error('Things went wrong');
  var res = xml.objectToXml(params);
  fn(err, res);
});

Configure a response body parser.

hippie()
.parser(function(bodyfn) {
  var err = new Error('Things went wrong');
  var res = xml.xmlToObject(body);
  fn(err, res);
});

Set request body.

hippie()
.json()
.patch('https://api.mindbloom.com/users/vesln');
.send({ timezone: 'UTC' }
.end();

Set Basic Auth credentials.

hippie()
.auth('user', 'password')
.patch('https://api.mindbloom.com/users/vesln');
.send({ timezone: 'UTC' }
.end();

Register a middleware that will be executed before the HTTP request.

hippie()
.json()
.use(function(optionsnext) {
  // modify the options for `request` here 
  // do other suff 
  next(options);
})
.get('https://api.github.com/users/vesln')
.end(fn);

#get, #del, #post, #put, #patch, #head

Helper method for:

  • Method: method
  • URL: url
  • End: fn [optional]
hippie()
.get('https://api.github.com/users/vesln')
.end(fn);

Or if you want to execute the test immediately:

hippie()
.get('https://api.github.com/users/vesln', fn);

Set a response status code expectation.

hippie()
.json()
.get('https://api.github.com/users/vesln')
.expectStatus(200)
.end(fn);

Set a response header expectation.

hippie()
.json()
.get('https://api.github.com/users/vesln')
.expectHeader('Content-Type', 'application/json; charset=utf-8')
.expectHeader('X-API-LIMIT', 3)
.end(fn);

Register a string path expectation.

hippie()
.json()
.get('https://api.github.com/users/vesln')
.expectValue('details.company', 'Awesome.io')
.expectValue('repos[0].name', 'hippie')
.end(fn);

For more information about string paths visit pathval.

Strict expectations:

hippie()
.get('https://api.github.com/users/vesln')
.expectBody('{ "username": "vesln" }')
.end(fn);

Regular expression expectations:

hippie()
.get('https://api.github.com/users/vesln')
.expectBody(/vesln/)
.end(fn);

Object/array expectations:

hippie()
.get('https://api.github.com/users/vesln')
.expectBody({ username: 'vesln' })
.end(fn);

Register a custom expectation.

hippie()
.get('https://api.github.com/users/vesln')
.expect(function(resbodynext) {
  var err = assertSomething;
  next(err);
})
.end(fn);

Execute the HTTP request and the tests.

hippie()
.json()
.get('https://api.github.com/users/vesln')
.expectValue('details.company', 'Awesome.io')
.expectValue('repos[0].name', 'hippie')
.end(fn);

Fire up an HTTP app and set its address as a base URL. Also works with HTTP handler functions function(req res){}.

hippie(expressApp)
.get('/')
.end(fn);
hippie()
.app(function(reqres) {
  res.end('Bye');
})
.get('/')
.end(fn);
npm install hippie
$ npm test
$ npm run-script coverage

(The MIT License)

Copyright (c) 2013 Veselin Todorov hi@vesln.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.