Files
Online-Topup-V1/application/tests/server-app.test.js
T
2026-11-26 17:18:30 +07:00

58 lines
2.3 KiB
JavaScript

const request = require('supertest');
jest.mock('../../server/routes', () => ({
mountApiRoutes(app) {
app.post('/api/fixture', (req, res) => res.json(req.body));
},
}));
jest.mock('../middlewares/response-telemetry.middleware', () => ({
transactionRequest(req, res, next) { next(); },
response(req, res, next) { next(); },
}));
const app = require('../../server');
test('entry point menyajikan halaman utama, file publik, metrik, dan health', async () => {
await request(app).get('/').expect(200, { message: 'Welcome to Topup Online Service.', data: {} });
await request(app).get('/health').expect(200, { status: 'ok' });
await request(app).get('/images/logo_portal_kmt.png').expect(200).expect('Content-Type', /image\/png/);
await request(app).get('/metrics').expect(200).expect('Content-Type', /text\/plain/);
});
test('health mengembalikan 503 dan header Connection close saat proses shutting down', async () => {
try {
app.setShuttingDown(true);
expect(app.isShuttingDown()).toBe(true);
const response = await request(app).get('/health').expect(503, { status: 'shutting_down' });
expect(response.headers.connection).toBe('close');
} finally {
app.setShuttingDown(false);
expect(app.isShuttingDown()).toBe(false);
}
});
test('entry point mempertahankan parse JSON, form, dan header CORS', async () => {
await request(app).post('/api/fixture')
.set('Origin', 'http://localhost:3000')
.send({ code: '00' })
.expect('Access-Control-Allow-Origin', 'http://localhost:3000')
.expect('Access-Control-Expose-Headers', 'X-Request-Id,X-Response-Time')
.expect(200, { code: '00' });
await request(app).post('/api/fixture')
.type('form').send({ code: '00' })
.expect(200, { code: '00' });
});
test('request ID dibuat oleh server, termasuk ketika body JSON tidak valid', async () => {
const malformed = await request(app).post('/api/fixture')
.set('X-Request-Id', 'client-value')
.type('json').send('{')
.expect(400);
const success = await request(app).post('/api/fixture').send({ code: '00' }).expect(200);
expect(malformed.headers['x-request-id']).toMatch(/^[a-f\d-]{36}$/);
expect(success.headers['x-request-id']).toMatch(/^[a-f\d-]{36}$/);
expect(malformed.headers['x-request-id']).not.toBe(success.headers['x-request-id']);
});