Merge remote-tracking branch 'origin/upload-ok'
This commit is contained in:
+13
-6
@@ -37,15 +37,22 @@ export function abortOnClose (res: Response): AbortSignal {
|
||||
return controller.signal
|
||||
}
|
||||
|
||||
/** `GET /healthcheck`: `ok` while Immich answers, else 503. */
|
||||
export async function healthcheck (_req: Request, res: Response): Promise<void> {
|
||||
if (await accessible()) {
|
||||
res.send('ok')
|
||||
} else {
|
||||
res.status(503).send()
|
||||
/**
|
||||
* A `GET /healthcheck` handler: `body` while Immich answers, else 503.
|
||||
*/
|
||||
export function healthcheckHandler (body: string) {
|
||||
return async (_req: Request, res: Response): Promise<void> => {
|
||||
if (await accessible()) {
|
||||
res.send(body)
|
||||
} else {
|
||||
res.status(503).send()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** IPP's `GET /healthcheck`: `ok` while Immich answers, else 503. */
|
||||
export const healthcheck = healthcheckHandler('ok')
|
||||
|
||||
/**
|
||||
* Wrap an async route handler so a rejected promise is passed to Express's
|
||||
* error chain (and on to `errorHandler`).
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import { healthcheck, healthcheckHandler } from '../src/http'
|
||||
import * as client from '../src/immich/client'
|
||||
|
||||
/*
|
||||
Each service answers its healthcheck with its own word: IPP says `ok` and
|
||||
the upload service says `upload-ok`. An upload hostname that a reverse proxy
|
||||
points at IPP's port by mistake then reads `ok`, not `upload-ok`, instead of
|
||||
passing for a working upload service (GitHub #300).
|
||||
*/
|
||||
|
||||
function response () {
|
||||
const res = { body: undefined as string | undefined, code: 200 }
|
||||
return Object.assign(res, {
|
||||
send (body?: string) { res.body = body; return res },
|
||||
status (code: number) { res.code = code; return res }
|
||||
})
|
||||
}
|
||||
|
||||
afterEach(() => vi.restoreAllMocks())
|
||||
|
||||
describe('healthcheckHandler', () => {
|
||||
it('answers with the given word while Immich is reachable', async () => {
|
||||
vi.spyOn(client, 'accessible').mockResolvedValue(true)
|
||||
const res = response()
|
||||
await healthcheckHandler('upload-ok')({} as never, res as never)
|
||||
expect(res.code).toBe(200)
|
||||
expect(res.body).toBe('upload-ok')
|
||||
})
|
||||
|
||||
it('answers 503 with no body when Immich is not reachable', async () => {
|
||||
vi.spyOn(client, 'accessible').mockResolvedValue(false)
|
||||
const res = response()
|
||||
await healthcheckHandler('upload-ok')({} as never, res as never)
|
||||
expect(res.code).toBe(503)
|
||||
expect(res.body).toBeUndefined()
|
||||
})
|
||||
|
||||
it("is IPP's own healthcheck with `ok`", async () => {
|
||||
vi.spyOn(client, 'accessible').mockResolvedValue(true)
|
||||
const res = response()
|
||||
await healthcheck({} as never, res as never)
|
||||
expect(res.body).toBe('ok')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user