Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: detection of client premature close #218

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ export const getRequestListener = (
outgoing.on('close', () => {
if (incoming.errored) {
req[getAbortController]().abort(incoming.errored.toString())
} else if (!outgoing.writableFinished) {
req[getAbortController]().abort('Client connection prematurely closed.')
}
})

Expand Down
126 changes: 66 additions & 60 deletions test/listener.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,94 +180,100 @@ describe('Abort request', () => {
server.close()
})

it('should emit an abort event when the nodejs request is aborted', async () => {
const requests: Request[] = []
const abortedPromise = new Promise<void>((resolve) => {
onAbort = (req) => {
requests.push(req)
resolve()
}
})

const req = request(server)
.get('/abort')
.end(() => {})

await reqReadyPromise

req.abort()

await abortedPromise

expect(requests).toHaveLength(1)
const abortedReq = requests[0]
expect(abortedReq).toBeInstanceOf(Request)
expect(abortedReq.signal.aborted).toBe(true)
})

it('should emit an abort event when the nodejs request is aborted on multiple requests', async () => {
const requests: Request[] = []

{
it.each(['get', 'put', 'patch', 'delete'] as const)(
'should emit an abort event when the nodejs %s request is aborted',
async (method) => {
const requests: Request[] = []
const abortedPromise = new Promise<void>((resolve) => {
onAbort = (req) => {
requests.push(req)
resolve()
}
})

reqReadyPromise = new Promise<void>((r) => {
reqReadyResolve = r
})

const req = request(server)
.get('/abort')
[method]('/abort')
.end(() => {})

await reqReadyPromise

req.abort()

await abortedPromise
}

expect(requests).toHaveLength(1)

for (const abortedReq of requests) {
expect(requests).toHaveLength(1)
const abortedReq = requests[0]
expect(abortedReq).toBeInstanceOf(Request)
expect(abortedReq.signal.aborted).toBe(true)
}
)

{
const abortedPromise = new Promise<void>((resolve) => {
onAbort = (req) => {
requests.push(req)
resolve()
}
})
it.each(['get', 'post', 'head', 'patch', 'delete', 'put'] as const)(
'should emit an abort event when the nodejs request is aborted on multiple %s requests',
async (method) => {
const requests: Request[] = []

reqReadyPromise = new Promise<void>((r) => {
reqReadyResolve = r
})
{
const abortedPromise = new Promise<void>((resolve) => {
onAbort = (req) => {
requests.push(req)
resolve()
}
})

const req = request(server)
.get('/abort')
.end(() => {})
reqReadyPromise = new Promise<void>((r) => {
reqReadyResolve = r
})

await reqReadyPromise
const req = request(server)
[method]('/abort')
.end(() => {})

req.abort()
await reqReadyPromise

await abortedPromise
}
req.abort()

expect(requests).toHaveLength(2)
await abortedPromise
}

for (const abortedReq of requests) {
expect(abortedReq).toBeInstanceOf(Request)
expect(abortedReq.signal.aborted).toBe(true)
expect(requests).toHaveLength(1)

for (const abortedReq of requests) {
expect(abortedReq).toBeInstanceOf(Request)
expect(abortedReq.signal.aborted).toBe(true)
}

{
const abortedPromise = new Promise<void>((resolve) => {
onAbort = (req) => {
requests.push(req)
resolve()
}
})

reqReadyPromise = new Promise<void>((r) => {
reqReadyResolve = r
})

const req = request(server)
[method]('/abort')
.end(() => {})

await reqReadyPromise

req.abort()

await abortedPromise
}

expect(requests).toHaveLength(2)

for (const abortedReq of requests) {
expect(abortedReq).toBeInstanceOf(Request)
expect(abortedReq.signal.aborted).toBe(true)
}
}
})
)
})

describe('overrideGlobalObjects', () => {
Expand Down