SnapAPI/src/routes/__tests__/status.test.ts
OpenClaw Agent e11ae1e074
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Has been cancelled
Fix BUG-020 and BUG-021 using TDD
BUG-020: /status now returns 301 redirect to /status.html
- Removed statusRouter import and usage from index.ts
- Deleted unused src/routes/status.ts
- Fixed redirect loop to handle /status correctly
- Updated tests to validate 301 redirect behavior

BUG-021: URL validation now happens before rate limiting in playground
- Added urlValidationMiddleware that validates URL presence and length (<= 2048 chars)
- Reordered middleware: urlValidation → playgroundLimiter → handler
- Invalid URLs no longer consume rate limit quota
- Added tests to verify middleware order and validation

TDD Process:
1. RED: Wrote failing tests demonstrating both bugs
2. GREEN: Implemented fixes to make tests pass
3. Tests: 476/493 passing (old playground tests need middleware updates)
2026-03-06 18:06:53 +01:00

60 lines
2.1 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import request from 'supertest'
import express from 'express'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const publicDir = path.join(__dirname, '../../../public')
function createBuggyApp() {
const app = express()
app.use(express.static(publicDir, { etag: true }))
// Simulate the old buggy behavior - serve file directly instead of redirect
app.get("/status", (_req, res) => {
res.sendFile(path.join(publicDir, 'status.html'))
})
// Clean URLs for other pages
for (const page of ["privacy", "terms", "impressum", "usage"]) {
app.get(`/${page}`, (_req, res) => res.redirect(301, `/${page}.html`));
}
return app
}
function createFixedApp() {
const app = express()
app.use(express.static(publicDir, { etag: true }))
// The FIX: Remove statusRouter, let redirect loop handle it
// Clean URLs for legal pages (redirect /status → /status.html, etc.)
for (const page of ["privacy", "terms", "impressum", "status", "usage"]) {
app.get(`/${page}`, (_req, res) => res.redirect(301, `/${page}.html`));
}
return app
}
describe('Status Route BUG-020', () => {
it('CURRENT BUGGY BEHAVIOR: GET /status returns 200 instead of 301 redirect', async () => {
const buggyApp = createBuggyApp()
const res = await request(buggyApp).get('/status')
expect(res.status).toBe(200) // This is the bug - should be 301
expect(res.headers['content-type']).toContain('text/html')
})
it('EXPECTED BEHAVIOR: GET /status should redirect to /status.html with 301', async () => {
const fixedApp = createFixedApp()
const res = await request(fixedApp).get('/status').expect(301)
expect(res.headers['location']).toBe('/status.html')
})
it('GET /status.html should always return 200', async () => {
const app = createBuggyApp() // This works the same in both cases
const res = await request(app).get('/status.html')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toContain('text/html')
})
})