UNPKG

6.2 kBPlain TextView Raw
1import {expect} from "chai";
2import "mocha";
3import * as sinon from "sinon";
4import {AxiosHttpClient} from "../../src/client/HttpClient";
5import {ClientOptions} from "../../src/client/models";
6import {Errors} from "../../src";
7
8describe("AxiosHttpClient", () => {
9 let sandbox: sinon.SinonSandbox;
10 const httpClient = new AxiosHttpClient();
11
12 beforeEach(() => {
13 sandbox = sinon.createSandbox();
14 });
15
16 afterEach(() => {
17 sandbox.restore();
18 });
19
20 it("default error", () => {
21 sandbox.stub(httpClient.client, "request").rejects(new Error("test"));
22
23 return httpClient.httpRequest(ClientOptions.HttpMethod.GET, "", {}, null, {}).then((result) => {
24 throw Error(`Should not be here with result: ${result}`);
25 }, (error) => {
26 expect(error).to.be.instanceOf(Errors.PostmarkError);
27 expect(error.message).to.equal("test");
28 expect(error.code).to.equal(0);
29 expect(error.statusCode).to.equal(0);
30 });
31 });
32
33
34 it("error with no message in it", () => {
35 const errorToThrow: any = { stack: 'Hello stack' };
36 sandbox.stub(httpClient.client, "request").rejects(errorToThrow);
37
38 return httpClient.httpRequest(ClientOptions.HttpMethod.GET, "", {}, null, {}).then((result) => {
39 throw Error(`Should not be here with result: ${result}`);
40 }, (error) => {
41 expect(error).to.be.an.instanceof(Errors.PostmarkError);
42 expect(error.name).to.equal("PostmarkError");
43 expect(error.message).to.equal(JSON.stringify(errorToThrow));
44 });
45 });
46
47 describe("http status code errors", () => {
48 const buildAxiosFormatError = (statusNumber: number) => ({
49 response: { data: { Message: "Basic error", ErrorCode: 0 }, status: statusNumber }
50 });
51
52 it("401", () => {
53 sandbox.stub(httpClient.client, "request").rejects(buildAxiosFormatError(401));
54
55 return httpClient.httpRequest(ClientOptions.HttpMethod.GET, "", {}, null, {}).then((result) => {
56 throw Error(`Should not be here with result: ${result}`);
57 }, (error) => {
58 expect(error).to.be.instanceOf(Errors.InvalidAPIKeyError);
59 });
60 });
61
62 it("404", () => {
63 sandbox.stub(httpClient.client, "request").rejects(buildAxiosFormatError(404));
64
65 return httpClient.httpRequest(ClientOptions.HttpMethod.GET, "", {}, null, {}).then((result) => {
66 throw Error(`Should not be here with result: ${result}`);
67 }, (error) => {
68 expect(error).to.be.instanceOf(Errors.PostmarkError);
69 });
70 });
71
72 it("422", () => {
73 sandbox.stub(httpClient.client, "request").rejects(buildAxiosFormatError(422));
74
75 return httpClient.httpRequest(ClientOptions.HttpMethod.GET, "", {}, null, {}).then((result) => {
76 throw Error(`Should not be here with result: ${result}`);
77 }, (error) => {
78 expect(error).to.be.instanceOf(Errors.ApiInputError);
79 });
80 });
81
82 it("422 - inactive recipients", () => {
83 const errorToThrow = buildAxiosFormatError(422)
84 errorToThrow.response.data.ErrorCode=300
85 sandbox.stub(httpClient.client, "request").rejects(errorToThrow);
86
87 return httpClient.httpRequest(ClientOptions.HttpMethod.GET, "", {}, null, {}).then((result) => {
88 throw Error(`Should not be here with result: ${result}`);
89 }, (error) => {
90 expect(error).to.be.instanceOf(Errors.InvalidEmailRequestError);
91 });
92 });
93
94 it("422 - invalid email", () => {
95 const errorToThrow = buildAxiosFormatError(422)
96 errorToThrow.response.data.ErrorCode=406
97 sandbox.stub(httpClient.client, "request").rejects(errorToThrow);
98
99 return httpClient.httpRequest(ClientOptions.HttpMethod.GET, "", {}, null, {}).then((result) => {
100 throw Error(`Should not be here with result: ${result}`);
101 }, (error) => {
102 expect(error).to.be.instanceOf(Errors.InactiveRecipientsError);
103 });
104 });
105
106 it("429", () => {
107 const errorToThrow = buildAxiosFormatError(429)
108 sandbox.stub(httpClient.client, "request").rejects(errorToThrow);
109
110 return httpClient.httpRequest(ClientOptions.HttpMethod.GET, "", {}, null, {}).then((result) => {
111 throw Error(`Should not be here with result: ${result}`);
112 }, (error) => {
113 expect(error).to.be.instanceOf(Errors.RateLimitExceededError);
114 });
115 });
116
117 it("500", () => {
118 const errorToThrow = buildAxiosFormatError(500)
119 sandbox.stub(httpClient.client, "request").rejects(errorToThrow);
120
121 return httpClient.httpRequest(ClientOptions.HttpMethod.GET, "", {}, null, {}).then((result) => {
122 throw Error(`Should not be here with result: ${result}`);
123 }, (error) => {
124 expect(error).to.be.instanceOf(Errors.InternalServerError);
125 });
126 });
127
128 it("503", () => {
129 const errorToThrow = buildAxiosFormatError(500)
130 sandbox.stub(httpClient.client, "request").rejects(errorToThrow);
131
132 return httpClient.httpRequest(ClientOptions.HttpMethod.GET, "", {}, null, {}).then((result) => {
133 throw Error(`Should not be here with result: ${result}`);
134 }, (error) => {
135 expect(error).to.be.instanceOf(Errors.InternalServerError);
136 });
137 });
138
139 it("unknown status", () => {
140 const errorToThrow = buildAxiosFormatError(-1)
141 sandbox.stub(httpClient.client, "request").rejects(errorToThrow);
142
143 return httpClient.httpRequest(ClientOptions.HttpMethod.GET, "", {}, null, {}).then((result) => {
144 throw Error(`Should not be here with result: ${result}`);
145 }, (error) => {
146 expect(error).to.be.instanceOf(Errors.UnknownError);
147 });
148 });
149 });
150});