From 597053ea91a83ed0ce98bf1b8bb59fbf282c3619 Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Wed, 15 Feb 2023 19:20:40 -0300 Subject: feat(fetch) AbortSignal (#2019) * add fetch abort signal * get aborted (still segfaults) * bidings.zig u0 error * still GC/memory error * fix start crash * fix AbortSignal fromJS * change fromJS to obj.as * addAbortSignalEventListenner * handle abort types, and add tests * fix tests * add custom reason test * merge 2 substring methods, use MAKE_STATIC_STRING_IMPL * fix create AbortError and TimeoutError, move globalThis and exception creation to main thread * fix tests and rebuild headers * no need to check with substring reason is already an exception * no need to check with substring reason is already an exception * fix dumb error inverting conditions for check reason * fix custom reason behavior --- test/bun.js/fetch.test.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'test/bun.js/fetch.test.js') 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({ -- cgit v1.2.3