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:
23
index.js
23
index.js
@@ -9,6 +9,9 @@ const PORT = process.env.PORT || 3000;
|
||||
// Load reasons from JSON
|
||||
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
|
||||
const limiter = rateLimit({
|
||||
windowMs: 60 * 1000, // 1 minute
|
||||
@@ -21,17 +24,35 @@ const limiter = rateLimit({
|
||||
|
||||
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
|
||||
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)];
|
||||
res.json({ reason });
|
||||
});
|
||||
|
||||
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"});
|
||||
});
|
||||
|
||||
// Start server
|
||||
// Start server only if this file is run directly
|
||||
if (require.main === module) {
|
||||
app.listen(PORT, () => {
|
||||
console.log(`No-as-a-Service is running on port ${PORT}`);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = app;
|
||||
|
||||
5516
package-lock.json
generated
Normal file
5516
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -4,12 +4,17 @@
|
||||
"description": "A lightweight API that returns random rejection or no reasons.",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node index.js"
|
||||
"start": "node index.js",
|
||||
"test": "jest"
|
||||
},
|
||||
"author": "hotheadhacker",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"express-rate-limit": "^7.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jest": "^30.1.2",
|
||||
"supertest": "^7.1.4"
|
||||
}
|
||||
}
|
||||
|
||||
28
test/api.test.js
Normal file
28
test/api.test.js
Normal 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
23
test/openapi.test.js
Normal 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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user