forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e-http-server.test.ts
More file actions
190 lines (162 loc) · 5.55 KB
/
Copy pathe2e-http-server.test.ts
File metadata and controls
190 lines (162 loc) · 5.55 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
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import * as http from 'node:http';
import type { AddressInfo } from 'node:net';
import { createFetchHttpClient } from '../src/http/fetch-http-client';
import { LilyApiError, LilyAuthenticationError } from '../src/errors/sdk-error';
let server: http.Server;
let baseUrl: URL;
beforeAll(async () => {
server = http.createServer((req, res) => {
const url = new URL(req.url ?? '/', `http://${req.headers.host}`);
if (url.pathname === '/v1/json') {
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ status: 'ok', method: req.method }));
return;
}
if (url.pathname === '/v1/no-content') {
res.writeHead(204);
res.end();
return;
}
if (url.pathname === '/v1/unauthorized') {
res.writeHead(401, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'invalid credentials' }));
return;
}
if (url.pathname === '/v1/text') {
res.writeHead(200, { 'content-type': 'text/plain' });
res.end('plain text response');
return;
}
if (url.pathname === '/v1/rate-limited') {
res.writeHead(429, {
'content-type': 'application/json',
'retry-after': '1',
});
res.end(JSON.stringify({ message: 'rate limited' }));
return;
}
res.writeHead(404, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'not found' }));
});
await new Promise<void>((resolve) => {
server.listen(0, '127.0.0.1', () => resolve());
});
const addr = server.address() as AddressInfo;
baseUrl = new URL(`http://127.0.0.1:${addr.port}/`);
});
afterAll(async () => {
await new Promise<void>((resolve) => server.close(() => resolve()));
});
describe('end-to-end transport against real node:http server', () => {
it('parses JSON response over a real socket', async () => {
const httpClient = createFetchHttpClient({
baseUrl,
timeoutMs: 5_000,
retry: { retries: 0, retryDelayMs: 0, retryableStatusCodes: [] },
defaultHeaders: {},
userAgent: 'lily-sdk/e2e-test',
fetch: globalThis.fetch,
});
const response = await httpClient.request<{
status: string;
method: string;
}>({
method: 'GET',
path: '/v1/json',
});
expect(response.status).toBe(200);
expect(response.data.status).toBe('ok');
expect(response.data.method).toBe('GET');
});
it('handles 204 No Content without parsing body', async () => {
const httpClient = createFetchHttpClient({
baseUrl,
timeoutMs: 5_000,
retry: { retries: 0, retryDelayMs: 0, retryableStatusCodes: [] },
defaultHeaders: {},
userAgent: 'lily-sdk/e2e-test',
fetch: globalThis.fetch,
});
const response = await httpClient.request({
method: 'POST',
path: '/v1/no-content',
});
expect(response.status).toBe(204);
expect(response.data).toBeNull();
});
it('throws LilyAuthenticationError on 401 over real network', async () => {
const httpClient = createFetchHttpClient({
baseUrl,
timeoutMs: 5_000,
retry: { retries: 0, retryDelayMs: 0, retryableStatusCodes: [] },
defaultHeaders: {},
userAgent: 'lily-sdk/e2e-test',
fetch: globalThis.fetch,
});
await expect(
httpClient.request({ method: 'GET', path: '/v1/unauthorized' }),
).rejects.toBeInstanceOf(LilyAuthenticationError);
});
it('returns plain text response when content-type is not JSON', async () => {
const httpClient = createFetchHttpClient({
baseUrl,
timeoutMs: 5_000,
retry: { retries: 0, retryDelayMs: 0, retryableStatusCodes: [] },
defaultHeaders: {},
userAgent: 'lily-sdk/e2e-test',
fetch: globalThis.fetch,
});
const response = await httpClient.request<string>({
method: 'GET',
path: '/v1/text',
});
expect(response.status).toBe(200);
expect(response.data).toBe('plain text response');
});
it('retries on 429 and succeeds when server recovers', async () => {
let callCount = 0;
const originalServer = server;
// Create a separate server that fails once then succeeds
const retryServer = http.createServer((_req, res) => {
callCount++;
if (callCount <= 1) {
res.writeHead(429, {
'content-type': 'application/json',
'retry-after': '0',
});
res.end(JSON.stringify({ message: 'rate limited' }));
} else {
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ recovered: true, attempt: callCount }));
}
});
await new Promise<void>((resolve) =>
retryServer.listen(0, '127.0.0.1', () => resolve()),
);
const retryAddr = retryServer.address() as AddressInfo;
const retryBaseUrl = new URL(`http://127.0.0.1:${retryAddr.port}/`);
try {
const httpClient = createFetchHttpClient({
baseUrl: retryBaseUrl,
timeoutMs: 5_000,
retry: { retries: 2, retryDelayMs: 10, retryableStatusCodes: [429] },
defaultHeaders: {},
userAgent: 'lily-sdk/e2e-test',
fetch: globalThis.fetch,
});
const response = await httpClient.request<{
recovered: boolean;
attempt: number;
}>({
method: 'GET',
path: '/v1/data',
});
expect(response.status).toBe(200);
expect(response.data.recovered).toBe(true);
expect(callCount).toBe(2);
} finally {
await new Promise<void>((resolve) => retryServer.close(() => resolve()));
}
});
});