Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

Commit

Permalink
Bug fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Dec 5, 2018
1 parent 6cc3f58 commit 7ef0c14
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 83 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
110 changes: 64 additions & 46 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@foxify/inject",
"version": "1.0.0",
"version": "1.0.1",
"description": "Fake HTTP injection library",
"author": "Ardalan Amini <ardalanamini22@gmail.com> [https://github.com/ardalanamini]",
"license": "MIT",
Expand Down Expand Up @@ -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": [
Expand All @@ -61,4 +61,4 @@
"<rootDir>/test/**/*\\.ts"
]
}
}
}
48 changes: 34 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const inject = (
...options,
};

opts.method = opts.method.toUpperCase();

let body = opts.body || null;
if (body) {
if (typeof body !== "string") {
Expand Down Expand Up @@ -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");
};
Expand All @@ -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,
Expand All @@ -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,
Expand Down
33 changes: 17 additions & 16 deletions test/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 7ef0c14

Please sign in to comment.