Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Apr 22, 2024
1 parent 92a4441 commit a83d1bb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ jobs:

- name: Run tests
run: npm test

- name: Run tests with fetch()
run: npm run test:node-fetch
if: ${{ matrix.node-version == '20' }}
63 changes: 31 additions & 32 deletions lib/transports/polling-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,35 @@ export class Fetch extends Polling {
}
}

override async doPoll() {
try {
const res = await this._fetch();
override doPoll() {
this._fetch()
.then((res) => {
if (!res.ok) {
return this.onError("fetch read error", res.status, res);
}

if (!res.ok) {
return this.onError("fetch read error", res.status, res);
}

const data = await res.text();
this.onData(data);
} catch (e) {
this.onError("fetch read error", e);
}
res.text().then((data) => this.onData(data));
})
.catch((err) => {
this.onError("fetch read error", err);
});
}

override async doWrite(data: string, callback: () => void) {
try {
const res = await this._fetch(data);
override doWrite(data: string, callback: () => void) {
this._fetch(data)
.then((res) => {
if (!res.ok) {
return this.onError("fetch write error", res.status, res);
}

if (!res.ok) {
return this.onError("fetch write error", res.status, res);
}

callback();
} catch (e) {
this.onError("fetch write error", e);
}
callback();
})
.catch((err) => {
this.onError("fetch write error", err);
});
}

private async _fetch(data?: string) {
private _fetch(data?: string) {
const isPost = data !== undefined;
const headers = new Headers(this.opts.extraHeaders);

Expand All @@ -56,18 +55,18 @@ export class Fetch extends Polling {

this.cookieJar?.appendCookies(headers);

const res = await fetch(this.uri(), {
return fetch(this.uri(), {
method: isPost ? "POST" : "GET",
body: isPost ? data : null,
headers,
credentials: this.opts.withCredentials ? "include" : "omit",
});

if (this.cookieJar) {
// @ts-ignore getSetCookie() was added in Node.js v19.7.0
this.cookieJar.parseCookies(res.headers.getSetCookie());
}
}).then((res) => {
if (this.cookieJar) {
// @ts-ignore getSetCookie() was added in Node.js v19.7.0
this.cookieJar.parseCookies(res.headers.getSetCookie());
}

return res;
return res;
});
}
}
2 changes: 1 addition & 1 deletion lib/transports/xmlhttprequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class CookieJar {

if (cookies.length) {
xhr.setDisableHeaderCheck(true);
xhr.set("cookie", cookies.join("; "));
xhr.setRequestHeader("cookie", cookies.join("; "));
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
},
"scripts": {
"compile": "rimraf ./build && tsc && tsc -p tsconfig.esm.json && ./postcompile.sh",
"test": "npm run format:check && npm run compile && if test \"$BROWSERS\" = \"1\" ; then npm run test:browser; else npm run test:node && npm run test:node-fetch; fi",
"test": "npm run format:check && npm run compile && if test \"$BROWSERS\" = \"1\" ; then npm run test:browser; else npm run test:node; fi",
"test:node": "mocha --bail --require test/support/hooks.js test/index.js test/webtransport.mjs",
"test:node-fetch": "USE_FETCH=1 npm run test:node",
"test:browser": "zuul test/index.js",
Expand Down
4 changes: 3 additions & 1 deletion test/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ describe("connection", function () {

if (env.browser && typeof addEventListener === "function") {
it("should close the socket when receiving a beforeunload event", (done) => {
const socket = new Socket();
const socket = new Socket({
closeOnBeforeunload: true,
});

const createEvent = (name) => {
if (typeof Event === "function") {
Expand Down

0 comments on commit a83d1bb

Please sign in to comment.