diff --git a/README.md b/README.md index 0cc5b21..01b6496 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ const dispatch = function (req, res) { res.end(reply) } +// Nah, you don't need this anymore :) // const server = http.createServer(dispatch) inject(dispatch, { method: 'get', url: '/' }, (err, res) => { diff --git a/package-lock.json b/package-lock.json index 13fc68d..71e1370 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@foxify/inject", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -33,38 +33,55 @@ } }, "@fimbul/bifrost": { - "version": "0.11.0", - "resolved": "https://registry.npmjs.org/@fimbul/bifrost/-/bifrost-0.11.0.tgz", - "integrity": "sha512-GspMaQafpaUoXWWOUgNLQ4vsV52tIHUt0zpKPeJUYEyMvOSp7FIcZ1eQa7SK3GTusrEiksjMrDX/fwanigC3nQ==", + "version": "0.15.0", + "resolved": "https://registry.npmjs.org/@fimbul/bifrost/-/bifrost-0.15.0.tgz", + "integrity": "sha512-sHTwnwA9YhxcVEJkBlfKH1KLmGQGnNYPxk+09w5NnkXelYiiP8a5f351weYfxG0CUPLt1Fgkha20Y/9+jhjn/Q==", "dev": true, "requires": { - "@fimbul/ymir": "^0.11.0", - "get-caller-file": "^1.0.2", + "@fimbul/ymir": "^0.15.0", + "get-caller-file": "^2.0.0", "tslib": "^1.8.1", - "tsutils": "^2.24.0" + "tsutils": "^3.1.0" + }, + "dependencies": { + "get-caller-file": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.0.tgz", + "integrity": "sha512-cF41L/f/7nXpSwMMHMY0FIurpTPZq/eHwJdeh2+0kKYhL9eD7RqsgMujd3qdqvWdjGIHjwvd/iEMTNECl2EhzA==", + "dev": true + }, + "tsutils": { + "version": "3.5.2", + "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-3.5.2.tgz", + "integrity": "sha512-qIlklNuI/1Dzfm+G+kJV5gg3gimZIX5haYtIVQe7qGyKd7eu8T1t1DY6pz4Sc2CGXAj9s1izycctm9Zfl9sRuQ==", + "dev": true, + "requires": { + "tslib": "^1.8.1" + } + } } }, "@fimbul/ymir": { - "version": "0.11.0", - "resolved": "https://registry.npmjs.org/@fimbul/ymir/-/ymir-0.11.0.tgz", - "integrity": "sha512-aIYQMCWbBXe7DIofgu+4DLCPDCfqbKhPjBg4ajskJdq6CAJgySz6KyhGLNnKiDYZMF93ZsaEB/y3SafyMi98Mg==", + "version": "0.15.0", + "resolved": "https://registry.npmjs.org/@fimbul/ymir/-/ymir-0.15.0.tgz", + "integrity": "sha512-Ow0TfxxQ65vIktHcZyXHeDsGKuzJ9Vt6y77R/aOrXQXLMdYHG+XdbiUWzQbtaGOmNzYVkQfINiFnIdvn5Bn24g==", "dev": true, "requires": { - "inversify": "^4.10.0", + "inversify": "^5.0.0", "reflect-metadata": "^0.1.12", "tslib": "^1.8.1" } }, "@types/jest": { - "version": "23.3.3", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-23.3.3.tgz", - "integrity": "sha512-G6EBrbjWDfmIpYu8UcRBOhwtDiYaLj5N5jUR5rx0YvbKxRBhXPZVLUmtfShewSUNKiQwpHavpML69a2WMbIlEQ==", + "version": "23.3.10", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-23.3.10.tgz", + "integrity": "sha512-DC8xTuW/6TYgvEg3HEXS7cu9OijFqprVDXXiOcdOKZCU/5PJNLZU37VVvmZHdtMiGOa8wAA/We+JzbdxFzQTRQ==", "dev": true }, "@types/node": { - "version": "10.11.4", - "resolved": "https://registry.npmjs.org/@types/node/-/node-10.11.4.tgz", - "integrity": "sha512-ojnbBiKkZFYRfQpmtnnWTMw+rzGp/JiystjluW9jgN3VzRwilXddJ6aGQ9V/7iuDG06SBgn7ozW9k3zcAnYjYQ==", + "version": "10.12.12", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.12.tgz", + "integrity": "sha512-Pr+6JRiKkfsFvmU/LK68oBRCQeEg36TyAbPhc2xpez24OOZZCuoIhWGTd39VZy6nGafSbxzGouFPTFD/rR1A0A==", "dev": true }, "abab": { @@ -897,12 +914,12 @@ } }, "bs-logger": { - "version": "0.2.5", - "resolved": "https://registry.npmjs.org/bs-logger/-/bs-logger-0.2.5.tgz", - "integrity": "sha512-uFLE0LFMxrH8Z5Hd9QgivvRbrl/NFkOTHzGhlqQxsnmx5JBLrp4bc249afLL+GccyY/8hkcGi2LpVaOzaEY0nQ==", + "version": "0.2.6", + "resolved": "https://registry.npmjs.org/bs-logger/-/bs-logger-0.2.6.tgz", + "integrity": "sha512-pd8DCoxmbgc7hyPKOvxtqNcjYoOsABPQdcCUjGp3d42VR2CX1ORhk2A87oqqu5R1kk+76nsxZupkmyd+MVtCog==", "dev": true, "requires": { - "fast-json-stable-stringify": "^2.0.0" + "fast-json-stable-stringify": "2.x" } }, "bser": { @@ -2513,9 +2530,9 @@ } }, "inversify": { - "version": "4.13.0", - "resolved": "https://registry.npmjs.org/inversify/-/inversify-4.13.0.tgz", - "integrity": "sha512-O5d8y7gKtyRwrvTLZzYET3kdFjqUy58sGpBYMARF13mzqDobpfBXVOPLH7HmnD2VR6Q+1HzZtslGvsdQfeb0SA==", + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/inversify/-/inversify-5.0.1.tgz", + "integrity": "sha512-Ieh06s48WnEYGcqHepdsJUIJUXpwH5o5vodAX+DK2JA/gjy4EbEcQZxw+uFfzysmKjiLXGYwNG3qDZsKVMcINQ==", "dev": true }, "invert-kv": { @@ -3532,9 +3549,9 @@ } }, "merge": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/merge/-/merge-1.2.0.tgz", - "integrity": "sha1-dTHjnUlJwoGma4xabgJl6LBYlNo=", + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/merge/-/merge-1.2.1.tgz", + "integrity": "sha512-VjFo4P5Whtj4vsLzsYBu5ayHhoHJ0UqNm7ibvShmbmoz7tGi0vXaoJbGdB+GmDMLUdg8DpQXEIeVDAe8MaABvQ==", "dev": true }, "merge-stream": { @@ -5220,9 +5237,9 @@ "dev": true }, "ts-jest": { - "version": "23.10.3", - "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-23.10.3.tgz", - "integrity": "sha512-Lgyfw1MYPfqAs1qrFBmqXu8LRrde8ItH70pmp1iQuRbkVXaap7QcaEpN+yiSxuppfvO8rqezVv8wOYZkKhR5wA==", + "version": "23.10.5", + "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-23.10.5.tgz", + "integrity": "sha512-MRCs9qnGoyKgFc8adDEntAOP64fWK1vZKnOYU1o2HxaqjdJvGqmkLCPCnVq1/If4zkUmEjKPnCiUisTrlX2p2A==", "dev": true, "requires": { "bs-logger": "0.x", @@ -5231,6 +5248,7 @@ "json5": "2.x", "make-error": "1.x", "mkdirp": "0.x", + "resolve": "1.x", "semver": "^5.5", "yargs-parser": "10.x" }, @@ -5299,25 +5317,25 @@ } }, "tslint-config-airbnb": { - "version": "5.11.0", - "resolved": "https://registry.npmjs.org/tslint-config-airbnb/-/tslint-config-airbnb-5.11.0.tgz", - "integrity": "sha512-o2FhaQtxXi6FQ1v0T2n/rACNos6PhuKRmvemMpWxI+9NJn2OOlJ3+OtEmnCdoF7GPXT3Eyk+Q0q4P96flrPl3w==", + "version": "5.11.1", + "resolved": "https://registry.npmjs.org/tslint-config-airbnb/-/tslint-config-airbnb-5.11.1.tgz", + "integrity": "sha512-hkaittm2607vVMe8eotANGN1CimD5tor7uoY3ypg2VTtEcDB/KGWYbJOz58t8LI4cWSyWtgqYQ5F0HwKxxhlkQ==", "dev": true, "requires": { - "tslint-consistent-codestyle": "^1.13.3", + "tslint-consistent-codestyle": "^1.14.1", "tslint-eslint-rules": "^5.4.0", - "tslint-microsoft-contrib": "~5.2.0" + "tslint-microsoft-contrib": "~5.2.1" } }, "tslint-consistent-codestyle": { - "version": "1.13.3", - "resolved": "https://registry.npmjs.org/tslint-consistent-codestyle/-/tslint-consistent-codestyle-1.13.3.tgz", - "integrity": "sha512-+ocXSNGHqUCUyTJsPhS7xqcC3qf6FyP4vd1jEaXaWaJ5NNN36gKZhqNt3nAWH/YgSV0tYaapjSWMbJQJmn/5MQ==", + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/tslint-consistent-codestyle/-/tslint-consistent-codestyle-1.14.1.tgz", + "integrity": "sha512-UxGRX2fF5LpZtqYpuPFaIva+2D7ASX3pTVw41yis6Hmw7PPA3cBnFEX1jqRsnyxGrca6mHxz7xDnwCHtOjWJMQ==", "dev": true, "requires": { - "@fimbul/bifrost": "^0.11.0", + "@fimbul/bifrost": "^0.15.0", "tslib": "^1.7.1", - "tsutils": "^2.27.0" + "tsutils": "^2.29.0" } }, "tslint-eslint-rules": { @@ -5338,9 +5356,9 @@ "dev": true }, "tsutils": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-3.0.0.tgz", - "integrity": "sha512-LjHBWR0vWAUHWdIAoTjoqi56Kz+FDKBgVEuL+gVPG/Pv7QW5IdaDDeK9Txlr6U0Cmckp5EgCIq1T25qe3J6hyw==", + "version": "3.5.2", + "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-3.5.2.tgz", + "integrity": "sha512-qIlklNuI/1Dzfm+G+kJV5gg3gimZIX5haYtIVQe7qGyKd7eu8T1t1DY6pz4Sc2CGXAj9s1izycctm9Zfl9sRuQ==", "dev": true, "requires": { "tslib": "^1.8.1" @@ -5403,9 +5421,9 @@ } }, "typescript": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.1.1.tgz", - "integrity": "sha512-Veu0w4dTc/9wlWNf2jeRInNodKlcdLgemvPsrNpfu5Pq39sgfFjvIIgTsvUHCoLBnMhPoUA+tFxsXjU6VexVRQ==", + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.2.1.tgz", + "integrity": "sha512-jw7P2z/h6aPT4AENXDGjcfHTu5CSqzsbZc6YlUIebTyBAq8XaKp78x7VcSh30xwSCcsu5irZkYZUSFP1MrAMbg==", "dev": true }, "uglify-js": { diff --git a/package.json b/package.json index 20cd466..abfcd9b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@foxify/inject", - "version": "1.0.0", + "version": "1.0.1", "description": "Fake HTTP injection library", "author": "Ardalan Amini [https://github.com/ardalanamini]", "license": "MIT", @@ -39,14 +39,14 @@ "readable-stream": "^3.0.6" }, "devDependencies": { - "@types/jest": "^23.3.3", - "@types/node": "^10.11.4", + "@types/jest": "^23.3.10", + "@types/node": "^10.12.12", "codecov": "^3.1.0", "jest": "^23.6.0", - "ts-jest": "^23.10.3", + "ts-jest": "^23.10.5", "tslint": "^5.11.0", - "tslint-config-airbnb": "^5.11.0", - "typescript": "^3.1.1" + "tslint-config-airbnb": "^5.11.1", + "typescript": "^3.2.1" }, "jest": { "moduleFileExtensions": [ @@ -61,4 +61,4 @@ "/test/**/*\\.ts" ] } -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index 18357e5..a4c300d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -56,6 +56,8 @@ const inject = ( ...options, }; + opts.method = opts.method.toUpperCase(); + let body = opts.body || null; if (body) { if (typeof body !== "string") { @@ -102,32 +104,50 @@ const inject = ( remoteAddress: opts.remoteAddress, } as any; - // req._read = function _read() { - // setImmediate(() => { - // if ((this as any)._inject.body) { - // this.push((this as any)._inject.body); - // } + req._read = function _read() { + setImmediate(() => { + if ((this as any)._inject.body) { + this.push((this as any)._inject.body); + } - // this.emit("close"); + this.emit("close"); - // this.push(null); - // }); - // }; + this.push(null); + }); + }; const res = new opts.ServerResponse(req); res.assignSocket(socket); + (res as any)._inject = { + bodyChinks: [], + headers: {}, + }; + + (res as any)._headers = {}; + + const resWrite = res.write; + res.write = function write( + data: any, + encoding?: string | ((error: Error | null | undefined) => void), + cb?: (error: Error | null | undefined) => void, + ) { + resWrite.call(this, data, encoding as any, cb); + (this as any)._inject.bodyChinks + .push(Buffer.from(data, typeof encoding === "string" ? encoding : "utf8")); + return true; + }; + const resEnd = res.end; res.end = function end( chunk?: string | (() => void), encoding?: string | (() => void), cb?: () => void, ) { - if (chunk && typeof chunk !== "function") (this as any).body = chunk; - else (this as any).body = ""; + if (chunk && typeof chunk !== "function") this.write(chunk, encoding as any); - resEnd.call(res, chunk, encoding, cb); + resEnd.call(this, chunk, encoding as any, cb); this.emit("finish"); }; @@ -137,7 +157,7 @@ const inject = ( res.connection.once("error", callback); const cb = () => callback(null, { - body: (res as any).body, + body: Buffer.concat((res as any)._inject.bodyChinks).toString(), headers: res.getHeaders(), raw: { req, @@ -157,7 +177,7 @@ const inject = ( res.connection.once("error", reject); const cb = () => resolve({ - body: (res as any).body, + body: Buffer.concat((res as any)._inject.bodyChinks).toString(), headers: res.getHeaders(), raw: { req, diff --git a/test/callback.ts b/test/callback.ts index 47f787e..b9bd3a1 100644 --- a/test/callback.ts +++ b/test/callback.ts @@ -115,19 +115,20 @@ test("returns payload 6", () => { }); }); -// test("returns payload 7", () => { -// expect.assertions(3); - -// const dispatch = (req: http.IncomingMessage, res: http.ServerResponse) => { -// res.writeHead(200, { "content-type": req.headers["content-type"] }); -// req.pipe(res); -// }; - -// const body = { hello: "world" }; - -// inject(dispatch, { body, method: "POST", url: "/hello" }, (err, res) => { -// expect(err).toBe(null); -// expect(res.headers["content-type"]).toBe("application/json"); -// expect(res.body).toBe(JSON.stringify(body)); -// }); -// }); +test("returns payload 7", (done) => { + expect.assertions(3); + + const dispatch = (req: http.IncomingMessage, res: http.ServerResponse) => { + res.writeHead(200, { "Content-Type": req.headers["content-type"] }); + req.pipe(res); + }; + + const body = { hello: "world" }; + + inject(dispatch, { body, method: "POST", url: "/hello" }, (err, res) => { + expect(err).toBe(null); + expect(res.headers["content-type"]).toBe("application/json"); + expect(res.body).toBe(JSON.stringify(body)); + done(); + }); +});