Testing HTTP Endpoints
Endpoint Tests
For every important endpoint, test authentication, authorization, valid input, invalid input, missing resources, conflict conditions, pagination boundaries, malformed parameters, and dependency failures.
A test should assert more than status code. Validate response headers, response schema, important fields, side effects, and database state where appropriate.
For destructive operations, verify that unauthorized callers cannot cause the side effect.
API Test Example
const response = await request(app)
.get('/users/42')
.set('Authorization', `Bearer ${token}`)
.expect('Content-Type', /json/)
.expect(200);
expect(response.body.id).toBe('42');💡 The test verifies both transport behavior and the response representation.