1 | import * as postmark from "../../src/index";
|
2 |
|
3 | import {expect} from "chai";
|
4 | import "mocha";
|
5 |
|
6 | import * as nconf from "nconf";
|
7 | import * as sinon from 'sinon';
|
8 |
|
9 | const testingKeys = nconf.env().file({file: __dirname + "/../../testing_keys.json"});
|
10 |
|
11 | describe("ServerClient - Errors", () => {
|
12 | let client: postmark.ServerClient;
|
13 | const serverToken: string = testingKeys.get("SERVER_TOKEN");
|
14 |
|
15 | beforeEach(() => {
|
16 | client = new postmark.ServerClient(serverToken);
|
17 | });
|
18 |
|
19 | const invalidTokenError = "InvalidAPIKeyError";
|
20 |
|
21 | let sandbox: sinon.SinonSandbox;
|
22 |
|
23 | beforeEach(() => {
|
24 | sandbox = sinon.createSandbox();
|
25 | });
|
26 |
|
27 | afterEach(() => {
|
28 | sandbox.restore();
|
29 | });
|
30 |
|
31 | describe("handling errors", () => {
|
32 | const serverToken: string = testingKeys.get("SERVER_TOKEN");
|
33 | const client: postmark.ServerClient = new postmark.ServerClient(serverToken);
|
34 |
|
35 | describe("promise error", () => {
|
36 | it("instance", () => {
|
37 | sandbox.stub(client.httpClient, "request").rejects({ message: "Basic error", response: {data: "Basic error" }});
|
38 |
|
39 | return client.getServer().then((result) => {
|
40 | return result;
|
41 | }, (error) => {
|
42 | expect(error).to.be.instanceOf(postmark.Errors.PostmarkError);
|
43 | });
|
44 | });
|
45 |
|
46 | it("message", () => {
|
47 | sandbox.stub(client.httpClient, "request").rejects({ message: "Basic error", response: {data: "Basic error" }});
|
48 |
|
49 | return client.getServer().then((result) => {
|
50 | return result;
|
51 | }, (error) => {
|
52 | expect(error.message).to.equal("Basic error");
|
53 | });
|
54 | });
|
55 |
|
56 | it("name", () => {
|
57 | sandbox.stub(client.httpClient, "request").rejects({ response: { data: 'response', status: 401} });
|
58 |
|
59 | return client.getBounces().then((result) => {
|
60 | return result;
|
61 | }, (error) => {
|
62 | expect(error.name).to.equal(invalidTokenError);
|
63 | });
|
64 | });
|
65 | });
|
66 |
|
67 | describe("callback error", () => {
|
68 | it("name", (done) => {
|
69 | sandbox.stub(client.httpClient, "request").rejects({ response: {data: "Basic error", status: 404}});
|
70 |
|
71 | client.getServer((error: any, data) => {
|
72 | expect(data).to.equal(null);
|
73 | expect(error.name).to.equal('PostmarkError');
|
74 | done();
|
75 | });
|
76 | });
|
77 | });
|
78 |
|
79 | it("empty token", () => {
|
80 | expect(() => new postmark.ServerClient(""))
|
81 | .to.throw("A valid API token must be provided.");
|
82 | });
|
83 |
|
84 | describe("http status code errors", () => {
|
85 | const buildError = (statusNumber: number) => ({
|
86 | response: {
|
87 | data: "Basic error",
|
88 | status: statusNumber
|
89 | }
|
90 | });
|
91 |
|
92 | it("401", () => {
|
93 | sandbox.stub(client.httpClient, "request").rejects(buildError(401));
|
94 |
|
95 | return client.getServer().then((result) => {
|
96 | return result;
|
97 | }, (error) => {
|
98 | expect(error).to.be.instanceOf(postmark.Errors.InvalidAPIKeyError);
|
99 | });
|
100 | });
|
101 |
|
102 | it("404", () => {
|
103 | sandbox.stub(client.httpClient, "request").rejects(buildError(404));
|
104 |
|
105 | return client.getServer().then((result) => {
|
106 | return result;
|
107 | }, (error) => {
|
108 | expect(error).to.be.instanceOf(postmark.Errors.PostmarkError);
|
109 | });
|
110 | });
|
111 |
|
112 | it("422", () => {
|
113 | sandbox.stub(client.httpClient, "request").rejects(buildError(422));
|
114 |
|
115 | return client.getServer().then((result) => {
|
116 | return result;
|
117 | }, (error) => {
|
118 | expect(error).to.be.instanceOf(postmark.Errors.ApiInputError);
|
119 | });
|
120 | });
|
121 |
|
122 | it("500", () => {
|
123 | sandbox.stub(client.httpClient, "request").rejects(buildError(500));
|
124 |
|
125 | return client.getServer().then((result) => {
|
126 | return result;
|
127 | }, (error) => {
|
128 | expect(error).to.be.instanceOf(postmark.Errors.InternalServerError);
|
129 | });
|
130 | });
|
131 |
|
132 | it("503", () => {
|
133 | sandbox.stub(client.httpClient, "request").rejects(buildError(503));
|
134 |
|
135 | return client.getServer().then((result) => {
|
136 | return result;
|
137 | }, (error) => {
|
138 | expect(error).to.be.instanceOf(postmark.Errors.ServiceUnavailablerError);
|
139 | });
|
140 | });
|
141 |
|
142 | it("505", () => {
|
143 | sandbox.stub(client.httpClient, "request").rejects(buildError(505));
|
144 |
|
145 | return client.getServer().then((result) => {
|
146 | return result;
|
147 | }, (error) => {
|
148 | expect(error).to.be.instanceOf(postmark.Errors.UnknownError);
|
149 | });
|
150 | });
|
151 | });
|
152 | });
|
153 | });
|