Skip to content

Commit

Permalink
Remove special case for test
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek3255 committed Jan 22, 2024
1 parent 0bb3d22 commit da7ad50
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 54 deletions.
2 changes: 1 addition & 1 deletion bundle/bundle.js

Large diffs are not rendered by default.

10 changes: 0 additions & 10 deletions lib/build/fetch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lib/build/utils/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 1 addition & 11 deletions lib/build/utils/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions lib/ts/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import CookieHandlerReference from "./utils/cookieHandler";
import WindowHandlerReference from "./utils/windowHandler";
import LockFactoryReference from "./utils/lockFactory";
import { logDebugMessage } from "./logger";
import { isTest } from "./utils";

export class AntiCsrfToken {
private static tokenInfo:
Expand Down Expand Up @@ -251,14 +250,6 @@ export default class AuthHttpRequest {
}

logDebugMessage("doRequest: Value of doNotDoInterception: " + doNotDoInterception);

// isomorphic-fetch uses node-fetch during tests and node-fetch does not work with relative URLs
// so if we are in test mode and the URL is a relative URL that starts with /interception we just directly
// return the value doNotDoInterception
if (isTest() && typeof url === "string" && url.startsWith("/interception")) {
return new Response(`{"doNotDoInterception": ${doNotDoInterception}}`);
}

if (doNotDoInterception) {
logDebugMessage("doRequest: Returning without interception");
return await httpCall(config);
Expand Down
9 changes: 0 additions & 9 deletions lib/ts/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,3 @@ export function getNormalisedUserContext(userContext?: any): any {

return userContext;
}

export function isTest(): boolean {
try {
return process.env.TEST_MODE === "testing";
} catch (err) {
// can get Uncaught ReferenceError: process is not defined error
return false;
}
}
49 changes: 36 additions & 13 deletions test/fetch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,6 @@ describe("Fetch AuthHttpRequest class tests", function () {
}
});

it("correctly intercepts requests with relative URLs", async function () {
AuthHttpRequest.init({
apiDomain: "http://localhost"
});

let getResponse = await fetch(`/interception`, {
method: "GET"
});

const res = await getResponse.json();
assert.strictEqual(res.doNotDoInterception, false);
});

it("testing with fetch api methods without config", async function () {
AuthHttpRequest.init({
apiDomain: BASE_URL
Expand Down Expand Up @@ -2338,4 +2325,40 @@ describe("Fetch AuthHttpRequest class tests", function () {
await browser.close();
}
});

it("test that relative URLs get intercepted if frontend and backend are on same domain", async function () {
await startST(3);
const browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"]
});
try {
const page = await browser.newPage();
await page.goto(BASE_URL + "/index.html", { waitUntil: "load" });
await page.addScriptTag({ path: `./bundle/bundle.js`, type: "text/javascript" });
await page.evaluate(async () => {
let BASE_URL = "http://localhost.org:8080";
supertokens.init({
apiDomain: BASE_URL
});
let userId = "testing-supertokens-website";

let loginResponse = await fetch(`/login`, {
method: "post",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({ userId })
});

assertEqual(await loginResponse.text(), userId);

let getResponse = await fetch(`/check-rid`);

assertEqual(await getResponse.text(), "success");
});
} finally {
await browser.close();
}
});
});

0 comments on commit da7ad50

Please sign in to comment.