Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {getCachedRedirectResponse} from '@/middleware/utils/getCachedRedirectRes

import {getRedirects} from '../index';

const studioBaseUrl = 'https://studio.code.org';
jest.mock('@/config/studio', () => ({
getStudioBaseUrl: jest.fn(() => 'https://studio.code.org'),
getStudioBaseUrl: jest.fn(() => studioBaseUrl),
}));
jest.mock('@/middleware/utils/getCachedRedirectResponse', () => ({
getCachedRedirectResponse: jest.fn((url, opts) => ({
Expand All @@ -14,10 +15,15 @@ jest.mock('@/middleware/utils/getCachedRedirectResponse', () => ({
})),
}));

function createMockRequest(pathname: string, origin = 'https://example.com') {
function createMockRequest(
pathname: string,
search = '',
origin = 'https://example.com',
) {
return {
nextUrl: {
pathname,
search,
origin,
},
} as unknown as NextRequest;
Expand Down Expand Up @@ -62,6 +68,31 @@ describe('getRedirects', () => {
);
});

it('redirects /congrats/:course_name to studio.code.org/congrats/:course_name', () => {
const reqPath = '/congrats/course_name';
const req = createMockRequest(reqPath);

getRedirects(req);

expect(getCachedRedirectResponse).toHaveBeenCalledWith(
new URL(reqPath, studioBaseUrl),
{status: 308},
);
});

it('redirects /congrats?s=course_name_base64 to studio.code.org/congrats?s=course_name_base64', () => {
const reqPath = '/congrats';
const reqQuery = '?s=course_name_base64';
const req = createMockRequest(reqPath, reqQuery);

getRedirects(req);

expect(getCachedRedirectResponse).toHaveBeenCalledWith(
new URL(reqPath + reqQuery, studioBaseUrl),
{status: 308},
);
});

it('returns undefined for unrelated paths', () => {
const req = createMockRequest('/other/path');
const result = getRedirects(req);
Expand Down
8 changes: 8 additions & 0 deletions apps/marketing/src/middleware/redirects/corporate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {getCachedRedirectResponse} from '@/middleware/utils/getCachedRedirectRes

export function getRedirects(request: NextRequest) {
const fullPath = request.nextUrl.pathname;
const urlQuery = request.nextUrl.search;
const pathParts = fullPath.split('/').filter(Boolean);

const maybeLocale = pathParts[0];
Expand Down Expand Up @@ -47,4 +48,11 @@ export function getRedirects(request: NextRequest) {

return getCachedRedirectResponse(redirectUrl, {status: 308});
}

// Permanently redirect /congrats/*?s=course_name_base64 to studio.code.org/congrats/*?s=course_name_base64
if (pathParts[0] === 'congrats') {
const redirectUrl = new URL(fullPath + urlQuery, getStudioBaseUrl());

return getCachedRedirectResponse(redirectUrl, {status: 308});
}
}
Loading