Skip to content

Commit

Permalink
Add similar tests for fetch and xhr
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek3255 committed Jan 22, 2024
1 parent 3da383f commit d4c77de
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
34 changes: 34 additions & 0 deletions test/axios.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,40 @@ describe("Axios 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.addAxiosInterceptors(axios);
supertokens.init({
apiDomain: BASE_URL
});
let userId = "testing-supertokens-website";
let loginResponse = await axios.post(`/login`, JSON.stringify({ userId }), {
headers: {
Accept: "application/json",
"Content-Type": "application/json"
}
});
let userIdFromResponse = loginResponse.data;
assertEqual(userId, userIdFromResponse);

let checkRidResponse = await axios({ url: `/check-rid`, method: "GET" });

assertEqual(await checkRidResponse.data, "success");
});
} finally {
await browser.close();
}
});
});

function addAxiosInterceptorsTest(axiosInstance) {
Expand Down
4 changes: 2 additions & 2 deletions test/fetch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2353,9 +2353,9 @@ describe("Fetch AuthHttpRequest class tests", function () {

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

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

assertEqual(await getResponse.text(), "success");
assertEqual(await checkRidResponse.text(), "success");
});
} finally {
await browser.close();
Expand Down
43 changes: 41 additions & 2 deletions test/xmlhttprequest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const axios = require("axios");

const puppeteer = require("puppeteer");
const assert = require("assert");
const { BASE_URL, BASE_URL_FOR_ST } = require("./utils.js");
const { BASE_URL, BASE_URL_FOR_ST, startST } = require("./utils.js");
const { spawn } = require("child_process");

describe("Axios AuthHttpRequest class tests header", function () {
describe("XmlHttpRequest tests", function () {
let browser, page;
before(async function () {
spawn(
Expand Down Expand Up @@ -123,4 +123,43 @@ describe("Axios AuthHttpRequest class tests header", function () {
assert.strictEqual(errors.length, 0);
});
});

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 loginRequest = new XMLHttpRequest();
loginRequest.open("POST", `/login`);
loginRequest.setRequestHeader("Content-Type", "application/json");
loginRequest.setRequestHeader("Accept", "application/json");
loginRequest.send(JSON.stringify({ userId }));
await new Promise(res => {
loginRequest.onload = res;
});
assertEqual(loginRequest.responseText, userId);

let checkRidRequest = new XMLHttpRequest();
checkRidRequest.open("GET", `/check-rid`);
checkRidRequest.send();
await new Promise(res => {
checkRidRequest.onload = res;
});
assertEqual(checkRidRequest.responseText, "success");
});
} finally {
await browser.close();
}
});
});

0 comments on commit d4c77de

Please sign in to comment.