Skip to content

Commit

Permalink
Merge pull request #25 from barisesen/original-url
Browse files Browse the repository at this point in the history
originalurl header added for client async mode
  • Loading branch information
mehmetsefabalik committed Apr 13, 2021
2 parents e3c8a45 + 30ee85b commit 3d74066
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@puzzle-js/client-lib",
"main": "dist/index.js",
"version": "1.4.3",
"version": "1.4.4",
"author": "<emre.kul@trendyol.com>",
"license": "MIT",
"repository": {
Expand Down
3 changes: 3 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ export class Core extends Module {
const queryString = this.prepareQueryString(fragment.attributes);
const fragmentRequestUrl = `${fragment.source}${window.location.pathname}${queryString}`;
return fetch(fragmentRequestUrl, {
headers: {
originalurl: window.location.pathname
},
credentials: 'include'
})
.then(res => {
Expand Down
14 changes: 9 additions & 5 deletions test/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {JSDOM} from "jsdom";
import {PuzzleJs} from "../src/puzzle";
import {Core} from "../src/core";
import {createPageLibConfiguration} from "./mock";
import sinon from "sinon";
import sinon, { SinonStub } from "sinon";
import {AssetHelper} from "../src/assetHelper";
import * as faker from "faker";
import {IPageLibAsset, IPageLibConfiguration, IPageLibDependency} from "../src/types";
Expand All @@ -22,17 +22,20 @@ declare global {
export interface Global {
document: Document;
window: Window;
fetch: any;
}

declare var global: Global;

describe('Module - Core', () => {
beforeEach(() => {
global.window = (new JSDOM(``, {runScripts: "outside-only"})).window;
global.window = (new JSDOM(``, { runScripts: "outside-only" })).window;
global.fetch = sandbox.stub().resolves({json: () => {}});
});

afterEach(() => {
delete global.window;
delete global.fetch;
PuzzleJs.clearListeners();
sandbox.verifyAndRestore();
(Core as any)._pageConfiguration = undefined;
Expand Down Expand Up @@ -210,14 +213,15 @@ describe('Module - Core', () => {
fragmentContainer.setAttribute('puzzle-fragment', 'test');
global.window.document.body.appendChild(fragmentContainer);

const stubFetchGatewayFragment = sandbox.stub(Core as any, "fetchGatewayFragment").resolves();
const fetchStub = global.fetch as SinonStub;
const stubAsyncRenderResponse = sandbox.stub(Core as any, "asyncRenderResponse").resolves();

Core.config(JSON.stringify(config));
await Core.renderAsyncFragment('test');
await Core.renderAsyncFragment('test');

expect(stubFetchGatewayFragment.calledOnce).to.eq(true);

expect(fetchStub.calledOnce).to.eq(true);
expect(fetchStub.getCall(0).lastArg.headers).to.haveOwnProperty("originalurl");
expect(stubAsyncRenderResponse.calledOnce).to.eq(true);
});
});

0 comments on commit 3d74066

Please sign in to comment.