UNPKG

7.35 kBPlain TextView Raw
1import * as chai from 'chai';
2import * as chaiAsPromised from 'chai-as-promised';
3chai.use(chaiAsPromised);
4const { expect } = chai;
5
6import { assert } from 'chai';
7import * as nock from 'nock';
8
9import { createWrapper, IRequestContext } from './index';
10
11describe('request-promise proxy', () => {
12 describe('GET', () => {
13 it('should output correct request options for a get request', () => {
14 const BASE_URL = 'http://localhost:8082/api/';
15 const wrapper = createWrapper(BASE_URL);
16
17 const result = wrapper.buildJsonRequest('agency/thing/23');
18
19 assert.deepEqual(result, {
20 json: true,
21 method: 'GET',
22 url: 'http://localhost:8082/api/agency/thing/23',
23 });
24 });
25
26 it('should output correct request options for a get request with qs option', () => {
27 const BASE_URL = 'http://localhost:8080/api/';
28 const wrapper = createWrapper(BASE_URL);
29
30 const result = wrapper.buildJsonRequest('things', 'GET', {
31 qs: { stuff: '123' },
32 });
33
34 assert.deepEqual(result, {
35 json: true,
36 method: 'GET',
37 qs: {
38 stuff: '123',
39 },
40 url: 'http://localhost:8080/api/things',
41 });
42 });
43
44 it('should output correct request options for a get request with qs and header option', () => {
45 const BASE_URL = 'http://localhost:8080/api/';
46 const wrapper = createWrapper(BASE_URL);
47
48 const result = wrapper.buildJsonRequest('things', 'GET', {
49 qs: { stuff: '123' },
50 headers: {
51 Authorization: 'Bearer xyzz23',
52 },
53 });
54
55 assert.deepEqual(result, {
56 json: true,
57 method: 'GET',
58 qs: {
59 stuff: '123',
60 },
61 headers: {
62 Authorization: 'Bearer xyzz23',
63 },
64 url: 'http://localhost:8080/api/things',
65 });
66 });
67
68 it('should output correct request options for a get request with context option', () => {
69 const BASE_URL = 'http://localhost:8080/api/';
70 const wrapper = createWrapper(BASE_URL);
71
72 const result = wrapper.buildJsonRequest('things', 'GET', {
73 qs: { stuff: '123' },
74 headers: {
75 Authorization: 'Bearer xyzz23',
76 },
77 });
78
79 assert.deepEqual(result, {
80 json: true,
81 method: 'GET',
82 qs: {
83 stuff: '123',
84 },
85 headers: {
86 Authorization: 'Bearer xyzz23',
87 },
88 url: 'http://localhost:8080/api/things',
89 });
90 });
91
92 it('should handle 404 error', async () => {
93 const BASE_URL = 'http://localhost:8080/api';
94
95 nock(BASE_URL).get('/things/1').reply(404, 'not found');
96
97 const wrapper = createWrapper(BASE_URL);
98
99 const expectedError = '404 - "not found" - http://localhost:8080/api/things/1';
100 await expect(wrapper.proxyJsonRequest('/things/1')).to.be.rejectedWith(expectedError);
101 });
102
103 it('should handle 500 error', async () => {
104 const BASE_URL = 'http://localhost:8080/api';
105
106 nock(BASE_URL).get('/things/1').reply(500, {
107 message: 'Internal Server Error',
108 });
109
110 const wrapper = createWrapper(BASE_URL);
111
112 const expectedError = '500 - {"message":"Internal Server Error"} - http://localhost:8080/api/things/1';
113 await expect(wrapper.proxyJsonRequest('/things/1')).to.be.rejectedWith(expectedError);
114 });
115 });
116
117 describe('POST', () => {
118 it('should output correct request options for a post request with body', () => {
119 const BASE_URL = 'http://localhost:8082/api/';
120 const wrapper = createWrapper(BASE_URL);
121
122 const result = wrapper.buildJsonRequest('agency/thing', 'POST', {
123 body: {
124 name: 'new thing',
125 description: 'this is new',
126 },
127 });
128
129 assert.deepEqual(result, {
130 json: true,
131 method: 'POST',
132 body: {
133 name: 'new thing',
134 description: 'this is new',
135 },
136 url: 'http://localhost:8082/api/agency/thing',
137 });
138 });
139 });
140
141 describe('PUT', () => {
142 it('should output correct request options for a put request with body', () => {
143 const BASE_URL = 'http://localhost:8082/api/';
144 const wrapper = createWrapper(BASE_URL);
145
146 const result = wrapper.buildJsonRequest('agency/thing/23', 'PUT', {
147 body: {
148 description: 'this is even newer',
149 },
150 });
151
152 assert.deepEqual(result, {
153 json: true,
154 method: 'PUT',
155 body: {
156 description: 'this is even newer',
157 },
158 url: 'http://localhost:8082/api/agency/thing/23',
159 });
160 });
161 });
162
163 describe('DELETE', () => {
164 it('should output correct request options for a delete request', () => {
165 const BASE_URL = 'http://localhost:8082/api/';
166 const wrapper = createWrapper(BASE_URL);
167
168 const result = wrapper.buildJsonRequest('agency/thing/23', 'DELETE');
169
170 assert.deepEqual(result, {
171 json: true,
172 method: 'DELETE',
173 url: 'http://localhost:8082/api/agency/thing/23',
174 });
175 });
176 });
177
178 describe('Istio HEADERS', () => {
179 it('GET should forward Request Headers', () => {
180 const BASE_URL = 'http://localhost:8082/api/';
181 const wrapper = createWrapper(BASE_URL);
182
183 const result = wrapper.buildJsonRequest('agency/thing/23', 'GET', {}, {
184 request: {
185 headers: {
186 'x-request-id': 'xRequestId',
187 'x-b3-traceid': 'traceId',
188 'x-b3-spanid': 'spanId',
189 'x-b3-parentspanid': 'parentSpanId',
190 'x-b3-sampled': 'sampleId',
191 'x-b3-flags': 'flags',
192 'x-ot-span-context': 'spanContext',
193 },
194 },
195 } as IRequestContext);
196
197 assert.deepEqual(result, {
198 json: true,
199 method: 'GET',
200 headers: {
201 'x-request-id': 'xRequestId',
202 'x-b3-traceid': 'traceId',
203 'x-b3-spanid': 'spanId',
204 'x-b3-parentspanid': 'parentSpanId',
205 'x-b3-sampled': 'sampleId',
206 'x-b3-flags': 'flags',
207 'x-ot-span-context': 'spanContext',
208 },
209 url: 'http://localhost:8082/api/agency/thing/23',
210 });
211 });
212 it('GET should forward Request Headers including manual sent', () => {
213 const BASE_URL = 'http://localhost:8082/api/';
214 const wrapper = createWrapper(BASE_URL);
215
216 const result = wrapper.buildJsonRequest('agency/thing/23', 'GET', {
217 headers: {
218 'x-request-id': 'thisShouldNotWinId',
219 },
220 }, {
221 request: {
222 headers: {
223 'x-request-id': 'xRequestId',
224 'x-b3-traceid': 'traceId',
225 'x-b3-spanid': 'spanId',
226 'x-b3-parentspanid': 'parentSpanId',
227 'x-b3-sampled': 'sampleId',
228 'x-b3-flags': 'flags',
229 'x-ot-span-context': 'spanContext',
230 },
231 },
232 } as IRequestContext);
233
234 assert.deepEqual(result, {
235 json: true,
236 method: 'GET',
237 headers: {
238 'x-request-id': 'xRequestId',
239 'x-b3-traceid': 'traceId',
240 'x-b3-spanid': 'spanId',
241 'x-b3-parentspanid': 'parentSpanId',
242 'x-b3-sampled': 'sampleId',
243 'x-b3-flags': 'flags',
244 'x-ot-span-context': 'spanContext',
245 },
246 url: 'http://localhost:8082/api/agency/thing/23',
247 });
248 });
249 });
250});