0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-25 13:19:30 +01:00
hono/runtime_tests/deno/stream.test.ts
Taku Amano ada10e5bf0
fix(stream): Fixed a problem that onAbort() is called even if request is normally closed in deno (#3079)
* Fix(stream): Fixed a problem that onAbort() is called even if request is normally closed in deno

* test: fix stream abort test for Node.js
2024-07-03 10:50:44 +09:00

110 lines
2.9 KiB
TypeScript

import { Hono } from '../../src/hono.ts'
import { assertEquals } from './deps.ts'
import { stream, streamSSE } from '../../src/helper/streaming/index.ts'
Deno.test('Shuld call onAbort via stream', async () => {
const app = new Hono()
let aborted = false
app.get('/stream', (c) => {
return stream(c, async (stream) => {
stream.onAbort(() => {
aborted = true
})
return new Promise<void>((resolve) => {
stream.onAbort(resolve)
})
})
})
const server = Deno.serve({ port: 0 }, app.fetch)
const ac = new AbortController()
const req = new Request(`http://localhost:${server.addr.port}/stream`, {
signal: ac.signal,
})
assertEquals
const res = fetch(req).catch(() => {})
assertEquals(aborted, false)
await new Promise((resolve) => setTimeout(resolve, 10))
ac.abort()
await res
while (!aborted) {
await new Promise((resolve) => setTimeout(resolve))
}
assertEquals(aborted, true)
await server.shutdown()
})
Deno.test('Shuld not call onAbort via stream if already closed', async () => {
const app = new Hono()
let aborted = false
app.get('/stream', (c) => {
return stream(c, async (stream) => {
stream.onAbort(() => {
aborted = true
})
await stream.write('Hello')
})
})
const server = Deno.serve({ port: 0 }, app.fetch)
assertEquals(aborted, false)
const res = await fetch(`http://localhost:${server.addr.port}/stream`)
assertEquals(await res.text(), 'Hello')
assertEquals(aborted, false)
await server.shutdown()
})
Deno.test('Shuld call onAbort via streamSSE', async () => {
const app = new Hono()
let aborted = false
app.get('/stream', (c) => {
return streamSSE(c, async (stream) => {
stream.onAbort(() => {
aborted = true
})
return new Promise<void>((resolve) => {
stream.onAbort(resolve)
})
})
})
const server = Deno.serve({ port: 0 }, app.fetch)
const ac = new AbortController()
const req = new Request(`http://localhost:${server.addr.port}/stream`, {
signal: ac.signal,
})
assertEquals
const res = fetch(req).catch(() => {})
assertEquals(aborted, false)
await new Promise((resolve) => setTimeout(resolve, 10))
ac.abort()
await res
while (!aborted) {
await new Promise((resolve) => setTimeout(resolve))
}
assertEquals(aborted, true)
await server.shutdown()
})
Deno.test('Shuld not call onAbort via streamSSE if already closed', async () => {
const app = new Hono()
let aborted = false
app.get('/stream', (c) => {
return streamSSE(c, async (stream) => {
stream.onAbort(() => {
aborted = true
})
await stream.write('Hello')
})
})
const server = Deno.serve({ port: 0 }, app.fetch)
assertEquals(aborted, false)
const res = await fetch(`http://localhost:${server.addr.port}/stream`)
assertEquals(await res.text(), 'Hello')
assertEquals(aborted, false)
await server.shutdown()
})