Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions api/mock-server/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { newMockApiApp } from './app.ts'

const json = async <T>(response: Response) => response.json() as Promise<T>

const expectProblemResponse = (response: Response, status: number) => {
expect(response.status).toBe(status)
expect(response.headers.get('content-type')).toContain('application/problem+json')
}

describe('mock api app', () => {
it('creates counter ids and exposes them through the workspace list', async () => {
const app = await newMockApiApp()
Expand Down Expand Up @@ -66,7 +71,7 @@ describe('mock api app', () => {
}),
)

expect(response.status).toBe(422)
expectProblemResponse(response, 422)
await expect(json(response)).resolves.toMatchObject({
issues: [
{
Expand All @@ -79,7 +84,49 @@ describe('mock api app', () => {
},
],
retryable: false,
type: 'validation',
status: 422,
title: 'Validation Failed',
type: '/problems/validation',
})
})

it('returns problem details for malformed request bodies', async () => {
const app = await newMockApiApp()

const response = await app.fetch(
new Request('http://localhost/api/v1/workspaces', {
body: '{',
headers: {
'content-type': 'application/json',
},
method: 'POST',
}),
)

expectProblemResponse(response, 400)
await expect(json(response)).resolves.toMatchObject({
detail: 'Request body must be valid JSON.',
retryable: false,
status: 400,
title: 'Bad Request',
type: '/problems/bad-request',
})
})

it('returns problem details for unknown routes', async () => {
const app = await newMockApiApp()

const response = await app.fetch(new Request('http://localhost/api/v1/not-real'))

expectProblemResponse(response, 404)
await expect(json(response)).resolves.toMatchObject({
detail: 'route request was not found',
entity: 'route',
entityId: 'request',
retryable: false,
status: 404,
title: 'Not Found',
type: '/problems/not-found',
})
})

Expand Down Expand Up @@ -165,7 +212,7 @@ describe('mock api app', () => {
}),
)

expect(response.status).toBe(422)
expectProblemResponse(response, 422)
await expect(json(response)).resolves.toMatchObject({
issues: [
{
Expand All @@ -174,7 +221,9 @@ describe('mock api app', () => {
},
],
retryable: false,
type: 'validation',
status: 422,
title: 'Validation Failed',
type: '/problems/validation',
})
})

Expand Down
4 changes: 2 additions & 2 deletions api/mock-server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { generatedMockRuntime as adminRuntime } from './generated/mock-admin/moc
import { generatedMockRuntime as clearWebApiRuntime } from './generated/clear-web-api/mock-runtime.ts'
import { notFound } from './lib/errors.ts'
import {
mockJsonResponse,
mockProblemResponse,
registerGeneratedMockRoutes,
} from './lib/honoMockRuntime.ts'

Expand Down Expand Up @@ -42,7 +42,7 @@ export const newMockApiApp = async ({
app.notFound(() => {
const routeError = notFound('route', 'request')

return mockJsonResponse(routeError.body, routeError.status)
return mockProblemResponse(routeError.body, routeError.status)
})

return app
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading