|
1 | 1 | import { expect } from "@hapi/code";
|
2 | 2 | import * as Lab from "@hapi/lab";
|
3 | 3 | import HttpError from "../src/HttpError";
|
| 4 | +import {Response} from "cross-fetch"; |
4 | 5 |
|
5 | 6 | export const lab = Lab.script();
|
6 | 7 | const { it, describe } = lab;
|
7 | 8 |
|
8 | 9 | describe("HttpError", () => {
|
9 |
| - it ("create with no args", () => { |
10 |
| - const error = new HttpError(); |
| 10 | + it ("create from text response", async () => { |
| 11 | + const resp = new Response("Test error", { |
| 12 | + status: 400, |
| 13 | + statusText: "Bad Request (test)", |
| 14 | + headers: { |
| 15 | + "content-type": "text/plain" |
| 16 | + } |
| 17 | + }); |
| 18 | + const error = new HttpError(resp); |
11 | 19 | expect(error.name).to.equal("HttpError");
|
12 |
| - expect(error.message).to.equal("Unknown error"); |
13 |
| - expect(error.statusCode).to.equal(0); |
14 |
| - expect(error.status).to.equal(0); |
15 |
| - expect(error.statusText).to.equal("Error"); |
16 |
| - expect(error.body).to.equal(null); |
17 |
| - }); |
18 |
| - |
19 |
| - it ("can set the message", () => { |
20 |
| - const error = new HttpError("Test Error"); |
21 |
| - expect(error.name).to.equal("HttpError"); |
22 |
| - expect(error.message).to.equal("Test Error"); |
23 |
| - expect(error.statusCode).to.equal(0); |
24 |
| - expect(error.status).to.equal(0); |
25 |
| - expect(error.statusText).to.equal("Error"); |
26 |
| - expect(error.body).to.equal(null); |
| 20 | + expect(error.message).to.equal("400 Bad Request (test)\nURL: "); |
| 21 | + expect(error.statusCode).to.equal(400); |
| 22 | + expect(error.status).to.equal(400); |
| 23 | + expect(error.statusText).to.equal("Bad Request (test)"); |
| 24 | + await error.parse(); |
| 25 | + expect(error.message).to.equal("400 Bad Request (test)\nURL: \n\nTest error"); |
27 | 26 | expect(JSON.stringify(error)).to.equal(JSON.stringify({
|
28 | 27 | name : "HttpError",
|
29 |
| - statusCode: 0, |
30 |
| - status : 0, |
31 |
| - statusText: "Error", |
32 |
| - message : "Test Error", |
33 |
| - body : null |
| 28 | + statusCode: 400, |
| 29 | + status : 400, |
| 30 | + statusText: "Bad Request (test)", |
| 31 | + message : "400 Bad Request (test)\nURL: \n\nTest error" |
34 | 32 | }));
|
35 | 33 | });
|
36 | 34 |
|
37 |
| - it ("can set the statusCode", () => { |
38 |
| - const error = new HttpError("Test Error", 234); |
39 |
| - expect(error.name).to.equal("HttpError"); |
40 |
| - expect(error.message).to.equal("Test Error"); |
41 |
| - expect(error.statusCode).to.equal(234); |
42 |
| - expect(error.status).to.equal(234); |
43 |
| - expect(error.statusText).to.equal("Error"); |
44 |
| - expect(error.body).to.equal(null); |
45 |
| - expect(JSON.stringify(error)).to.equal(JSON.stringify({ |
46 |
| - name : "HttpError", |
47 |
| - statusCode: 234, |
48 |
| - status : 234, |
49 |
| - statusText: "Error", |
50 |
| - message : "Test Error", |
51 |
| - body : null |
52 |
| - })); |
| 35 | + it ("parse ignores unknown mime types", async () => { |
| 36 | + const resp = new Response("Test error", { |
| 37 | + status: 400, |
| 38 | + statusText: "Bad Request (test)", |
| 39 | + headers: { |
| 40 | + "content-type": "application/pdf" |
| 41 | + } |
| 42 | + }); |
| 43 | + const error = new HttpError(resp); |
| 44 | + expect(error.message).to.equal("400 Bad Request (test)\nURL: "); |
| 45 | + await error.parse(); |
| 46 | + expect(error.message).to.equal("400 Bad Request (test)\nURL: "); |
53 | 47 | });
|
54 | 48 |
|
55 |
| - it ("can set the statusText", () => { |
56 |
| - const error = new HttpError("Test Error", 234, "Test"); |
| 49 | + it ("create from json response", async () => { |
| 50 | + const resp = new Response('{"x":"y"}', { |
| 51 | + status: 400, |
| 52 | + statusText: "Bad Request (test)", |
| 53 | + headers: { |
| 54 | + "content-type": "application/json" |
| 55 | + } |
| 56 | + }); |
| 57 | + const error = new HttpError(resp); |
57 | 58 | expect(error.name).to.equal("HttpError");
|
58 |
| - expect(error.message).to.equal("Test Error"); |
59 |
| - expect(error.statusCode).to.equal(234); |
60 |
| - expect(error.status).to.equal(234); |
61 |
| - expect(error.statusText).to.equal("Test"); |
62 |
| - expect(error.body).to.equal(null); |
63 |
| - expect(JSON.stringify(error)).to.equal(JSON.stringify({ |
64 |
| - name : "HttpError", |
65 |
| - statusCode: 234, |
66 |
| - status : 234, |
67 |
| - statusText: "Test", |
68 |
| - message : "Test Error", |
69 |
| - body : null |
70 |
| - })); |
| 59 | + expect(error.message).to.equal("400 Bad Request (test)\nURL: "); |
| 60 | + expect(error.statusCode).to.equal(400); |
| 61 | + expect(error.status).to.equal(400); |
| 62 | + expect(error.statusText).to.equal("Bad Request (test)"); |
| 63 | + await error.parse(); |
| 64 | + expect(error.message).to.equal('400 Bad Request (test)\nURL: \n\n{\n "x": "y"\n}'); |
71 | 65 | });
|
72 | 66 |
|
73 |
| - it ("can set the body as text", () => { |
74 |
| - const error = new HttpError("Test Error", 234, "Test", "test body"); |
| 67 | + it ("create from json response having error property", async () => { |
| 68 | + const resp = new Response('{"error":"x"}', { |
| 69 | + status: 400, |
| 70 | + statusText: "Bad Request (test)", |
| 71 | + headers: { |
| 72 | + "content-type": "application/json" |
| 73 | + } |
| 74 | + }); |
| 75 | + const error = new HttpError(resp); |
75 | 76 | expect(error.name).to.equal("HttpError");
|
76 |
| - expect(error.message).to.equal("Test Error"); |
77 |
| - expect(error.statusCode).to.equal(234); |
78 |
| - expect(error.status).to.equal(234); |
79 |
| - expect(error.statusText).to.equal("Test"); |
80 |
| - expect(error.body).to.equal("test body"); |
81 |
| - expect(JSON.stringify(error)).to.equal(JSON.stringify({ |
82 |
| - name : "HttpError", |
83 |
| - statusCode: 234, |
84 |
| - status : 234, |
85 |
| - statusText: "Test", |
86 |
| - message : "Test Error", |
87 |
| - body : "test body" |
88 |
| - })); |
| 77 | + expect(error.message).to.equal("400 Bad Request (test)\nURL: "); |
| 78 | + expect(error.statusCode).to.equal(400); |
| 79 | + expect(error.status).to.equal(400); |
| 80 | + expect(error.statusText).to.equal("Bad Request (test)"); |
| 81 | + await error.parse(); |
| 82 | + expect(error.message).to.equal( |
| 83 | + '400 Bad Request (test)\nURL: \nx' |
| 84 | + ); |
89 | 85 | });
|
90 | 86 |
|
91 |
| - it ("can set the body as object", () => { |
92 |
| - const error = new HttpError("Test Error", 234, "Test", { a: 2 }); |
| 87 | + it ("create from json response having error and error_description properties", async () => { |
| 88 | + const resp = new Response('{"error":"x","error_description":"y"}', { |
| 89 | + status: 400, |
| 90 | + statusText: "Bad Request (test)", |
| 91 | + headers: { |
| 92 | + "content-type": "application/json" |
| 93 | + } |
| 94 | + }); |
| 95 | + const error = new HttpError(resp); |
93 | 96 | expect(error.name).to.equal("HttpError");
|
94 |
| - expect(error.message).to.equal("Test Error"); |
95 |
| - expect(error.statusCode).to.equal(234); |
96 |
| - expect(error.status).to.equal(234); |
97 |
| - expect(error.statusText).to.equal("Test"); |
98 |
| - expect(error.body).to.equal({ a: 2 }); |
99 |
| - expect(JSON.stringify(error)).to.equal(JSON.stringify({ |
100 |
| - name : "HttpError", |
101 |
| - statusCode: 234, |
102 |
| - status : 234, |
103 |
| - statusText: "Test", |
104 |
| - message : "Test Error", |
105 |
| - body : { a: 2 } |
106 |
| - })); |
| 97 | + expect(error.message).to.equal("400 Bad Request (test)\nURL: "); |
| 98 | + expect(error.statusCode).to.equal(400); |
| 99 | + expect(error.status).to.equal(400); |
| 100 | + expect(error.statusText).to.equal("Bad Request (test)"); |
| 101 | + await error.parse(); |
| 102 | + expect(error.message).to.equal( |
| 103 | + '400 Bad Request (test)\nURL: \nx: y' |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it ("parse can be called twice", async () => { |
| 108 | + const error = new HttpError(new Response()); |
| 109 | + await error.parse(); |
| 110 | + await error.parse(); |
107 | 111 | });
|
108 | 112 | });
|
0 commit comments