koa-lite/test/helpers/context.js

22 lines
784 B
JavaScript
Raw 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
module.exports = (req, res) => {
const socket = new Stream.Duplex();
req = req || { headers: {}, socket: socket, __proto__: Stream.Readable.prototype };
res = res || { _headers: {}, socket: socket, __proto__: Stream.Writable.prototype };
2015-12-02 15:13:01 +00:00
req.socket = req.socket || socket;
res.socket = res.socket || socket;
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()];
2015-10-13 06:19:42 +00:00
return (new Koa()).createContext(req, res);
2015-10-12 20:36:41 +00:00
};
2013-08-17 07:15:57 +00:00
module.exports.request = (req, res) => module.exports(req, res).request;
module.exports.response = (req, res) => module.exports(req, res).response;