Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
188 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const head = require("../../../index").node.middleware.head | ||
const Promise = require("bluebird") | ||
|
||
describe("Middleware: head", () => { | ||
|
||
it("modifies any HEAD request to be a GET, stripping response body, and changing back to GET when done", (done) => { | ||
const handler = (request) => { | ||
expect(request.method).toBe("GET") | ||
return Promise.resolve({ | ||
status: 200, | ||
headers: { a: 123 }, | ||
body: "Hi" | ||
}) | ||
} | ||
const req = { method: "HEAD" } | ||
head(handler)(req).then((response) => { | ||
expect(req.method).toBe("HEAD") | ||
expect(response.status).toBe(200) | ||
expect(response.headers).toEqual({ | ||
a: 123 | ||
}) | ||
expect(response.body).toBe(undefined) | ||
done() | ||
}) | ||
}) | ||
|
||
it("when stripping, it closes a stream body", () => { | ||
pending() | ||
}) | ||
|
||
it("doesn't touch other requests", (done) => { | ||
const handler = (request) => { | ||
return { | ||
status: 200, | ||
headers: { a: 123 }, | ||
body: "ok" | ||
} | ||
} | ||
|
||
let result = head(handler)({ method: "GET" }) | ||
expect(result).toEqual({ status: 200, headers: { a: 123 }, body: "ok" }) | ||
|
||
result = head(handler)({ method: "hEad" }) | ||
expect(result).toEqual({ status: 200, headers: { a: 123 }, body: "ok" }) | ||
|
||
result = head(handler)({ method: "POST" }) | ||
expect(result).toEqual({ status: 200, headers: { a: 123 }, body: "ok" }) | ||
|
||
done() | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const log = require("../../../index").node.middleware.log | ||
const Promise = require("bluebird") | ||
|
||
describe("Middleware: log", () => { | ||
const orig_log = console.log | ||
|
||
afterEach(() => { | ||
console.log = orig_log | ||
}) | ||
|
||
it("outputs a request", (done) => { | ||
let called = 0 | ||
console.log = (prefix, method, url, status, time) => { | ||
expect(method).toBe("TEST") | ||
expect(url).toBe("/test/path") | ||
|
||
if (!called) { | ||
called = 1 | ||
expect(status).toBe(undefined) | ||
expect(time).toBe(undefined) | ||
return | ||
} | ||
|
||
const ms = parseInt(time.substr(0, time.length)) | ||
expect(ms).not.toBe(NaN) | ||
// the time it took to run the test should | ||
// at least be 3ms but probably not greater than 7ms | ||
if (ms < 3 && ms > 7) { | ||
throw new Error("time in milliseconds seems unlikely") | ||
} | ||
called = 2 | ||
} | ||
|
||
const handler = () => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve({ status: 123, headers: {}, body: "hi" }) | ||
}, 3) | ||
}) | ||
} | ||
|
||
log(handler)({ method: "TEST", url: "/test/path" }).then((response) => { | ||
expect(response).toEqual({ | ||
status: 123, | ||
headers: {}, | ||
body: "hi" | ||
}) | ||
expect(called).toBe(2) | ||
done() | ||
}) | ||
}) | ||
|
||
it("visual test", (done) => { | ||
console.log("\nvisual test for log middleware (4 lines will be outputted):") | ||
let called = false | ||
|
||
const handler = () => { | ||
return new Promise((resolve, reject) => { | ||
if (called) { | ||
return reject("simulating error") | ||
} | ||
|
||
called = true | ||
setTimeout(() => { | ||
resolve({ status: 123, headers: {}, body: "hi" }) | ||
}, 3) | ||
}) | ||
} | ||
|
||
log(handler)({ method: "TEST", url: "/test/path" }).then(() => { | ||
log(handler)({ method: "TEST", url: "/test/path" }).catch(done) | ||
}) | ||
}) | ||
|
||
it("does not do anything if production env set", (done) => { | ||
process.env.NODE_ENV = "production" | ||
console.log = () => { | ||
throw new Error("should not be called") | ||
} | ||
const handler = () => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve(123) | ||
}, 3) | ||
}) | ||
} | ||
log(handler)({ method: "TEST", url: "/test/path" }).then((resp) => { | ||
expect(resp).toBe(123) | ||
process.env.NODE_ENV = "" | ||
done() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,40 @@ | ||
/* | ||
* A simple logger for reporting the time in milliseconds | ||
* the amount of time elapsed in responding to a request | ||
* | ||
* It is meant purely for development | ||
*/ | ||
|
||
//const arrow_in = "\u001b[33m=\u001b[93m>\u001b[39m" | ||
//const arrow_out = "\u001b[92m<\u001b[32m=\u001b[39m" | ||
|
||
const prefix_in = "⇢ " | ||
const prefix_out = "↩︎ " | ||
const prefix_err = "⚠︎ " | ||
|
||
module.exports = (handler) => { | ||
if (typeof process !== "undefined" && typeof process.env !== "undefined" && process.env.NODE_ENV === "production") { | ||
if (typeof process !== "undefined" | ||
&& typeof process.env !== "undefined" | ||
&& process.env.NODE_ENV === "production") { | ||
return (request) => { | ||
return handler(request) | ||
} | ||
} | ||
|
||
return (request) => { | ||
const t = new Date() | ||
return handler(request).then((response) => { | ||
const diff = new Date - t | ||
console.log(request.method, request.url, diff + "ms") | ||
return response | ||
}) | ||
console.log(prefix_in, request.method, request.url) | ||
|
||
return handler(request) | ||
.then((response) => { | ||
const diff = new Date - t | ||
console.log(prefix_out, request.method, request.url, response.status, diff + "ms") | ||
return response | ||
}) | ||
.catch((err) => { | ||
const diff = new Date - t | ||
console.log(prefix_err, request.method, request.url, "ERR", diff + "ms") | ||
throw err | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters