mirror of
https://github.com/caprover/caprover.git
synced 2026-12-04 00:28:15 +00:00
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
93 lines
3.2 KiB
TypeScript
93 lines
3.2 KiB
TypeScript
import type DataStore from '../src/datastore/DataStore'
|
|
import type DockerApi from '../src/docker/DockerApi'
|
|
import type CertbotManager from '../src/user/system/CertbotManager'
|
|
import LoadBalancerManager from '../src/user/system/LoadBalancerManager'
|
|
|
|
async function getServers(webApp: Record<string, unknown>, hasRootSsl = true) {
|
|
const dataStore = {
|
|
getAppsDataStore: () => ({
|
|
getAppDefinitions: async () => ({ web: webApp }),
|
|
getServiceName: () => 'srv-captain--web',
|
|
}),
|
|
getGoAccessInfo: async () => ({ isEnabled: false }),
|
|
} as unknown as DataStore
|
|
return new LoadBalancerManager(
|
|
{} as DockerApi,
|
|
{} as CertbotManager,
|
|
dataStore
|
|
).getAppsServerConfig(dataStore, '', hasRootSsl, 'root.example.com')
|
|
}
|
|
|
|
async function getRedirects(
|
|
webApp: Record<string, unknown>,
|
|
hasRootSsl = true
|
|
) {
|
|
const redirects: Record<string, string | undefined> = {}
|
|
;(await getServers(webApp, hasRootSsl)).forEach(
|
|
(s) => (redirects[s.publicDomain] = s.redirectToPath)
|
|
)
|
|
return redirects
|
|
}
|
|
|
|
async function getStatusCodes(webApp: Record<string, unknown>) {
|
|
const codes: Record<string, number | undefined> = {}
|
|
;(await getServers(webApp)).forEach(
|
|
(s) => (codes[s.publicDomain] = s.redirectStatusCode)
|
|
)
|
|
return codes
|
|
}
|
|
|
|
const app = (redirectDomain: string, targetHasSsl: boolean) => ({
|
|
hasDefaultSubDomainSsl: true,
|
|
redirectDomain,
|
|
customDomain: [
|
|
{ publicDomain: 'www.example.com', hasSsl: targetHasSsl },
|
|
{ publicDomain: 'example.com', hasSsl: true },
|
|
],
|
|
})
|
|
|
|
describe('redirect all domains', () => {
|
|
test('uses https when the target domain has ssl', async () => {
|
|
expect(await getRedirects(app('www.example.com', true))).toEqual({
|
|
'web.root.example.com': 'https://www.example.com',
|
|
'www.example.com': undefined,
|
|
'example.com': 'https://www.example.com',
|
|
})
|
|
})
|
|
|
|
test('keeps http when the target domain has no ssl', async () => {
|
|
const redirects = await getRedirects(app('www.example.com', false))
|
|
expect(redirects['example.com']).toBe('http://www.example.com')
|
|
})
|
|
|
|
test('uses the default subdomain ssl when it is the target', async () => {
|
|
const target = app('web.root.example.com', false)
|
|
expect((await getRedirects(target, true))['example.com']).toBe(
|
|
'https://web.root.example.com'
|
|
)
|
|
expect((await getRedirects(target, false))['example.com']).toBe(
|
|
'http://web.root.example.com'
|
|
)
|
|
})
|
|
|
|
test('defaults to 302 when no status code is set', async () => {
|
|
expect(await getStatusCodes(app('www.example.com', true))).toEqual({
|
|
'web.root.example.com': 302,
|
|
'www.example.com': undefined,
|
|
'example.com': 302,
|
|
})
|
|
})
|
|
|
|
test('uses 301 when it is set on the app', async () => {
|
|
const target = {
|
|
...app('www.example.com', true),
|
|
redirectDomainStatusCode: 301,
|
|
}
|
|
expect(await getStatusCodes(target)).toEqual({
|
|
'web.root.example.com': 301,
|
|
'www.example.com': undefined,
|
|
'example.com': 301,
|
|
})
|
|
})
|
|
})
|