Skip to content

Commit 398c2e6

Browse files
committed
wuip
1 parent 6b21c11 commit 398c2e6

File tree

8 files changed

+3247
-0
lines changed

8 files changed

+3247
-0
lines changed
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
/* eslint-disable no-console */
2+
import { Pong } from "../../../../src/request-reply/payloads/Pong";
3+
import { createTestServer, runWithServer } from './test-utils';
4+
import {
5+
getPingGetRequest,
6+
AuthConfig,
7+
} from '../../../../src/request-reply/channels/http_client';
8+
9+
jest.setTimeout(15000);
10+
11+
describe('HTTP Client - Authentication', () => {
12+
describe('bearer token', () => {
13+
it('should send bearer token in Authorization header', async () => {
14+
const { app, router, port } = createTestServer();
15+
16+
const replyMessage = new Pong({});
17+
let receivedAuthHeader: string | undefined;
18+
19+
router.get('/ping', (req, res) => {
20+
receivedAuthHeader = req.headers.authorization;
21+
res.setHeader('Content-Type', 'application/json');
22+
res.write(replyMessage.marshal());
23+
res.end();
24+
});
25+
26+
return runWithServer(app, port, async () => {
27+
const auth: AuthConfig = { type: 'bearer', token: 'test-token-123' };
28+
29+
await getPingGetRequest({
30+
server: `http://localhost:${port}`,
31+
auth
32+
});
33+
34+
expect(receivedAuthHeader).toBe('Bearer test-token-123');
35+
});
36+
});
37+
});
38+
39+
describe('basic auth', () => {
40+
it('should send basic auth credentials', async () => {
41+
const { app, router, port } = createTestServer();
42+
43+
const replyMessage = new Pong({});
44+
let receivedAuthHeader: string | undefined;
45+
46+
router.get('/ping', (req, res) => {
47+
receivedAuthHeader = req.headers.authorization;
48+
res.setHeader('Content-Type', 'application/json');
49+
res.write(replyMessage.marshal());
50+
res.end();
51+
});
52+
53+
return runWithServer(app, port, async () => {
54+
const auth: AuthConfig = { type: 'basic', username: 'user', password: 'pass' };
55+
56+
await getPingGetRequest({
57+
server: `http://localhost:${port}`,
58+
auth
59+
});
60+
61+
const expectedCredentials = Buffer.from('user:pass').toString('base64');
62+
expect(receivedAuthHeader).toBe(`Basic ${expectedCredentials}`);
63+
});
64+
});
65+
});
66+
67+
describe('API key', () => {
68+
it('should send API key in header', async () => {
69+
const { app, router, port } = createTestServer();
70+
71+
const replyMessage = new Pong({});
72+
let receivedApiKey: string | undefined;
73+
74+
router.get('/ping', (req, res) => {
75+
receivedApiKey = req.headers['x-api-key'] as string;
76+
res.setHeader('Content-Type', 'application/json');
77+
res.write(replyMessage.marshal());
78+
res.end();
79+
});
80+
81+
return runWithServer(app, port, async () => {
82+
const auth: AuthConfig = { type: 'apiKey', key: 'my-api-key-123' };
83+
84+
await getPingGetRequest({
85+
server: `http://localhost:${port}`,
86+
auth
87+
});
88+
89+
expect(receivedApiKey).toBe('my-api-key-123');
90+
});
91+
});
92+
93+
it('should send API key in query string', async () => {
94+
const { app, router, port } = createTestServer();
95+
96+
const replyMessage = new Pong({});
97+
let receivedApiKey: string | undefined;
98+
99+
router.get('/ping', (req, res) => {
100+
receivedApiKey = req.query['api_key'] as string;
101+
res.setHeader('Content-Type', 'application/json');
102+
res.write(replyMessage.marshal());
103+
res.end();
104+
});
105+
106+
return runWithServer(app, port, async () => {
107+
const auth: AuthConfig = {
108+
type: 'apiKey',
109+
key: 'my-api-key-123',
110+
name: 'api_key',
111+
in: 'query'
112+
};
113+
114+
await getPingGetRequest({
115+
server: `http://localhost:${port}`,
116+
auth
117+
});
118+
119+
expect(receivedApiKey).toBe('my-api-key-123');
120+
});
121+
});
122+
123+
it('should use custom API key header name', async () => {
124+
const { app, router, port } = createTestServer();
125+
126+
const replyMessage = new Pong({});
127+
let receivedApiKey: string | undefined;
128+
129+
router.get('/ping', (req, res) => {
130+
receivedApiKey = req.headers['x-custom-auth'] as string;
131+
res.setHeader('Content-Type', 'application/json');
132+
res.write(replyMessage.marshal());
133+
res.end();
134+
});
135+
136+
return runWithServer(app, port, async () => {
137+
const auth: AuthConfig = {
138+
type: 'apiKey',
139+
key: 'custom-key-value',
140+
name: 'X-Custom-Auth',
141+
in: 'header'
142+
};
143+
144+
await getPingGetRequest({
145+
server: `http://localhost:${port}`,
146+
auth
147+
});
148+
149+
expect(receivedApiKey).toBe('custom-key-value');
150+
});
151+
});
152+
153+
it('should handle API key in query string with existing query params', async () => {
154+
const { app, router, port } = createTestServer();
155+
156+
const replyMessage = new Pong({});
157+
let receivedApiKey: string | undefined;
158+
let receivedFilter: string | undefined;
159+
160+
router.get('/ping', (req, res) => {
161+
receivedApiKey = req.query['api_key'] as string;
162+
receivedFilter = req.query['filter'] as string;
163+
res.setHeader('Content-Type', 'application/json');
164+
res.write(replyMessage.marshal());
165+
res.end();
166+
});
167+
168+
return runWithServer(app, port, async () => {
169+
await getPingGetRequest({
170+
server: `http://localhost:${port}`,
171+
auth: {
172+
type: 'apiKey',
173+
key: 'secret-key',
174+
name: 'api_key',
175+
in: 'query'
176+
},
177+
queryParams: {
178+
filter: 'active'
179+
}
180+
});
181+
182+
expect(receivedApiKey).toBe('secret-key');
183+
expect(receivedFilter).toBe('active');
184+
});
185+
});
186+
});
187+
188+
describe('OAuth2 access token', () => {
189+
it('should use OAuth2 access token', async () => {
190+
const { app, router, port } = createTestServer();
191+
192+
const replyMessage = new Pong({});
193+
let receivedAuthHeader: string | undefined;
194+
195+
router.get('/ping', (req, res) => {
196+
receivedAuthHeader = req.headers.authorization;
197+
res.setHeader('Content-Type', 'application/json');
198+
res.write(replyMessage.marshal());
199+
res.end();
200+
});
201+
202+
return runWithServer(app, port, async () => {
203+
const auth: AuthConfig = {
204+
type: 'oauth2',
205+
accessToken: 'oauth-access-token-xyz'
206+
};
207+
208+
await getPingGetRequest({
209+
server: `http://localhost:${port}`,
210+
auth
211+
});
212+
213+
expect(receivedAuthHeader).toBe('Bearer oauth-access-token-xyz');
214+
});
215+
});
216+
});
217+
218+
describe('combined with additional headers', () => {
219+
it('should combine auth with additional headers', async () => {
220+
const { app, router, port } = createTestServer();
221+
222+
const replyMessage = new Pong({});
223+
let receivedAuthHeader: string | undefined;
224+
let receivedCustomHeader: string | undefined;
225+
let receivedTraceId: string | undefined;
226+
227+
router.get('/ping', (req, res) => {
228+
receivedAuthHeader = req.headers.authorization;
229+
receivedCustomHeader = req.headers['x-custom-header'] as string;
230+
receivedTraceId = req.headers['x-trace-id'] as string;
231+
res.setHeader('Content-Type', 'application/json');
232+
res.write(replyMessage.marshal());
233+
res.end();
234+
});
235+
236+
return runWithServer(app, port, async () => {
237+
await getPingGetRequest({
238+
server: `http://localhost:${port}`,
239+
auth: { type: 'bearer', token: 'my-token' },
240+
additionalHeaders: {
241+
'X-Custom-Header': 'custom-value',
242+
'X-Trace-Id': 'trace-123'
243+
}
244+
});
245+
246+
expect(receivedAuthHeader).toBe('Bearer my-token');
247+
expect(receivedCustomHeader).toBe('custom-value');
248+
expect(receivedTraceId).toBe('trace-123');
249+
});
250+
});
251+
});
252+
});

0 commit comments

Comments
 (0)