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