diff options
author | Aravind <63452117+codesculpture@users.noreply.github.com> | 2023-11-08 04:22:44 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-07 15:52:44 -0700 |
commit | e4593873a9c791238685dfbb45e64b4485884174 (patch) | |
tree | 5a0d6087dd18a4cfd6ef5b2736c72a5dc584a575 /cli/tests/unit | |
parent | 7978bc5d1b417055c37b90933b25300df577a72f (diff) |
fix(ext/http): Throwing Error if the return value of Deno.serve handler is not a Response class (#21099)
---------
Co-authored-by: Matt Mastracci <matthew@mastracci.com>
Diffstat (limited to 'cli/tests/unit')
-rw-r--r-- | cli/tests/unit/serve_test.ts | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/cli/tests/unit/serve_test.ts b/cli/tests/unit/serve_test.ts index 9f6bd4aa1..59334039a 100644 --- a/cli/tests/unit/serve_test.ts +++ b/cli/tests/unit/serve_test.ts @@ -3805,3 +3805,70 @@ Deno.test( await server.finished; }, ); + +// serve Handler must return Response class or promise that resolves Response class +Deno.test( + { permissions: { net: true, run: true } }, + async function handleServeCallbackReturn() { + const d = deferred(); + const listeningPromise = deferred(); + const ac = new AbortController(); + + const server = Deno.serve( + { + port: servePort, + onListen: onListen(listeningPromise), + signal: ac.signal, + onError: (error) => { + assert(error instanceof TypeError); + assert( + error.message === + "Return value from serve handler must be a response or a promise resolving to a response", + ); + d.resolve(); + return new Response("Customized Internal Error from onError"); + }, + }, + () => { + // Trick the typechecker + return <Response> <unknown> undefined; + }, + ); + await listeningPromise; + const respText = await curlRequest([`http://localhost:${servePort}`]); + await d; + ac.abort(); + await server.finished; + assert(respText === "Customized Internal Error from onError"); + }, +); + +// onError Handler must return Response class or promise that resolves Response class +Deno.test( + { permissions: { net: true, run: true } }, + async function handleServeErrorCallbackReturn() { + const listeningPromise = deferred(); + const ac = new AbortController(); + + const server = Deno.serve( + { + port: servePort, + onListen: onListen(listeningPromise), + signal: ac.signal, + onError: () => { + // Trick the typechecker + return <Response> <unknown> undefined; + }, + }, + () => { + // Trick the typechecker + return <Response> <unknown> undefined; + }, + ); + await listeningPromise; + const respText = await curlRequest([`http://localhost:${servePort}`]); + ac.abort(); + await server.finished; + assert(respText === "Internal Server Error"); + }, +); |