|
1 | | -import { describe, it, expect, afterEach } from 'bun:test'; |
2 | | -import { requestJson } from '../../src/client/http'; |
| 1 | +import { describe, it, expect, afterEach, spyOn } from 'bun:test'; |
| 2 | +import { request, requestJson } from '../../src/client/http'; |
3 | 3 | import { CLI_VERSION } from '../../src/version'; |
4 | 4 | import { createMockServer, jsonResponse, type MockServer } from '../helpers/mock-server'; |
5 | 5 | import type { Config } from '../../src/config/schema'; |
@@ -93,4 +93,158 @@ describe('HTTP client', () => { |
93 | 93 | requestJson(config, { url: `${server.url}/v1/test` }), |
94 | 94 | ).rejects.toThrow('Rate limit'); |
95 | 95 | }); |
| 96 | + |
| 97 | + it('aborts a streaming request when response headers stall', async () => { |
| 98 | + let aborted = false; |
| 99 | + const fetchSpy = spyOn(globalThis, 'fetch').mockImplementation(( |
| 100 | + (_input: string | URL | Request, init?: RequestInit) => |
| 101 | + new Promise<Response>((_resolve, reject) => { |
| 102 | + init?.signal?.addEventListener('abort', () => { |
| 103 | + aborted = true; |
| 104 | + reject(init.signal?.reason); |
| 105 | + }, { once: true }); |
| 106 | + }) |
| 107 | + ) as unknown as typeof fetch); |
| 108 | + |
| 109 | + try { |
| 110 | + const config = makeConfig('https://example.com'); |
| 111 | + await expect(request(config, { |
| 112 | + url: 'https://example.com/stream', |
| 113 | + stream: true, |
| 114 | + timeout: 0.02, |
| 115 | + noAuth: true, |
| 116 | + })).rejects.toMatchObject({ name: 'TimeoutError' }); |
| 117 | + expect(aborted).toBe(true); |
| 118 | + } finally { |
| 119 | + fetchSpy.mockRestore(); |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + it('aborts and cancels a streaming response when its body stalls', async () => { |
| 124 | + let aborted = false; |
| 125 | + let cancelled = false; |
| 126 | + const encoder = new TextEncoder(); |
| 127 | + const fetchSpy = spyOn(globalThis, 'fetch').mockImplementation(( |
| 128 | + (_input: string | URL | Request, init?: RequestInit) => { |
| 129 | + init?.signal?.addEventListener('abort', () => { |
| 130 | + aborted = true; |
| 131 | + }, { once: true }); |
| 132 | + |
| 133 | + const body = new ReadableStream<Uint8Array>({ |
| 134 | + start(controller) { |
| 135 | + controller.enqueue(encoder.encode('data: first\n\n')); |
| 136 | + }, |
| 137 | + cancel() { |
| 138 | + cancelled = true; |
| 139 | + }, |
| 140 | + }); |
| 141 | + return Promise.resolve(new Response(body)); |
| 142 | + } |
| 143 | + ) as unknown as typeof fetch); |
| 144 | + |
| 145 | + try { |
| 146 | + const config = makeConfig('https://example.com'); |
| 147 | + const response = await request(config, { |
| 148 | + url: 'https://example.com/stream', |
| 149 | + stream: true, |
| 150 | + timeout: 0.02, |
| 151 | + noAuth: true, |
| 152 | + }); |
| 153 | + const reader = response.body!.getReader(); |
| 154 | + |
| 155 | + expect(new TextDecoder().decode((await reader.read()).value)).toBe('data: first\n\n'); |
| 156 | + await expect(reader.read()).rejects.toMatchObject({ name: 'TimeoutError' }); |
| 157 | + expect(aborted).toBe(true); |
| 158 | + expect(cancelled).toBe(true); |
| 159 | + } finally { |
| 160 | + fetchSpy.mockRestore(); |
| 161 | + } |
| 162 | + }); |
| 163 | + |
| 164 | + it('allows an active stream to outlive a single timeout interval', async () => { |
| 165 | + let aborted = false; |
| 166 | + const encoder = new TextEncoder(); |
| 167 | + const fetchSpy = spyOn(globalThis, 'fetch').mockImplementation(( |
| 168 | + (_input: string | URL | Request, init?: RequestInit) => { |
| 169 | + init?.signal?.addEventListener('abort', () => { |
| 170 | + aborted = true; |
| 171 | + }, { once: true }); |
| 172 | + |
| 173 | + let interval: ReturnType<typeof setInterval> | undefined; |
| 174 | + const body = new ReadableStream<Uint8Array>({ |
| 175 | + start(controller) { |
| 176 | + let chunk = 0; |
| 177 | + interval = setInterval(() => { |
| 178 | + controller.enqueue(encoder.encode(String(chunk))); |
| 179 | + chunk += 1; |
| 180 | + if (chunk === 6) { |
| 181 | + clearInterval(interval); |
| 182 | + controller.close(); |
| 183 | + } |
| 184 | + }, 10); |
| 185 | + }, |
| 186 | + cancel() { |
| 187 | + if (interval) clearInterval(interval); |
| 188 | + }, |
| 189 | + }); |
| 190 | + return Promise.resolve(new Response(body)); |
| 191 | + } |
| 192 | + ) as unknown as typeof fetch); |
| 193 | + |
| 194 | + try { |
| 195 | + const config = makeConfig('https://example.com'); |
| 196 | + const response = await request(config, { |
| 197 | + url: 'https://example.com/stream', |
| 198 | + stream: true, |
| 199 | + timeout: 0.03, |
| 200 | + noAuth: true, |
| 201 | + }); |
| 202 | + const chunks: string[] = []; |
| 203 | + |
| 204 | + for await (const chunk of response.body!) { |
| 205 | + chunks.push(new TextDecoder().decode(chunk)); |
| 206 | + } |
| 207 | + |
| 208 | + expect(chunks).toEqual(['0', '1', '2', '3', '4', '5']); |
| 209 | + expect(aborted).toBe(false); |
| 210 | + } finally { |
| 211 | + fetchSpy.mockRestore(); |
| 212 | + } |
| 213 | + }); |
| 214 | + |
| 215 | + it('cancels the network request when the response consumer cancels', async () => { |
| 216 | + let aborted = false; |
| 217 | + let cancelled = false; |
| 218 | + const fetchSpy = spyOn(globalThis, 'fetch').mockImplementation(( |
| 219 | + (_input: string | URL | Request, init?: RequestInit) => { |
| 220 | + init?.signal?.addEventListener('abort', () => { |
| 221 | + aborted = true; |
| 222 | + }, { once: true }); |
| 223 | + |
| 224 | + const body = new ReadableStream<Uint8Array>({ |
| 225 | + cancel() { |
| 226 | + cancelled = true; |
| 227 | + }, |
| 228 | + }); |
| 229 | + return Promise.resolve(new Response(body)); |
| 230 | + } |
| 231 | + ) as unknown as typeof fetch); |
| 232 | + |
| 233 | + try { |
| 234 | + const config = makeConfig('https://example.com'); |
| 235 | + const response = await request(config, { |
| 236 | + url: 'https://example.com/stream', |
| 237 | + stream: true, |
| 238 | + timeout: 1, |
| 239 | + noAuth: true, |
| 240 | + }); |
| 241 | + |
| 242 | + await response.body!.cancel('consumer stopped'); |
| 243 | + |
| 244 | + expect(aborted).toBe(true); |
| 245 | + expect(cancelled).toBe(true); |
| 246 | + } finally { |
| 247 | + fetchSpy.mockRestore(); |
| 248 | + } |
| 249 | + }); |
96 | 250 | }); |
0 commit comments