Files
caprover/tests/RedirectStatusCodePersistence.test.ts
T
Han Ming 7628860532 Allow 301 for "Redirect all domains to" (#2562)
Add an optional redirectDomainStatusCode (301 or 302) to the app
definition. The default server block template returns it instead of a
hard-coded 302. Apps without the field keep redirecting with 302.

Clients that do not send the field, such as the current caprover-api
updateConfigAndSave, keep the stored value instead of resetting it.

Refs #1857
2026-11-28 08:27:48 -08:00

54 lines
1.4 KiB
TypeScript

import AppsDataStore from '../src/datastore/AppsDataStore'
async function saveWithStatusCode(
storedCode: number | undefined,
sentCode: number | undefined
) {
const appsDataStore = new AppsDataStore({} as any, 'captain')
jest.spyOn(appsDataStore, 'getAppDefinition').mockResolvedValue({
redirectDomainStatusCode: storedCode,
ports: [],
} as any)
const saveApp = jest.fn().mockResolvedValue(undefined)
;(appsDataStore as any).saveApp = saveApp
await appsDataStore.updateAppDefinitionInDb(
'test-app',
undefined,
'',
1,
'./captain-definition',
[],
[],
[],
'',
false,
80,
undefined,
false,
[],
undefined as any,
{} as any,
'',
'www.example.com',
'',
'',
false,
{ enabled: false },
sentCode
)
return saveApp.mock.calls[0][1].redirectDomainStatusCode
}
describe('redirect status code persistence', () => {
test('stores the status code that is sent', async () => {
expect(await saveWithStatusCode(undefined, 301)).toBe(301)
expect(await saveWithStatusCode(301, 302)).toBe(302)
})
test('keeps the stored status code when an older client omits it', async () => {
expect(await saveWithStatusCode(301, undefined)).toBe(301)
})
})