@dkx/http-server
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -11,3 +11,4 @@ /// <reference types="node" /> | ||
state?: RequestState; | ||
onBodyWrite?: (chunk: any) => void; | ||
} | ||
export declare function testMiddleware(middleware: Middleware, options?: MiddlewareTestOptions): Promise<Response>; |
@@ -15,16 +15,26 @@ "use strict"; | ||
function testMiddleware(middleware, options = {}) { | ||
let body; | ||
if (typeof options.body !== 'undefined') { | ||
body = new stream_1.Readable; | ||
body._read = () => { | ||
body.push(new Buffer(options.body)); | ||
body.push(null); | ||
}; | ||
} | ||
const req = new request_1.Request(options.method, options.url, options.headers, body); | ||
const res = new response_1.Response(new response_1.ResponseBodyBag); | ||
const state = typeof options.state === 'undefined' ? {} : options.state; | ||
return middleware(req, res, (res) => __awaiter(this, void 0, void 0, function* () { return res; }), state); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let body; | ||
if (typeof options.body !== 'undefined') { | ||
body = new stream_1.Readable; | ||
body._read = () => { | ||
body.push(new Buffer(options.body)); | ||
body.push(null); | ||
}; | ||
} | ||
const state = typeof options.state === 'undefined' ? {} : options.state; | ||
const bodyBag = new response_1.ResponseBodyBag; | ||
const req = new request_1.Request(options.method, options.url, options.headers, body); | ||
const res = yield middleware(req, new response_1.Response(bodyBag), (res) => __awaiter(this, void 0, void 0, function* () { return res; }), state); | ||
if (typeof options.onBodyWrite !== 'undefined') { | ||
const body = new response_1.ResponseBody((chunk, encoding, done) => { | ||
options.onBodyWrite(chunk); | ||
done(); | ||
}); | ||
bodyBag.attachBody(body); | ||
} | ||
return res; | ||
}); | ||
} | ||
exports.testMiddleware = testMiddleware; | ||
//# sourceMappingURL=middleware.js.map |
{ | ||
"name": "@dkx/http-server", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Minimalistic HTTP server", | ||
@@ -5,0 +5,0 @@ "repository": "git@gitlab.com:dkx/http/server.git", |
@@ -91,2 +91,3 @@ # DKX/Http/Server | ||
{ | ||
response.write('hello world'); | ||
response = response.withHeader('X-Middleware-header', 'lorem ipsum'); | ||
@@ -96,2 +97,3 @@ return next(response); | ||
const data = []; | ||
const response = await testMiddleware(myUselessMiddleware, { | ||
@@ -106,2 +108,5 @@ method: 'POST', | ||
msg: 'My custom shared state' | ||
}, | ||
onBodyWrite: function(chunk) { | ||
data.push(chunk.toString()); | ||
} | ||
@@ -111,2 +116,3 @@ }); | ||
console.log(response.getHeader('X-Middleware-header')); // output: "lorem ipsum" | ||
console.log(data); // output: ["hello world"] | ||
``` | ||
@@ -113,0 +119,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32702
396
277