Nana Plays Minesweeper
Learn how companies like Phone2Action are using npm in their front-end developmentJoin the conversation »

@medley/self-request

0.4.0 • Public • Published

@medley/self-request

npm Version Build Status Coverage Status dependencies Status

A Medley plugin that augments an app to be able to make HTTP requests to itself for testing purposes.

It adds a request method to the app that will start the app server and make HTTP requests to it using got.

Installation

npm install @medley/self-request --save-dev
# or 
yarn add @medley/self-request --dev

Usage

app.js

const medley = require('@medley/medley');
const app = medley();
 
app.get('/', (req, res) => {
  res.send('Hello');
});
 
module.exports = app;

test.js

const assert = require('assert').strict;
 
function buildApp() {
  const app = require('./app');
 
  app.register(require('@medley/self-request'));
 
  return app;
}
 
describe('app', () => {
  it('should say Hello', async () => {
    const app = buildApp();
    const res = await app.request('/');
 
    assert.equal(res.statusCode, 200);
    assert.equal(res.body, 'Hello');
  });
});

API

app.request([url], [options])

Returns a Promise for a got response object.

Automatically calls app.listen() if the server is not already listening.

url

Type: string

The route URL to request.

app.request('/hello');
options

Type: Object

Any of the got options.

app.request('/hello', {
  method: 'POST',
  body: 'Greetings',
});
 
app.request({
  url: '/hello',
  method: 'POST',
  body: 'Greetings',
});

Plugin Options

basePath

Type: string
Default: app.basePath

A path prefix that will be prepended to all requests.

const medley = require('@medley/medley');
const app = medley();
 
app.get('/v1/hello', (req, res) => {
  res.send('Hello');
});
 
app.register(require('@medley/self-request'), {
  basePath: '/v1',
});
 
(async () => {
  const res = await app.request('/hello');
  console.log(res.body); // -> 'Hello'
})();

Since the default is app.basePath, testing sub-apps is trivial:

const medley = require('@medley/medley');
const app = medley();
 
const v1Routes = app.createSubApp('/v1');
 
v1Routes.get('/hello', (req, res) => {
  res.send('Hello');
});
 
v1Routes.register(require('@medley/self-request'));
 
(async () => {
  const res = await v1Routes.request('/hello');
  console.log(res.body); // -> 'Hello'
})();

gotDefaults

Type: Object

A set of got options that will be used as the defaults for each request.

These options will be merged in with the following defaults set by self-request:

{
  retry: 0,
  timeout: 2000,
  followRedirect: false,
  throwHttpErrors: false,
  rejectUnauthorized: false,
}

Example:

const medley = require('@medley/medley');
const app = medley();
 
app.get('/', (req, res) => {
  res.send('Hello');
});
 
app.register(require('@medley/self-request'), {
  gotDefaults: {
    encoding: null,
  },
});
 
(async () => {
  const res = await app.request('/hello');
  console.log(res.body); // -> <Buffer 48 65 6c 6c 6f>
})();

Note: The baseUrl got option cannot be used since it is automatically set by this plugin.

Install

npm i @medley/self-request

DownloadsWeekly Downloads

10

Version

0.4.0

License

MIT

Unpacked Size

7.35 kB

Total Files

4

Last publish

Collaborators

  • avatar