Skip to content

Commit

Permalink
[sitecore-jss-nextjs]: redirects middleware and unit-test has been f…
Browse files Browse the repository at this point in the history
…ixed
  • Loading branch information
Rmatkovsky committed Aug 12, 2024
1 parent e28193b commit c5b6e16
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 31 deletions.
154 changes: 126 additions & 28 deletions packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,16 +414,24 @@ describe('RedirectsMiddleware', () => {

it('should override locale with locale parsed from target', async () => {
const setCookies = () => {};
const cloneUrl = () => {
return Object.assign({}, req.nextUrl);
};
const res = createResponse({
url: 'http://localhost:3000/ua/found',
status: 301,
url: {
pathname: 'http://localhost:3000/ua/found',
href: 'http://localhost:3000/not-found',
locale: 'en',
clone: cloneUrl,
},
status: 200,
setCookies,
});
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const nextRewriteStub = sinon.stub(NextResponse, 'rewrite').callsFake((url, _init) => {
return ({
url,
status: 301,
status: 200,
cookies: { set: setCookies },
headers: res.headers,
} as unknown) as NextResponse;
Expand All @@ -433,9 +441,7 @@ describe('RedirectsMiddleware', () => {
pathname: '/not-found',
href: 'http://localhost:3000/not-found',
locale: 'en',
clone() {
return Object.assign({}, req.nextUrl);
},
clone: cloneUrl,
},
});

Expand All @@ -456,10 +462,17 @@ describe('RedirectsMiddleware', () => {
});

validateEndMessageDebugLog('redirects middleware end in %dms: %o', {
headers: {},
headers: {
'x-sc-rewrite': 'http://localhost:3000/ua/found',
},
redirected: undefined,
status: 301,
url: 'http://localhost:3000/ua/found',
status: 200,
url: {
pathname: 'http://localhost:3000/ua/found',
href: 'http://localhost:3000/not-found',
locale: 'en',
clone: cloneUrl,
},
});

expect(siteResolver.getByHost).to.be.calledWith(hostname);
Expand All @@ -473,15 +486,25 @@ describe('RedirectsMiddleware', () => {

it('should preserve query string on relative path redirect, when isQueryStringPreserved is true', async () => {
const setCookies = () => {};
const cloneUrl = () => {
return Object.assign({}, req.nextUrl);
};
const res = createResponse({
url: 'http://localhost:3000/found?abc=def',
status: 301,
url: {
origin: 'http://localhost:3000',
pathname: 'http://localhost:3000/found?abc=def',
href: 'http://localhost:3000/not-found?abc=def',
search: '?abc=def',
locale: 'en',
clone: cloneUrl,
},
status: 200,
setCookies,
});
const nextRewriteStub = sinon.stub(NextResponse, 'rewrite').callsFake((url) => {
return ({
url,
status: 301,
status: 200,
cookies: { set: setCookies },
headers: res.headers,
} as unknown) as NextResponse;
Expand All @@ -490,16 +513,14 @@ describe('RedirectsMiddleware', () => {
nextUrl: {
origin: 'http://localhost:3000',
pathname: '/not-found',
search: 'abc=def',
search: '?abc=def',
href: 'http://localhost:3000/not-found?abc=def',
clone() {
return Object.assign({}, req.nextUrl);
},
clone: cloneUrl,
},
});

const { middleware, fetchRedirects, siteResolver } = createMiddleware({
pattern: 'not-found',
pattern: 'not-found?abc=def',
target: 'found',
redirectType: REDIRECT_TYPE_SERVER_TRANSFER,
isQueryStringPreserved: true,
Expand All @@ -514,10 +535,19 @@ describe('RedirectsMiddleware', () => {
});

validateEndMessageDebugLog('redirects middleware end in %dms: %o', {
headers: {},
headers: {
'x-sc-rewrite': 'http://localhost:3000/found?abc=def',
},
redirected: undefined,
status: 301,
url: 'http://localhost:3000/found?abc=def',
status: 200,
url: {
origin: 'http://localhost:3000',
pathname: 'http://localhost:3000/found?abc=def',
href: 'http://localhost:3000/not-found?abc=def',
search: '?abc=def',
locale: 'en',
clone: cloneUrl,
},
});

expect(siteResolver.getByHost).to.be.calledWith(hostname);
Expand Down Expand Up @@ -865,8 +895,16 @@ describe('RedirectsMiddleware', () => {

it('should rewrite path when redirect type is server transfer', async () => {
const setCookies = () => {};
const cloneUrl = () => {
return Object.assign({}, req.nextUrl);
};
const res = createResponse({
url: 'http://localhost:3000/found',
url: {
clone: cloneUrl,
href: 'http://localhost:3000/not-found',
locale: 'en',
pathname: 'http://localhost:3000/found',
},
setCookies,
});
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -882,9 +920,7 @@ describe('RedirectsMiddleware', () => {
pathname: '/not-found',
href: 'http://localhost:3000/not-found',
locale: 'en',
clone() {
return Object.assign({}, req.nextUrl);
},
clone: cloneUrl,
},
});

Expand All @@ -905,10 +941,17 @@ describe('RedirectsMiddleware', () => {
});

validateEndMessageDebugLog('redirects middleware end in %dms: %o', {
headers: {},
headers: {
'x-sc-rewrite': 'http://localhost:3000/found',
},
redirected: undefined,
status: undefined,
url: 'http://localhost:3000/found',
url: {
clone: cloneUrl,
href: 'http://localhost:3000/not-found',
locale: 'en',
pathname: 'http://localhost:3000/found',
},
});

expect(siteResolver.getByHost).to.be.calledWith(hostname);
Expand All @@ -921,7 +964,7 @@ describe('RedirectsMiddleware', () => {

it('should use sc_site cookie', async () => {
const siteName = 'foo';
const res = NextResponse.rewrite('http://localhost:3000/found');
const res = NextResponse.redirect('http://localhost:3000/found');
res.cookies.set('sc_site', siteName);
const req = createRequest({
nextUrl: {
Expand All @@ -945,6 +988,7 @@ describe('RedirectsMiddleware', () => {
const expected = NextResponse.redirect('http://localhost:3000/found', {
...res,
status: 301,
headers: { ...res?.headers },
});

const finalRes = await middleware.getHandler()(req, res);
Expand All @@ -958,6 +1002,7 @@ describe('RedirectsMiddleware', () => {
validateEndMessageDebugLog('redirects middleware end in %dms: %o', {
headers: {
location: 'http://localhost:3000/found',
'set-cookie': 'sc_site=foo; Path=/',
},
redirected: false,
status: 301,
Expand All @@ -967,7 +1012,6 @@ describe('RedirectsMiddleware', () => {
expect(siteResolver.getByHost).not.called.to.equal(true);
expect(siteResolver.getByName).to.be.calledWith(siteName);
expect(fetchRedirects).to.be.calledWith(siteName);
expect(finalRes).to.deep.equal(expected);
expect(finalRes.status).to.equal(expected.status);
});

Expand Down Expand Up @@ -1251,6 +1295,60 @@ describe('RedirectsMiddleware', () => {

nextRedirectStub.restore();
});

it('should return 301 redirect when user click on the link', async () => {
const siteName = 'foo';
const res = NextResponse.redirect('http://localhost:3000/found');
res.cookies.set('sc_site', siteName);
const req = createRequest({
nextUrl: {
href: 'http://localhost:3000/not-found?path=not-found',
pathname: '/not-found',
locale: 'en',
search: '?path=not-found',
clone() {
return Object.assign({}, req.nextUrl);
},
},
});

const { middleware, fetchRedirects, siteResolver } = createMiddleware({
pattern: 'not-found',
target: 'http://localhost:3000/found',
redirectType: REDIRECT_TYPE_301,
isQueryStringPreserved: false,
locale: 'en',
});

const expected = NextResponse.redirect('http://localhost:3000/found', {
...res,
status: 301,
headers: { ...res?.headers },
});

const finalRes = await middleware.getHandler()(req, res);

validateDebugLog('redirects middleware start: %o', {
hostname: 'foo.net',
language: 'en',
pathname: '/not-found',
});

validateEndMessageDebugLog('redirects middleware end in %dms: %o', {
headers: {
location: 'http://localhost:3000/found',
'set-cookie': 'sc_site=foo; Path=/',
},
redirected: false,
status: 301,
url: '',
});

expect(siteResolver.getByHost).not.called.to.equal(true);
expect(siteResolver.getByName).to.be.calledWith(siteName);
expect(fetchRedirects).to.be.calledWith(siteName);
expect(finalRes.status).to.equal(expected.status);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,18 @@ export class RedirectsMiddleware extends MiddlewareBase {
...res,
status: 301,
statusText: 'Moved Permanently',
headers: res?.headers,
});
case REDIRECT_TYPE_302:
return NextResponse.redirect(redirectUrl, {
...res,
status: 302,
statusText: 'Found',
headers: res?.headers,
});
case REDIRECT_TYPE_SERVER_TRANSFER:
return NextResponse.rewrite(redirectUrl, res);
case REDIRECT_TYPE_SERVER_TRANSFER: {
return this.rewrite(redirectUrl, req, res || new NextResponse());
}
default:
return res || NextResponse.next();
}
Expand Down Expand Up @@ -204,7 +207,7 @@ export class RedirectsMiddleware extends MiddlewareBase {
.replace(/^\^\/|\/\$$/g, '')
.replace(/^\^|\$$/g, '')
.replace(/(?<!\\)\?/g, '\\?')
.replace(/\$\/gi$/g, '')}[\/]?/gi`;
.replace(/\$\/gi$/g, '')}[\/]?$/gi`;

return (
(regexParser(redirect.pattern).test(tragetURL) ||
Expand Down

0 comments on commit c5b6e16

Please sign in to comment.