feat: add OpenAPI documentation and comprehensive test suite

- Add OpenAPI schema endpoint at /openapi.json with proper logging
- Make server exportable for testing by wrapping app.listen in module check
- Add Jest test suite covering all API endpoints
- Improve request logging with IP addresses and timestamps
- Add development dependencies for testing (jest, supertest)
This commit is contained in:
2025-09-02 12:26:30 +02:00
parent 69c216a0d0
commit 271eb107a2
5 changed files with 5598 additions and 5 deletions

View File

@@ -9,6 +9,9 @@ const PORT = process.env.PORT || 3000;
// Load reasons from JSON // Load reasons from JSON
const reasons = JSON.parse(fs.readFileSync('./reasons.json', 'utf-8')); const reasons = JSON.parse(fs.readFileSync('./reasons.json', 'utf-8'));
// Load OpenAPI schema
const openapiSchema = JSON.parse(fs.readFileSync('./openapi.json', 'utf-8'));
// Rate limiter: 120 requests per minute per IP // Rate limiter: 120 requests per minute per IP
const limiter = rateLimit({ const limiter = rateLimit({
windowMs: 60 * 1000, // 1 minute windowMs: 60 * 1000, // 1 minute
@@ -21,17 +24,35 @@ const limiter = rateLimit({
app.use(limiter); app.use(limiter);
// Serve OpenAPI schema
app.get('/openapi.json', (req, res) => {
const ip = req.headers['x-forwarded-for'] || req.ip;
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] OpenAPI schema request from IP: ${ip}`);
res.json(openapiSchema);
});
// Random rejection reason endpoint // Random rejection reason endpoint
app.get('/', (req, res) => { app.get('/', (req, res) => {
const ip = req.headers['x-forwarded-for'] || req.ip;
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] Request from IP: ${ip}`);
const reason = reasons[Math.floor(Math.random() * reasons.length)]; const reason = reasons[Math.floor(Math.random() * reasons.length)];
res.json({ reason }); res.json({ reason });
}); });
app.get('/health', (req, res) => { app.get('/health', (req, res) => {
const ip = req.headers['x-forwarded-for'] || req.ip;
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] Request from IP: ${ip}`);
res.json({"status": "ok"}); res.json({"status": "ok"});
}); });
// Start server // Start server only if this file is run directly
app.listen(PORT, () => { if (require.main === module) {
console.log(`No-as-a-Service is running on port ${PORT}`); app.listen(PORT, () => {
}); console.log(`No-as-a-Service is running on port ${PORT}`);
});
}
module.exports = app;

5516
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -4,12 +4,17 @@
"description": "A lightweight API that returns random rejection or no reasons.", "description": "A lightweight API that returns random rejection or no reasons.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "node index.js" "start": "node index.js",
"test": "jest"
}, },
"author": "hotheadhacker", "author": "hotheadhacker",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"express": "^4.18.2", "express": "^4.18.2",
"express-rate-limit": "^7.0.0" "express-rate-limit": "^7.0.0"
},
"devDependencies": {
"jest": "^30.1.2",
"supertest": "^7.1.4"
} }
} }

28
test/api.test.js Normal file
View File

@@ -0,0 +1,28 @@
const request = require('supertest');
describe('API Endpoints', () => {
let app;
beforeAll(() => {
app = require('../index.js');
});
test('should return a random reason at root endpoint', async () => {
const response = await request(app)
.get('/')
.expect(200)
.expect('Content-Type', /application\/json/);
expect(response.body).toHaveProperty('reason');
expect(typeof response.body.reason).toBe('string');
});
test('should return health status at /health endpoint', async () => {
const response = await request(app)
.get('/health')
.expect(200)
.expect('Content-Type', /application\/json/);
expect(response.body).toEqual({ status: 'ok' });
});
});

23
test/openapi.test.js Normal file
View File

@@ -0,0 +1,23 @@
const request = require('supertest');
const fs = require('fs');
describe('OpenAPI Schema Endpoint', () => {
let app;
beforeAll(() => {
// Import the app after modifications
app = require('../index.js');
});
test('should serve OpenAPI schema at /openapi.json', async () => {
// Read the expected schema from the file
const expectedSchema = JSON.parse(fs.readFileSync('./openapi.json', 'utf-8'));
const response = await request(app)
.get('/openapi.json')
.expect(200)
.expect('Content-Type', /application\/json/);
expect(response.body).toEqual(expectedSchema);
});
});