Socket
Socket
Sign inDemoInstall

hapi-test-request

Package Overview
Dependencies
1
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    hapi-test-request

Hapi request helper for testing


Version published
Weekly downloads
1
Maintainers
1
Install size
829 kB
Created
Weekly downloads
 

Readme

Source

Hapi request test helper

Build Status npm version Dependency Status Follow @trailsjs on Twitter

HTTP assertions made easy for Hapi using Promises

Requirements

This module requires Node.js v4.1+

Usage

Server file should export server server.js:

'use strict';

const Hapi = require('hapi');

// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
    host: 'localhost',
    port: 8000
});

// Add the route
server.route({
    method: 'GET',
    path:'/hello',
    handler: function (request, reply) {
      return reply({
        test: true
      });
    }
});

server.route({
  method: 'GET',
  path: '/api/v1/test',
  handler: function (request, reply) {
    reply({
      test: true
    });
  }
});

// Start the server
server.start((err) => {
    if (err) {
        throw err;
    }
});

// !!!!!!!
module.exports = server;

Example of some.test.js:

const server = require('../server.js');
const request = require('hapi-test-request')(server);

//...
let.it('something', (done) => {
  request.call({
  	method: 'POST',
  	url: '/mail',
  	payload: {
  	  email: 'someemail@email.com'
  	}
  }).then((response) => {
    // ....
    expect(response.statusCode).to.equal(200);
    done();
  });
});

Options for testing

{
  prefix: '/api/v1' // Will add prefix for all requests
}

Example:

const server = require('./server');
const request = require('hapi-test-request');
const lab = require('lab');

lab.it('Some tests', (done) => {
  const config = {
    prefix: '/api/v1'
  };
  request(server, config)
    .call({
      method: 'GET',
      url: '/test' // will be merged with prefix. Will be called: /api/v1/test
    })
    .then((res) => {
      expect(res.statusCode).to.equal(200);
    })
    .then(done)
    .catch(done);
});

License

MIT

Keywords

FAQs

Last updated on 29 Dec 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc