Skip to content

astur/mockser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mockser

Server to test http clients. Actually it is old good core [http/https].createServer with few improvements.

Build Status NPM version

Why?

Because we need something to test our http-clients, scrapers, bots and so on. On the moment there are three solutions:

  • some people make requests to online endpoints like httpbin. So they need to be online when testing.
  • others use so complicated things as nock with all their pluses and minuses (numerous and well-documented).
  • and in most real cases people write their own http-servers (right in test code or in 'helpers'). Such servers are not properly tested or documented so work with tests became too hard.

This server is easy, handy, tested and don't need more documentation. And it is open to your issues if you need something more.

Features

  • listen and close methods are promisified, so you can just await them in before-all and after-all sections of tests respectively.
  • on every response server emits event named same as responsed path, so just write handlers for routs.
  • by default mocker creates 'http' server but you may call it with options object and create https server, for example with {key, cert} pair of your choice.
  • nothing more. Yes, it's so easy.

Install

npm i mockser

Usage

const mocker = require('mocker');

// Creating server
const server = mocker();
// or
// const sslServer = mocker({key, cert});

// Routing (treat it as usual callback for 'request' event but routed to specific path)
server.on('/test', (req, res) => {
    res.end('ok');
});
server.on('/404', (req, res) => {
    res.statusCode = 404;
    res.end('Not Found');
});

// Let it be top-level-await REPL here or do the same inside async function

// Start server
await server.listen(3000);

// Testing
const goodResponse = await httpClient('http://localhost:3000/test'); // 200 - ok
const badResponse = await httpClient('http://localhost:3000/404'); // 404 - Not Found

// Cleanup
await server.close();

See working example in test

License

MIT

About

Server to test http clients

Resources

License

Stars

Watchers

Forks

Packages

No packages published