aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/fetch.test.js
diff options
context:
space:
mode:
authorGravatar Ciro Spaciari <ciro.spaciari@gmail.com> 2023-02-15 19:20:40 -0300
committerGravatar GitHub <noreply@github.com> 2023-02-15 14:20:40 -0800
commit597053ea91a83ed0ce98bf1b8bb59fbf282c3619 (patch)
tree51141e81e587d8ce747fe81fb6064f5740d5bc40 /test/bun.js/fetch.test.js
parent1c221d33b0def3810c262bd19a4bf8060389dae0 (diff)
downloadbun-597053ea91a83ed0ce98bf1b8bb59fbf282c3619.tar.gz
bun-597053ea91a83ed0ce98bf1b8bb59fbf282c3619.tar.zst
bun-597053ea91a83ed0ce98bf1b8bb59fbf282c3619.zip
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
Diffstat (limited to 'test/bun.js/fetch.test.js')
-rw-r--r--test/bun.js/fetch.test.js60
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({