http-hash-mocker
v2.0.0
Published
create mock data with http hash
Downloads
4
Readme
http-hash-mocker
http-hash-mocker create request handler
import { HttpRequest, HttpResponse } from "node.http"
type RoutePattern : String
type MockerOpts : {
basedir: String,
rootdir?: String,
locator?: String,
routes?: Array<RoutePattern>,
autoGenerate?: Boolean,
template?: String,
}
type Router : (
req: HttpReqest,
res: HttpResponse,
opts: Object,
cb: Callback<Error, void>
) => void
type Mocker : { router: HttpHashRouter } & Router
http-hash-mocker : (mopts: MockerOpts) => Mocker
const http = require('http');
const path = require('path');
const sendError = require('send-data/error');
const mocker = require('http-hash-mocker')({
basedir: path.resolve(__dirname, '../')
// rootdir: '/',
// locator: 'test/fixtures',
routes: [
'/api/photo/:photoid'
],
autoGenerate: true,
template: `
module.exports = {
statusCode: 200,
body: 'hello data template',
};
`,
});
const server = http.createServer(function (req, res) {
mocker(req, res, {}, function (err) {
if (err) {
if (!res.finished) {
sendError(req, res, { body: err });
}
}
});
});
test/fixtures/api/photo/200.js
module.exports = {
statusCode: 200,
body: 'OK',
};
test/fixtures/api/photo/:photoid.js
const sendPlain = require('send-data/plain');
module.exports = function (req, res, opts, cb) {
sendPlain(req, res, 'Nine Nine Nine', cb);
};
you mock it.