Skip to content

Commit

Permalink
feat(fromOpenApi): support response headers (#14)
Browse files Browse the repository at this point in the history
* chore(tsconfig): remove path aliases

* feat(fromOpenApi): support response headers
  • Loading branch information
kettanaito authored Jan 20, 2022
1 parent fcd8019 commit 996915a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 10 deletions.
23 changes: 20 additions & 3 deletions src/fromOpenApi/fromOpenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,26 @@ function createResponseResolver(
const transformers: ResponseTransformer[] = []
transformers.push(ctx.status(Number(status)))

/**
* @todo Support "response.headers" schema.
*/
// Set response headers.
if (response.headers) {
for (const [headerName, headerDefinition] of Object.entries(
response.headers,
)) {
invariant(
!('$ref' in headerDefinition),
'Failed to generate mock response headers: found an unresolved reference',
headerDefinition,
)

const headerSchema = headerDefinition.schema as OpenAPIV3.SchemaObject
const headerValue = evolveJsonSchema(headerSchema)
if (!headerValue) {
continue
}

transformers.push(ctx.set(headerName, toString(headerValue)))
}
}

if ('content' in response && response.content != null) {
let body: unknown
Expand Down
59 changes: 59 additions & 0 deletions test/oas/oas-response-headers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import fetch from 'cross-fetch'
import { Headers } from 'headers-utils'
import { fromOpenApi } from '../../src/fromOpenApi/fromOpenApi'
import { createOpenApiSpec } from '../../test/support/createOpenApiSpec'
import { withHandlers } from '../support/withHandlers'

it('supports response headers', async () => {
const handlers = await fromOpenApi(
createOpenApiSpec({
paths: {
'/user': {
get: {
responses: {
200: {
headers: {
'X-Rate-Limit-Remaining': {
schema: {
type: 'number',
},
},
'X-Rate-Limit-Reset': {
schema: {
type: 'string',
format: 'date-time',
},
},
},
content: {
'text/plain': {
schema: {
type: 'string',
},
},
},
},
},
},
},
},
}),
)

const res = await withHandlers(handlers, () => {
return fetch('http://localhost/user')
})

expect(res.status).toEqual(200)
const headers = new Headers(res.headers)

expect(headers.all()).toEqual({
'content-type': 'text/plain',
'x-powered-by': 'msw',
// Header values are always strings.
'x-rate-limit-remaining': expect.any(String),
'x-rate-limit-reset': expect.stringMatching(
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+?Z$/,
),
})
})
2 changes: 1 addition & 1 deletion test/traffic/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs'
import { Har, Header } from 'har-format'
import { HeadersObject, headersToObject } from 'headers-utils'
import { MapEntryFn } from 'src/fromTraffic/fromTraffic'
import { MapEntryFn } from '../../../src/fromTraffic/fromTraffic'

export function readArchive(archivePath: string): Har {
return JSON.parse(fs.readFileSync(archivePath, 'utf8'))
Expand Down
7 changes: 1 addition & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
"noImplicitAny": true,
"outDir": "lib",
"declaration": true,
"declarationDir": "lib",
"baseUrl": "./",
"paths": {
"src/*": ["src/*"],
"test/*": ["test/*"]
}
"declarationDir": "lib"
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
Expand Down

0 comments on commit 996915a

Please sign in to comment.