2026-09-17 19:14:41 +08:00
|
|
|
|
// services/api/test/health.e2e-spec.ts
|
|
|
|
|
|
// e2e 冒烟:验证 e2e 工装(Nest testing module + supertest + 根 .env 加载)可用。
|
|
|
|
|
|
// 不监听端口:app.init() 后直接对 app.getHttpServer() 发请求。
|
2026-09-17 20:54:30 +08:00
|
|
|
|
// 全局前缀与 main-cn.ts 共用 src/shared/constants 的 API_PREFIX(Task 7 遗留项⑥)。
|
2026-09-17 19:14:41 +08:00
|
|
|
|
import { type INestApplication } from '@nestjs/common';
|
|
|
|
|
|
import { Test } from '@nestjs/testing';
|
|
|
|
|
|
import request from 'supertest';
|
|
|
|
|
|
import { AppCnModule } from '../src/app-cn.module';
|
2026-09-17 20:54:30 +08:00
|
|
|
|
import { API_PREFIX } from '../src/shared/constants';
|
2026-09-17 19:14:41 +08:00
|
|
|
|
import { resetDb } from './reset';
|
|
|
|
|
|
|
2026-09-17 20:54:30 +08:00
|
|
|
|
describe(`GET /${API_PREFIX}/health (e2e)`, () => {
|
2026-09-17 19:14:41 +08:00
|
|
|
|
let app: INestApplication;
|
|
|
|
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
|
await resetDb();
|
|
|
|
|
|
const moduleRef = await Test.createTestingModule({
|
|
|
|
|
|
imports: [AppCnModule],
|
|
|
|
|
|
}).compile();
|
|
|
|
|
|
app = moduleRef.createNestApplication();
|
2026-09-17 20:54:30 +08:00
|
|
|
|
app.setGlobalPrefix(API_PREFIX);
|
2026-09-17 19:14:41 +08:00
|
|
|
|
await app.init();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
|
|
await app.close();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('responds 200 {"status":"ok"}', async () => {
|
2026-09-17 20:54:30 +08:00
|
|
|
|
const res = await request(app.getHttpServer()).get(`/${API_PREFIX}/health`);
|
2026-09-17 19:14:41 +08:00
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
|
|
expect(res.body).toEqual({ status: 'ok' });
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('sets x-request-id response header (generated when absent)', async () => {
|
2026-09-17 20:54:30 +08:00
|
|
|
|
const res = await request(app.getHttpServer()).get(`/${API_PREFIX}/health`);
|
2026-09-17 19:14:41 +08:00
|
|
|
|
const requestId = res.headers['x-request-id'];
|
|
|
|
|
|
expect(typeof requestId).toBe('string');
|
|
|
|
|
|
expect(requestId.length ?? 0).toBeGreaterThan(0);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('echoes incoming x-request-id header', async () => {
|
|
|
|
|
|
const res = await request(app.getHttpServer())
|
2026-09-17 20:54:30 +08:00
|
|
|
|
.get(`/${API_PREFIX}/health`)
|
2026-09-17 19:14:41 +08:00
|
|
|
|
.set('x-request-id', 'e2e-fixed-request-id');
|
|
|
|
|
|
expect(res.headers['x-request-id']).toBe('e2e-fixed-request-id');
|
|
|
|
|
|
});
|
2026-09-17 20:54:30 +08:00
|
|
|
|
|
|
|
|
|
|
it('replaces an over-long incoming x-request-id (>128 chars) with a fresh one', async () => {
|
|
|
|
|
|
const overlong = 'z'.repeat(200);
|
|
|
|
|
|
const res = await request(app.getHttpServer())
|
|
|
|
|
|
.get(`/${API_PREFIX}/health`)
|
|
|
|
|
|
.set('x-request-id', overlong);
|
|
|
|
|
|
const requestId = res.headers['x-request-id'];
|
|
|
|
|
|
expect(typeof requestId).toBe('string');
|
|
|
|
|
|
expect(requestId).not.toBe(overlong);
|
|
|
|
|
|
expect((requestId as string).length).toBeLessThanOrEqual(128);
|
|
|
|
|
|
});
|
2026-09-17 19:14:41 +08:00
|
|
|
|
});
|