UNPKG

5.77 kBJavaScriptView Raw
1const expect = require("chai").expect;
2const nock = require("nock");
3
4const BuzzApi = require("../index");
5const response = require("./response");
6
7const buzzapisync = new BuzzApi({ apiUser: "", apiPassword: "", sync: true });
8const buzzapi = new BuzzApi({ apiUser: "", apiPassword: "" });
9
10const defaultBody = {
11 api_operation: "read",
12 api_pull_response_to: ["ABC123"],
13 api_app_ticket: "XYZ789",
14 api_receive_timeout: 5000,
15 api_client_request_handle: /.*/
16};
17const api = nock("https://api.gatech.edu");
18
19beforeEach(() => {
20 nock.cleanAll();
21});
22
23describe("Sync tests", () => {
24 it("Gets a resource in a single request", () => {
25 api
26 .post("/apiv3/test/test", () => {
27 return true;
28 })
29 .reply(200, response.sync);
30 return buzzapisync.post("test", "test", {}).then(response => {
31 expect(typeof response).to.equal("object");
32 expect(response.success);
33 });
34 });
35
36 it("Handles buzzapi errors", () => {
37 api
38 .post("/apiv3/test/test", () => {
39 return true;
40 })
41 .reply(200, response.syncError);
42 return buzzapisync.post("test", "test", {}).catch(err => {
43 expect(typeof err.buzzApiBody).to.equal("object");
44 expect(err.buzzApiErrorInfo.success).to.equal(false);
45 });
46 });
47
48 it("Handles http errors", () => {
49 api
50 .post("/apiv3/test/test", () => {
51 return true;
52 })
53 .reply(404, "Not Found");
54 return buzzapisync.post("test", "test", {}).catch(err => {
55 expect(err.buzzApiBody).to.equal("Not Found");
56 return expect(err.buzzApiErrorInfo).to.be.empty;
57 });
58 });
59
60 it("Handles errors with no body set", () => {
61 api
62 .post("/apiv3/test/test", () => {
63 return true;
64 })
65 .reply(400);
66 return buzzapisync.post("test", "test", {}).catch(err => {
67 expect(err.message).to.equal("Bad Request");
68 return expect(err.buzzApiBody).to.equal("Bad Request");
69 });
70 });
71
72 it("Does not lose requests when opening more than the queuing limit of 20", () => {
73 const reqs = api
74 .post("/apiv3/test/test", () => {
75 return true;
76 })
77 .times(25)
78 .socketDelay(200)
79 .reply(200, response.sync);
80 const check = response => {
81 expect(typeof response).to.equal("object");
82 expect(response.success);
83 };
84 const promises = function*(start = 0, end = 24, step = 1) {
85 for (let i = start; i < end; i += step) {
86 yield buzzapisync.post("test", "test", {});
87 }
88 };
89 return Promise.all([...promises()]).then(res => res.map(check));
90 expect(reqs.isDone());
91 }).timeout(6000);
92});
93
94describe("Async tests", () => {
95 it("Makes a second request to get async messages", () => {
96 api
97 .post("/apiv3/test/test", () => {
98 return true;
99 })
100 .reply(200, response.async);
101 const aReq = api
102 .post("/apiv3/api.my_messages", defaultBody)
103 .reply(200, response.asyncSuccess);
104 return buzzapi.post("test", "test", {}).then(response => {
105 expect(typeof response).to.equal("object");
106 expect(response.success);
107 expect(aReq.isDone());
108 });
109 });
110
111 it("Tries again if async result not ready", () => {
112 api
113 .post("/apiv3/test/test", () => {
114 return true;
115 })
116 .reply(200, response.async);
117 const nrReq = api
118 .post("/apiv3/api.my_messages", defaultBody)
119 .reply(200, response.asyncNotReady);
120 api
121 .post("/apiv3/api.my_messages", defaultBody)
122 .reply(200, response.asyncSuccess);
123 return buzzapi.post("test", "test", {}).then(response => {
124 expect(typeof response).to.equal("object");
125 expect(response.success);
126 expect(nrReq.isDone());
127 });
128 }).timeout(6000);
129
130 it("Handles buzzapi errors", () => {
131 api
132 .post("/apiv3/test/test", () => {
133 return true;
134 })
135 .reply(200, response.async);
136 api
137 .post("/apiv3/api.my_messages", defaultBody)
138 .reply(200, response.asyncError);
139 return buzzapi.post("test", "test", {}).catch(err => {
140 expect(typeof err.buzzApiBody).to.equal("object");
141 expect(err.buzzApiErrorInfo.success).to.equal(false);
142 });
143 });
144
145 // I don't think this ever happens
146 /* it("Handles buzzapi errors at top level of response", () => {
147 api
148 .post("/apiv3/test/test", () => {
149 return true;
150 })
151 .reply(200, response.async);
152 api
153 .post("/apiv3/api.my_messages", defaultBody)
154 .reply(200, response.syncError);
155 return buzzapi.post("test", "test", {}).catch(err => {
156 expect(typeof err.buzzApiBody).to.equal("object");
157 expect(err.buzzApiErrorInfo.success).to.equal(false);
158 });
159 }); */
160
161 it("Retries getting results on error", () => {
162 api
163 .post("/apiv3/test/test", () => {
164 return true;
165 })
166 .reply(200, response.async);
167 api.post("/apiv3/api.my_messages", defaultBody).reply(500);
168 api
169 .post("/apiv3/api.my_messages", defaultBody)
170 .reply(200, response.asyncSuccess);
171 return buzzapi.post("test", "test", {}).then(response => {
172 expect(typeof response).to.equal("object");
173 expect(response.success);
174 });
175 }).timeout(6000);
176
177 it("Gives up retrying a request after reaching the timeout", () => {
178 api
179 .post("/apiv3/test/test", () => {
180 return true;
181 })
182 .reply(200, response.async);
183 api
184 .post("/apiv3/api.my_messages", defaultBody)
185 .reply(200, response.asyncNotReady);
186 const defaultTimeout = buzzapi.options.api_receive_timeout;
187 buzzapi.options.api_receive_timeout = 1;
188 return buzzapi.post("test", "test", {}).catch(err => {
189 buzzapi.options.api_receive_timeout = defaultTimeout;
190 expect(err.message).to.equal("Request timed out for: ABC123");
191 });
192 }).timeout(6000);
193});