UNPKG

4.39 kBPlain TextView Raw
1import * as postmark from "../../src/index";
2
3import {expect} from "chai";
4import "mocha";
5
6import * as nconf from "nconf";
7const testingKeys = nconf.env().file({file: __dirname + "/../../testing_keys.json"});
8
9const packageJson = require("../../package.json");
10const clientVersion = packageJson.version;
11import * as sinon from "sinon";
12
13describe("ServerClient", () => {
14 let client: postmark.ServerClient;
15 const serverToken: string = testingKeys.get("SERVER_TOKEN");
16
17 beforeEach(() => {
18 client = new postmark.ServerClient(serverToken);
19 });
20
21 describe("#new", () => {
22 it("default clientOptions", () => {
23 const defaultClientOptions = { useHttps: true, requestHost: "api.postmarkapp.com", timeout: 60 };
24 expect(client.getClientOptions()).to.eql(defaultClientOptions);
25 });
26
27 it("clientVersion", () => {
28 expect(client.clientVersion).to.equal(clientVersion);
29 });
30 });
31
32 it("clientVersion=", () => {
33 const customClientVersion = "test";
34 client.clientVersion = customClientVersion;
35 expect(client.clientVersion).to.equal(customClientVersion);
36 });
37
38 it("getComposedHttpRequestHeaders", () => {
39 expect(client.getComposedHttpRequestHeaders()).to.eql({
40 "X-Postmark-Server-Token": serverToken,
41 "Accept": "application/json",
42 "User-Agent": `Postmark.JS - ${clientVersion}`,
43 });
44 });
45
46 describe("clientOptions", () => {
47 it("clientOptions=", () => {
48 const requestHost = "test";
49 const useHttps = false;
50 const timeout = 10;
51 client.setClientOptions({requestHost, useHttps, timeout});
52
53 expect(client.getClientOptions()).to.eql({
54 useHttps,
55 requestHost,
56 timeout,
57 });
58 });
59
60 it("new clientOptions as object", () => {
61 const requestHost = "test";
62 const useHttps = false;
63 const timeout = 50;
64 const clientOptions = new postmark.Models.ClientOptions.Configuration(useHttps, requestHost, timeout);
65 client = new postmark.ServerClient(serverToken, clientOptions);
66
67 expect(client.getClientOptions()).to.eql({
68 useHttps,
69 requestHost,
70 timeout,
71 });
72 });
73
74 it("new clientOptions as parameter", () => {
75 const requestHost = "test";
76 const useHttps = false;
77 const timeout = 50;
78
79 client = new postmark.ServerClient(serverToken, {
80 useHttps,
81 requestHost,
82 timeout,
83 });
84
85 expect(client.getClientOptions()).to.eql({
86 useHttps,
87 requestHost,
88 timeout,
89 });
90 });
91
92 });
93
94 describe("requests", () => {
95 let sandbox: sinon.SinonSandbox;
96
97 beforeEach(() => {
98 sandbox = sinon.createSandbox();
99 });
100
101 afterEach(() => {
102 sandbox.restore();
103 });
104
105 describe("callback", () => {
106 it("process it when there are no errors", async() => {
107 let callback = sinon.spy();
108 sandbox.stub(client.httpClient, "request").returns(Promise.resolve("test"));
109
110 await client.getServer(callback);
111 expect(callback.calledOnce).to.be.true
112 });
113
114 it("process regular response based on request status", () => {
115 sandbox.stub(client.httpClient, "request").returns(Promise.resolve("test"));
116
117 return client.getServer().then((result) => {
118 expect(result).to.eq("test");
119 }, (error) => {
120 throw Error(`Should not be here with error: ${error}`);
121 });
122 });
123
124 it("process error response based on request status", () => {
125 sandbox.stub(client.httpClient, "request").rejects({response: {status: 600, data: "response"}});
126
127 return client.getServer().then((result) => {
128 throw Error(`Should not be here with result: ${result}`);
129 }, (error) => {
130 expect(error.name).to.eq("UnknownError");
131 });
132 });
133 });
134 });
135
136});