strapi/test/helpers/context.js

44 lines
854 B
JavaScript
Raw Normal View History

2015-10-01 00:30:16 +02:00
'use strict';
const Stream = require('stream');
2015-10-16 15:47:17 +02:00
const Koa = require('../..').server;
2015-10-01 00:30:16 +02:00
2015-10-16 15:47:17 +02:00
module.exports = function (req, res) {
2015-10-01 00:30:16 +02:00
const socket = new Stream.Duplex();
req = req || {
headers: {},
socket: socket,
__proto__: Stream.Readable.prototype
};
res = res || {
_headers: {},
socket: socket,
__proto__: Stream.Writable.prototype
};
res.getHeader = function (k) {
return res._headers[k.toLowerCase()];
};
res.setHeader = function (k, v) {
res._headers[k.toLowerCase()] = v;
};
res.removeHeader = function (k) {
delete res._headers[k.toLowerCase()];
};
2015-10-16 15:47:17 +02:00
return (new Koa()).createContext(req, res);
2015-10-01 00:30:16 +02:00
};
2015-10-16 15:47:17 +02:00
module.exports.request = function (req, res) {
return module.exports(req, res).request;
2015-10-01 00:30:16 +02:00
};
2015-10-16 15:47:17 +02:00
module.exports.response = function (req, res) {
return module.exports(req, res).response;
2015-10-01 00:30:16 +02:00
};