Repository navigation
Expand file tree
/
Copy pathdtryx.test.ts
More file actions
358 lines (355 loc) · 13.3 KB
/
Copy pathdtryx.test.ts
File metadata and controls
358 lines (355 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import { describe, it, expect, vi } from 'vitest';
import { createDtryxRelay } from '../../scripts/relay/dtryx.js';
const token = 'test-token';
const input = { brandCode: 'art', cinemaCode: '000001', playDate: '20260928' };
const result = { RetCode: 'success', Recordset: [{ MovieCd: '1' }] };
const request = (body: unknown = input, path = 'timetable', signal?: AbortSignal) =>
new Request(`http://localhost/v1/dtryx/${path}`, {
method: 'POST',
headers: { Authorization: `Bearer ${token}` },
body: JSON.stringify(body),
signal,
});
const setup = (options = {}) => {
const fetcher = vi.fn(async () => Response.json(result));
const takeQuota = vi.fn(async () => true);
return {
fetcher,
takeQuota,
handler: createDtryxRelay(token, { fetcher, takeQuota, ...options }),
};
};
describe('디트릭스 로컬 릴레이', () => {
it('고정 HTTPS URL과 작업 식별자로 원본 응답을 보존한다', async () => {
const { handler, fetcher } = setup();
expect(await (await handler(request())).json()).toEqual(result);
const [url, init] = fetcher.mock.calls[0] as unknown as [URL, RequestInit];
expect(url.origin).toBe('https://api.dtryx.com');
expect(url.searchParams.get('PlaySDT')).toBe('2026-09-28');
expect(url.searchParams.get('WorkGuID')).toBe('37E0BA0F-DA5F-4376-9BA4-B5D27286AB87');
expect(init.redirect).toBe('manual');
});
it.each([
null,
[],
{},
{ ...input, url: 'http://127.0.0.1' },
{ ...input, headers: {} },
{ ...input, brandCode: 'https://evil' },
{ ...input, brandCode: 'A' },
{ ...input, brandCode: 'a'.repeat(33) },
{ ...input, cinemaCode: 123456 },
{ ...input, playDate: '20260230' },
{ ...input, playDate: '20261301' },
{ ...input, playDate: 1e99 },
])('잘못된 입력 거절 %j', async (body) => {
const { handler, takeQuota, fetcher } = setup();
expect((await handler(request(body))).status).toBe(400);
expect(takeQuota).not.toHaveBeenCalled();
expect(fetcher).not.toHaveBeenCalled();
});
it('인증, 상태 확인, 경로와 메서드를 검사한다', async () => {
const { handler, takeQuota } = setup();
expect((await handler(new Request('http://localhost/v1/dtryx/health'))).status).toBe(401);
expect(
(
await handler(
new Request('http://localhost/v1/dtryx/health', {
headers: { Authorization: `Bearer ${token}` },
}),
)
).status,
).toBe(200);
for (const path of ['constructor', '__proto__', 'toString', 'movies?url=evil'])
expect((await handler(request({}, path))).status).toBe(404);
expect(
(
await handler(
new Request('http://localhost/v1/dtryx/movies', {
headers: { Authorization: `Bearer ${token}` },
}),
)
).status,
).toBe(405);
expect(takeQuota).not.toHaveBeenCalled();
});
it.each(['play-dates', 'movies'])('날짜 없는 경로 %s', async (path) => {
const { handler } = setup();
expect((await handler(request({ brandCode: 'a_0-', cinemaCode: '000001' }, path))).status).toBe(
200,
);
expect((await handler(request(input, path))).status).toBe(400);
});
it.each([301, 204, 500])('원본 HTTP %i를 거절한다', async (status) => {
const { handler } = setup({ fetcher: async () => new Response(null, { status }) });
expect((await handler(request())).status).toBe(502);
});
it.each([
null,
[],
{},
{ RetCode: 'failed', Recordset: [] },
{ RetCode: 'success', Recordset: [null] },
{ RetCode: 'success', Recordset: [[]] },
])('원본 스키마 거절 %j', async (body) => {
const { handler } = setup({ fetcher: async () => Response.json(body) });
expect((await handler(request())).status).toBe(502);
});
it('용량 제한과 JSON 오류를 거절한다', async () => {
const { handler } = setup();
expect((await handler(request('a'.repeat(16384)))).status).toBe(413);
expect(
(
await handler(
new Request('http://localhost/v1/dtryx/movies', {
method: 'POST',
headers: { Authorization: `Bearer ${token}` },
body: '{',
}),
)
).status,
).toBe(400);
const huge = setup({ fetcher: async () => new Response('a'.repeat(2097153)) });
expect((await huge.handler(request())).status).toBe(502);
});
it('할당량 거절 및 저장 실패는 안전하게 처리한다', async () => {
expect((await setup({ takeQuota: async () => false }).handler(request())).status).toBe(429);
expect(
(
await setup({
takeQuota: async () => {
throw Error('secret');
},
}).handler(request())
).status,
).toBe(503);
});
it('전체 기한을 원본과 요청 본문에 적용한다', async () => {
vi.useFakeTimers();
try {
const { handler } = setup({ fetcher: () => new Promise(() => {}) });
const pending = handler(request());
await vi.advanceTimersByTimeAsync(15000);
expect((await pending).status).toBe(504);
const stalled = new Request('http://localhost/v1/dtryx/movies', {
method: 'POST',
headers: { Authorization: `Bearer ${token}` },
body: new ReadableStream(),
duplex: 'half',
} as RequestInit);
const bodyPending = handler(stalled);
await vi.advanceTimersByTimeAsync(15000);
expect((await bodyPending).status).toBe(504);
expect(vi.getTimerCount()).toBe(0);
} finally {
vi.useRealTimers();
}
});
it('대기 취소는 예산을 소비하지 않고 동시4개·전체32개를 제한한다', async () => {
const releases: (() => void)[] = [];
const fetcher = vi.fn(
() => new Promise<Response>((resolve) => releases.push(() => resolve(Response.json(result)))),
);
const { handler, takeQuota } = setup({ fetcher });
const controllers = Array.from({ length: 32 }, () => new AbortController());
const pending = controllers.map((c) => handler(request(input, 'timetable', c.signal)));
await vi.waitFor(() => expect(fetcher).toHaveBeenCalledTimes(4));
expect((await handler(request())).status).toBe(503);
controllers[4].abort();
expect((await pending[4]).status).toBe(499);
expect(takeQuota).toHaveBeenCalledTimes(4);
releases[0]();
await vi.waitFor(() => expect(fetcher).toHaveBeenCalledTimes(5));
controllers.forEach((c) => c.abort());
await Promise.all(pending);
expect((await setup().handler(request())).status).toBe(200);
});
it('빈 토큰·본문·이미 취소된 요청과 오류 스트림을 처리한다', async () => {
expect(() => createDtryxRelay(' ', { takeQuota: async () => true })).toThrow();
const { handler } = setup();
const empty = new Request('http://localhost/v1/dtryx/movies', {
method: 'POST',
headers: { Authorization: `Bearer ${token}` },
});
expect((await handler(empty)).status).toBe(400);
const controller = new AbortController();
controller.abort();
expect((await handler(request(input, 'timetable', controller.signal))).status).toBe(499);
const stream = new ReadableStream({
start(c) {
c.error(Error('stream'));
},
});
const broken = new Request('http://localhost/v1/dtryx/movies', {
method: 'POST',
headers: { Authorization: `Bearer ${token}` },
body: stream,
duplex: 'half',
} as RequestInit);
expect((await handler(broken)).status).toBe(400);
});
it('기본 fetch와 같은 길이의 잘못된 인증을 검사한다', async () => {
vi.stubGlobal('fetch', async () => Response.json(result));
try {
const handler = createDtryxRelay(token, { takeQuota: async () => true });
expect((await handler(request())).status).toBe(200);
expect(
(
await handler(
new Request('http://localhost/v1/dtryx/health', {
headers: { Authorization: 'Bearer test-tokem' },
}),
)
).status,
).toBe(401);
} finally {
vi.unstubAllGlobals();
}
});
it('본문 파싱 직후 취소하면 큐나 예산을 소비하지 않는다', async () => {
const controller = new AbortController();
const { handler, takeQuota } = setup();
const original = JSON.parse;
const spy = vi.spyOn(JSON, 'parse').mockImplementation((...args) => {
const value = original(...args);
controller.abort();
return value;
});
try {
expect((await handler(request(input, 'timetable', controller.signal))).status).toBe(499);
} finally {
spy.mockRestore();
}
expect(takeQuota).not.toHaveBeenCalled();
});
it('리다이렉트 본문 취소가 멈춰도 오류 응답을 반환한다', async () => {
vi.useFakeTimers();
try {
const { handler } = setup({
fetcher: async () =>
new Response(new ReadableStream({ cancel: () => new Promise(() => {}) }), {
status: 302,
}),
});
let response: Response | undefined;
const pending = handler(request()).then((value) => {
response = value;
});
await vi.advanceTimersByTimeAsync(15000);
expect(response?.status).toBe(502);
await pending;
} finally {
vi.useRealTimers();
}
});
it('대기 중 기한 만료는 슬롯과 타이머를 반환하고 예산을 소비하지 않는다', async () => {
vi.useFakeTimers();
try {
let blocked = true;
const { handler, takeQuota } = setup({
fetcher: () => (blocked ? new Promise(() => {}) : Promise.resolve(Response.json(result))),
});
const pending = Array.from({ length: 6 }, () => handler(request()));
await vi.advanceTimersByTimeAsync(14999);
expect(takeQuota).toHaveBeenCalledTimes(4);
await vi.advanceTimersByTimeAsync(1);
expect((await Promise.all(pending)).every((r) => r.status === 504)).toBe(true);
expect(takeQuota).toHaveBeenCalledTimes(4);
blocked = false;
expect((await handler(request())).status).toBe(200);
expect(vi.getTimerCount()).toBe(0);
} finally {
vi.useRealTimers();
}
});
it('정리 실패와 원본 JSON 오류도 안전하게 처리한다', async () => {
const { handler } = setup({
fetcher: async () =>
new Response(new ReadableStream({ cancel: () => Promise.reject(Error('cancel')) }), {
status: 302,
}),
});
expect((await handler(request())).status).toBe(502);
expect(
(await setup({ fetcher: async () => new Response('{') }).handler(request())).status,
).toBe(502);
});
it('연결 실패 내용을 숨기고 이미 취소된 오류 스트림의 거절을 관찰한다', async () => {
const network = setup({
fetcher: async () => {
throw Error('private upstream detail');
},
});
const response = await network.handler(request());
expect(response.status).toBe(502);
expect(await response.text()).not.toContain('private');
const controller = new AbortController();
controller.abort();
const body = new ReadableStream({
start(c) {
c.error(Error('already failed'));
},
});
const req = new Request('http://localhost/v1/dtryx/movies', {
method: 'POST',
headers: { Authorization: `Bearer ${token}` },
body,
signal: controller.signal,
duplex: 'half',
} as RequestInit);
expect((await setup().handler(req)).status).toBe(499);
await new Promise((resolve) => setTimeout(resolve, 0));
});
it('인증된 상태 응답으로 실행과 대기 요청 수를 관찰한다', async () => {
const { handler } = setup({ fetcher: () => new Promise(() => {}) });
const controllers = Array.from({ length: 5 }, () => new AbortController());
const pending = controllers.map((c) => handler(request(input, 'timetable', c.signal)));
const health = () =>
handler(
new Request('http://localhost/v1/dtryx/health', {
headers: { Authorization: `Bearer ${token}` },
}),
);
await vi.waitFor(async () =>
expect(await (await health()).json()).toEqual({
status: 'ok',
active: 4,
outstanding: 5,
queued: 1,
}),
);
controllers.forEach((c) => c.abort());
await Promise.all(pending);
expect(await (await health()).json()).toEqual({
status: 'ok',
active: 0,
outstanding: 0,
queued: 0,
});
});
it('대기 요청은 FIFO 순서로 원본 호출을 시작한다', async () => {
const started: string[] = [];
const releases: (() => void)[] = [];
const { handler } = setup({
fetcher: (url: URL) => {
started.push(url.searchParams.get('CinemaCd') as string);
return new Promise<Response>((resolve) =>
releases.push(() => resolve(Response.json(result))),
);
},
});
const controllers = Array.from({ length: 6 }, () => new AbortController());
const pending = controllers.map((c, i) =>
handler(request({ ...input, cinemaCode: String(i).padStart(6, '0') }, 'timetable', c.signal)),
);
await vi.waitFor(() => expect(started).toHaveLength(4));
releases[1]();
await vi.waitFor(() => expect(started).toHaveLength(5));
releases[0]();
await vi.waitFor(() =>
expect(started).toEqual(['000000', '000001', '000002', '000003', '000004', '000005']),
);
controllers.forEach((c) => c.abort());
await Promise.all(pending);
});
});