feat: 初始化项目
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import 'reflect-metadata';
|
||||
import assert from 'node:assert/strict';
|
||||
import { test } from 'node:test';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { AppModule } from '../src/app.module';
|
||||
import { configureApplication } from '../src/app.setup';
|
||||
|
||||
test('服务可以启动并通过带版本前缀的 HTTP 健康检查', async (context) => {
|
||||
const testingModule = await Test.createTestingModule({
|
||||
imports: [AppModule],
|
||||
}).compile();
|
||||
const app = testingModule.createNestApplication({ logger: false });
|
||||
context.after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
configureApplication(app);
|
||||
await app.listen(0, '127.0.0.1');
|
||||
const origin = await app.getUrl();
|
||||
|
||||
const response = await fetch(`${origin}/api/v1/health`, {
|
||||
signal: AbortSignal.timeout(5000),
|
||||
});
|
||||
assert.equal(response.status, 200);
|
||||
assert.deepEqual(await response.json(), {
|
||||
code: 0,
|
||||
message: 'ok',
|
||||
data: {
|
||||
status: 'ok',
|
||||
service: 'doodle-store-server',
|
||||
},
|
||||
});
|
||||
|
||||
const unversioned = await fetch(`${origin}/health`, {
|
||||
signal: AbortSignal.timeout(5000),
|
||||
});
|
||||
assert.equal(unversioned.status, 404);
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { test } from 'node:test';
|
||||
import { getRuntimeOptions } from '../src/common/runtime-options';
|
||||
|
||||
test('使用本地默认 host 和 port', () => {
|
||||
assert.deepEqual(getRuntimeOptions({}), {
|
||||
host: '127.0.0.1',
|
||||
port: 3000,
|
||||
});
|
||||
});
|
||||
|
||||
test('读取显式 host 和 port', () => {
|
||||
assert.deepEqual(getRuntimeOptions({ HOST: '0.0.0.0', PORT: '4000' }), {
|
||||
host: '0.0.0.0',
|
||||
port: 4000,
|
||||
});
|
||||
});
|
||||
|
||||
test('拒绝非法端口', () => {
|
||||
for (const port of ['', '0', '65536', '-1', '3000abc', '3.5', '1e3']) {
|
||||
assert.throws(() => getRuntimeOptions({ PORT: port }), /PORT/);
|
||||
}
|
||||
});
|
||||
|
||||
test('拒绝空 host', () => {
|
||||
assert.throws(() => getRuntimeOptions({ HOST: ' ' }), /HOST/);
|
||||
});
|
||||
Reference in New Issue
Block a user