diff options
Diffstat (limited to 'test/bun.js/fetch.test.js')
-rw-r--r-- | test/bun.js/fetch.test.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/test/bun.js/fetch.test.js b/test/bun.js/fetch.test.js index 8758e4889..46409633c 100644 --- a/test/bun.js/fetch.test.js +++ b/test/bun.js/fetch.test.js @@ -8,6 +8,66 @@ const exampleFixture = fs.readFileSync( "utf8", ); +let server ; +beforeAll(() => { + server = Bun.serve({ + async fetch(){ + await Bun.sleep(2000); + return new Response("Hello") + }, + port: 64321 + }); + +}); +afterAll(() => { + server.stop(); +}); + + +describe("AbortSignal", ()=> { + it("AbortError", async ()=> { + let name; + try { + var controller = new AbortController(); + const signal = controller.signal; + + async function manualAbort(){ + await Bun.sleep(10); + controller.abort(); + } + await Promise.all([fetch("http://127.0.0.1:64321", { signal: signal }).then((res)=> res.text()), manualAbort()]); + } catch (error){ + name = error.name; + } + expect(name).toBe("AbortError"); + }) + it("AbortErrorWithReason", async ()=> { + let reason; + try { + var controller = new AbortController(); + const signal = controller.signal; + async function manualAbort(){ + await Bun.sleep(10); + controller.abort("My Reason"); + } + await Promise.all([fetch("http://127.0.0.1:64321", { signal: signal }).then((res)=> res.text()), manualAbort()]); + } catch (error){ + reason = error + } + expect(reason).toBe("My Reason"); + }) + it("TimeoutError", async ()=> { + let name; + try { + const signal = AbortSignal.timeout(10); + await fetch("http://127.0.0.1:64321", { signal: signal }).then((res)=> res.text()); + } catch (error){ + name = error.name; + } + expect(name).toBe("TimeoutError"); + }) +}) + describe("Headers", () => { it(".toJSON", () => { var headers = new Headers({ |