| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | 1 | "use strict"; | |
| 2 | + import errorHandler from "./errorHandler"; | ||
| 2 | 3 | import hello from "./hello"; | |
| 3 | 4 | import nosniff from "./nosniff"; | |
| 4 | 5 | import assert from "assert"; | |
@@ -7,6 +8,7 @@ import http from "http"; | |||
| 7 | 8 | import fetch from "node-fetch"; | |
| 8 | 9 | const responseText = "response text"; | |
| 9 | 10 | var app = connect(); | |
| 11 | + app.use(errorHandler()); | ||
| 10 | 12 | app.use(nosniff()); | |
| 11 | 13 | app.use(hello(responseText)); | |
| 12 | 14 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,8 @@ | |||
| 1 | + "use strict"; | ||
| 2 | + export default function errorHandler() { | ||
| 3 | + return function (err, req, res, next) { | ||
| 4 | + console.error(err.stack); | ||
| 5 | + res.status(500).send(err.message); | ||
| 6 | + next(); | ||
| 7 | + }; | ||
| 8 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,23 +2,45 @@ | |||
| 2 | 2 | "use strict"; | |
| 3 | 3 | import assert from "power-assert"; | |
| 4 | 4 | import connect from "connect"; | |
| 5 | + import errorHandler from "../../src/connect/errorHandler"; | ||
| 5 | 6 | import nosniff from "../../src/connect/nosniff"; | |
| 6 | 7 | import hello from "../../src/connect/hello"; | |
| 7 | 8 | import http from "http"; | |
| 8 | 9 | import fetch from "node-fetch"; | |
| 9 | 10 | describe("connect", function () { | |
| 10 | 11 | var responseText = "test"; | |
| 11 | 12 | var server; | |
| 12 | - before(function (done) { | ||
| 13 | - var app = connect(); | ||
| 14 | - app.use(nosniff()); | ||
| 15 | - app.use(hello(responseText)); | ||
| 16 | - server = http.createServer(app).listen(3000, done); | ||
| 17 | - }); | ||
| 18 | - after(function () { | ||
| 19 | - server.close(); | ||
| 13 | + describe("errorHandler", function () { | ||
| 14 | + beforeEach(function (done) { | ||
| 15 | + var app = connect(); | ||
| 16 | + app.use(errorHandler()); | ||
| 17 | + app.use((req, res, next) => { | ||
| 18 | + next(new Error("wrong")); | ||
| 19 | + }); | ||
| 20 | + server = http.createServer(app).listen(3000, done); | ||
| 21 | + }); | ||
| 22 | + afterEach(function () { | ||
| 23 | + server && server.close(); | ||
| 24 | + }); | ||
| 25 | + it("should return 500 status response", function () { | ||
| 26 | + return fetch("http://localhost:3000") | ||
| 27 | + .then(res => res.status) | ||
| 28 | + .then(status => { | ||
| 29 | + assert(status, 500); | ||
| 30 | + }); | ||
| 31 | + }); | ||
| 32 | + | ||
| 20 | 33 | }); | |
| 21 | 34 | describe("hello", function () { | |
| 35 | + beforeEach(function (done) { | ||
| 36 | + var app = connect(); | ||
| 37 | + app.use(errorHandler()); | ||
| 38 | + app.use(hello(responseText)); | ||
| 39 | + server = http.createServer(app).listen(3000, done); | ||
| 40 | + }); | ||
| 41 | + afterEach(function () { | ||
| 42 | + server && server.close(); | ||
| 43 | + }); | ||
| 22 | 44 | it("should return response text", function () { | |
| 23 | 45 | return fetch("http://localhost:3000") | |
| 24 | 46 | .then(res => res.text()) | |
@@ -28,6 +50,15 @@ describe("connect", function () { | |||
| 28 | 50 | }); | |
| 29 | 51 | }); | |
| 30 | 52 | describe("sniff", function () { | |
| 53 | + beforeEach(function (done) { | ||
| 54 | + var app = connect(); | ||
| 55 | + app.use(nosniff()); | ||
| 56 | + app.use(hello(responseText)); | ||
| 57 | + server = http.createServer(app).listen(3000, done); | ||
| 58 | + }); | ||
| 59 | + afterEach(function () { | ||
| 60 | + server && server.close(); | ||
| 61 | + }); | ||
| 31 | 62 | it("should return response has `X-Content-Type-Options` header", function () { | |
| 32 | 63 | return fetch("http://localhost:3000") | |
| 33 | 64 | .then(res => { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments