Skip to content

Commit

Permalink
feat: Allow withCredentials for XHR (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
j0Shi82 authored Feb 24, 2024
1 parent ffb213d commit b2f9b0b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
11 changes: 11 additions & 0 deletions scripts/server.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ exports.createServer = function (port, enableAtomics) {
if (url.pathname === '/') {
res.writeHead(301, { Location: '/tests/' });
return res.end();
} else if (url.pathname === '/api/cookie') {
const name = url.searchParams.get('name');
let date = new Date();
date.setTime(date.getTime() + 24 * 60 * 60 * 1000); // 24 hours from now
let expires = date.toUTCString();
res.writeHead(200, {
'Set-Cookie': `${name}=1; Path=/; Domain=localhost; expires=${expires}; SameSite=Lax;`,
'Access-Control-Allow-Origin': req.headers.origin ? req.headers.origin : '*',
'Access-Control-Allow-Credentials': 'true',
});
return res.end();
} else if (url.pathname.endsWith('post')) {
res.writeHead(200);
let body = '';
Expand Down
5 changes: 5 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,11 @@ export interface PartytownConfig {
get?: GetHook;
set?: SetHook;
apply?: ApplyHook;
/**
* When set to true, the Partytown Web Worker will respect the `withCredentials` option of XMLHttpRequests.
* Default: false
*/
allowXhrCredentials?: boolean;
/**
* An absolute path to the root directory which Partytown library files
* can be found. The library path must start and end with a `/`.
Expand Down
6 changes: 5 additions & 1 deletion src/lib/web-worker/worker-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,11 @@ export const createWindow = (
args[1] = resolveUrl(env, args[1], 'xhr');
(super.open as any)(...args);
}
set withCredentials(_: any) {}
set withCredentials(_: boolean) {
if (webWorkerCtx.$config$.allowXhrCredentials) {
super.withCredentials = _;
}
}
toString() {
return str;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/platform/fetch/fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ test('fetch', async ({ page }) => {
const testFetchJson = page.locator('#testFetchJson');
await expect(testFetchJson).toHaveText('{"mph":88}');

await page.waitForSelector('.testFetchCookie');
const testFetchCookie = page.locator('#testFetchCookie');
await expect(testFetchCookie).toContainText('server-test-fetch=1');

await page.waitForSelector('.testXMLHttpRequest');
const testXMLHttpRequest = page.locator('#testXMLHttpRequest');
await expect(testXMLHttpRequest).toHaveText('text');
Expand All @@ -22,4 +26,8 @@ test('fetch', async ({ page }) => {

const testXMLHttpRequestCstrNative = page.locator('#testXMLHttpRequestCstrNative');
await expect(testXMLHttpRequestCstrNative).toHaveText('true');

await page.waitForSelector('.testXMLHttpRequestCookie');
const testXMLHttpRequestCookie = page.locator('#testXMLHttpRequestCookie');
await expect(testXMLHttpRequestCookie).toContainText('server-test-xhr=1');
});
39 changes: 39 additions & 0 deletions tests/platform/fetch/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ <h1>fetch() / XMLHttpRequest</h1>
</script>
</li>

<li>
<strong>fetch Set-Cookie</strong>
<code>
<span id="testFetchCookie"></span>
</code>
<script type="text/partytown">
(async function () {
const url = new URL('/api/cookie?name=server-test-fetch', location.origin);
const elm = document.getElementById('testFetchCookie');
const rsp = await fetch(url, {
credentials: 'include',
});
elm.textContent = document.cookie;
elm.className = 'testFetchCookie';
})();
</script>
</li>

<li>
<strong>XMLHttpRequest</strong>
<code id="testXMLHttpRequest"></code>
Expand Down Expand Up @@ -161,6 +179,27 @@ <h1>fetch() / XMLHttpRequest</h1>
</script>
</li>

<li>
<strong>XMLHttpRequest Set-Cookie</strong>
<code>
<span id="testXMLHttpRequestCookie"></span>
</code>
<script type="text/partytown">
(async function () {
const url = new URL('/api/cookie?name=server-test-xhr', location.origin);
const elm = document.getElementById('testXMLHttpRequestCookie');
const xhr = new XMLHttpRequest();
xhr.addEventListener('load', function () {
elm.textContent = document.cookie;
elm.className = 'testXMLHttpRequestCookie';
});
xhr.open('GET', url);
xhr.withCredentials = true;
xhr.send();
})();
</script>
</li>

<script type="text/partytown">
(function () {
document.body.classList.add('completed');
Expand Down

0 comments on commit b2f9b0b

Please sign in to comment.