UNPKG

4.88 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';
12import BaseClient from "../../src/client/BaseClient";
13
14describe("ServerClient", () => {
15 let client: postmark.ServerClient;
16 const serverToken: string = testingKeys.get("SERVER_TOKEN");
17
18 beforeEach(() => {
19 client = new postmark.ServerClient(serverToken);
20 });
21
22 describe("#new", () => {
23 it("default clientOptions", () => {
24 expect(client.clientOptions).to.eql({
25 useHttps: true,
26 requestHost: "api.postmarkapp.com",
27 timeout: 30,
28 });
29 });
30
31 it("clientVersion", () => {
32 expect(client.clientVersion).to.equal(clientVersion);
33 });
34 });
35
36 it("clientVersion=", () => {
37 const customClientVersion = "test";
38 client.clientVersion = customClientVersion;
39 expect(client.clientVersion).to.equal(customClientVersion);
40 });
41
42 it("getComposedHttpRequestHeaders", () => {
43 expect(client.getComposedHttpRequestHeaders()).to.eql({
44 "X-Postmark-Server-Token": serverToken,
45 "Accept": "application/json",
46 "User-Agent": `Postmark.JS - ${clientVersion}`,
47 });
48 });
49
50 describe("clientOptions", () => {
51 it("clientOptions=", () => {
52 const requestHost = "test";
53 const useHttps = false;
54 const timeout = 10;
55
56 client.clientOptions.requestHost = requestHost;
57 client.clientOptions.useHttps = useHttps;
58 client.clientOptions.timeout = timeout;
59
60 expect(client.clientOptions).to.eql({
61 useHttps,
62 requestHost,
63 timeout,
64 });
65 });
66
67 it("new clientOptions as object", () => {
68 const requestHost = "test";
69 const useHttps = false;
70 const timeout = 50;
71 const clientOptions = new postmark.Models.ClientOptions.Configuration(useHttps, requestHost, timeout);
72 client = new postmark.ServerClient(serverToken, clientOptions);
73
74 expect(client.clientOptions).to.eql({
75 useHttps,
76 requestHost,
77 timeout,
78 });
79 });
80
81 it("new clientOptions as parameter", () => {
82 const requestHost = "test";
83 const useHttps = false;
84 const timeout = 50;
85
86 client = new postmark.ServerClient(serverToken, {
87 useHttps,
88 requestHost,
89 timeout,
90 });
91
92 expect(client.clientOptions).to.eql({
93 useHttps,
94 requestHost,
95 timeout,
96 });
97 });
98
99 });
100
101 describe("requests", () => {
102 let sandbox: sinon.SinonSandbox;
103
104 beforeEach(() => {
105 sandbox = sinon.createSandbox();
106 });
107
108 afterEach(() => {
109 sandbox.restore();
110 });
111
112 it('processRequest - without body called', () => {
113 sandbox.stub(BaseClient.prototype, <any> "processRequest").returns("called")
114 expect(client.getServer()).to.eq("called")
115 });
116
117 it('processRequest - with body called', () => {
118 sandbox.stub(BaseClient.prototype, <any> "processRequest").returns("called");
119 expect(client.editServer({Name: 'Test'})).to.eq("called")
120 });
121
122 describe("callback", () => {
123 it('process it when there are no errors', async() => {
124 let callback = sinon.spy();
125 sandbox.stub(BaseClient.prototype, <any> "processHttpRequest").returns(new Promise( function(resolve) { resolve("test"); }));
126 await client.getServer(callback);
127
128 expect(callback.calledOnce).to.be.true
129 });
130
131 it('process regular response based on request status', () => {
132 sandbox.stub(BaseClient.prototype, <any> "httpRequest").yields(undefined, {statusCode: 200, body: 'response'});
133
134 return client.getServer( (error, result) => {
135 expect(result).to.eq('response');
136 });
137 });
138
139 it('process error response based on request status', () => {
140 sandbox.stub(BaseClient.prototype, <any> "httpRequest").yields(undefined, {statusCode: 201, body: 'response'});
141
142 return client.getServer( (error: any, result) => {
143 expect(error.name).to.eq('UnknownError');
144 }).catch( error => {});
145 });
146 });
147 });
148
149});