koa-lite/test/helpers/context.js

22 lines
811 B
JavaScript
Raw Permalink Normal View History

2013-08-17 07:15:57 +00:00
'use strict';
const Stream = require('stream');
2015-10-13 06:19:42 +00:00
const Koa = require('../..');
2013-08-17 07:15:57 +00:00
2016-04-04 02:30:06 +00:00
module.exports = (req, res, app) => {
const socket = new Stream.Duplex();
2016-04-04 02:30:06 +00:00
req = Object.assign({ headers: {}, socket }, Stream.Readable.prototype, req);
res = Object.assign({ _headers: {}, socket }, Stream.Writable.prototype, res);
req.socket.remoteAddress = req.socket.remoteAddress || '127.0.0.1';
app = app || new Koa();
res.getHeader = k => res._headers[k.toLowerCase()];
res.setHeader = (k, v) => res._headers[k.toLowerCase()] = v;
res.removeHeader = (k, v) => delete res._headers[k.toLowerCase()];
2016-04-04 02:30:06 +00:00
return app.createContext(req, res);
2015-10-12 20:36:41 +00:00
};
2013-08-17 07:15:57 +00:00
2016-04-04 02:30:06 +00:00
module.exports.request = (req, res, app) => module.exports(req, res, app).request;
2016-04-04 02:30:06 +00:00
module.exports.response = (req, res, app) => module.exports(req, res, app).response;