Skip to content

Commit

Permalink
test: update to use context
Browse files Browse the repository at this point in the history
  • Loading branch information
willfarrell committed Nov 23, 2023
1 parent e81111d commit 21ddf0e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
57 changes: 33 additions & 24 deletions packages/http-response-serializer/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import test from 'ava'
import middy from '../../core/index.js'
import { createError } from '../../util/index.js'

import httpContentNegotiation from '../../http-content-negotiation/index.js'
import httpErrorHandler from '../../http-error-handler/index.js'
import httpResponseSerializer from '../index.js'

Expand Down Expand Up @@ -49,7 +50,7 @@ for (const [key] of [['Content-Type'], ['content-type']]) {
const event = {
headers: {}
}
const response = await handler(event, context)
const response = await handler(event, { ...context })

t.deepEqual(response, handlerResponse)
})
Expand All @@ -73,6 +74,7 @@ for (const [accept, result] of [
]) {
test(`${accept} returns ${result}`, async (t) => {
const handler = middy()
.use(httpContentNegotiation())
.use(httpResponseSerializer(standardConfiguration))
.handler(createHttpResponse)

Expand All @@ -82,7 +84,7 @@ for (const [accept, result] of [
}
}

const response = await handler(event, context)
const response = await handler(event, { ...context })

t.is(response.body, result)
})
Expand All @@ -95,27 +97,30 @@ test('missing headers skips', async (t) => {

const event = {}

const response = await handler(event, context)
const response = await handler(event, { ...context })

t.is(response.body, '{"message":"Hello World"}')
})

// TODO deprecate in v6
test('It should use `event.requiredContentType` instead of accept headers', async (t) => {
const handler = middy((event, context) => {
event.requiredContentType = 'text/plain'

return createHttpResponse()
})

handler.use(httpResponseSerializer(standardConfiguration))
handler
.use(httpContentNegotiation())
.use(httpResponseSerializer(standardConfiguration))

const event = {
headers: {
Accept: 'application/xml, text/x-dvi; q=0.8, text/x-c'
}
}

const response = await handler(event, context)
const response = await handler(event, { ...context })

t.deepEqual(response, {
statusCode: 200,
Expand All @@ -134,7 +139,7 @@ test('It should use the defaultContentType when no accept preferences are given'
const event = {
headers: {}
}
const response = await handler(event, context)
const response = await handler(event, { ...context })

t.deepEqual(response, {
statusCode: 200,
Expand Down Expand Up @@ -166,7 +171,7 @@ test('It should allow the return of the entire response', async (t) => {
const event = {
headers: {}
}
const response = await handler(event, context)
const response = await handler(event, { ...context })

t.deepEqual(response, {
statusCode: 200,
Expand All @@ -179,20 +184,20 @@ test('It should allow the return of the entire response', async (t) => {

test('It should use the defaultContentType when no matching accept preferences are found', async (t) => {
const handler = middy((event, context) => {
event.preferredContentType = 'text/java'

return createHttpResponse()
})

handler.use(httpResponseSerializer(standardConfiguration))
handler
.use(httpContentNegotiation())
.use(httpResponseSerializer(standardConfiguration))

const event = {
headers: {
Accept: 'application/java, text/x-dvi; q=0.8, text/x-c'
}
}

const response = await handler(event, context)
const response = await handler(event, { ...context })

t.deepEqual(response, {
statusCode: 200,
Expand All @@ -203,19 +208,21 @@ test('It should use the defaultContentType when no matching accept preferences a
})
})

test('It should use `event.preferredContentType` instead of the defaultContentType', async (t) => {
test('It should use `context.preferredMediaTypes` instead of the defaultContentType', async (t) => {
const handler = middy((event, context) => {
event.preferredContentType = 'text/plain'

return createHttpResponse()
})

handler.use(httpResponseSerializer(standardConfiguration))
handler
.use(httpContentNegotiation())
.use(httpResponseSerializer(standardConfiguration))

const event = {
headers: {}
headers: {
Accept: 'text/plain'
}
}
const response = await handler(event, context)
const response = await handler(event, { ...context })

t.deepEqual(response, {
statusCode: 200,
Expand All @@ -238,7 +245,7 @@ test('It should pass-through when no preference or defaultContentType is found',
const event = {
headers: {}
}
const response = await handler(event, context)
const response = await handler(event, { ...context })

t.deepEqual(response, {
statusCode: 200,
Expand All @@ -258,7 +265,7 @@ test('It should not pass-through when request content-type is set', async (t) =>
}
}

const response = await handler(event, context)
const response = await handler(event, { ...context })

t.deepEqual(response, {
statusCode: 200,
Expand Down Expand Up @@ -291,7 +298,7 @@ test('It should replace the response object when the serializer returns an objec
const event = {
headers: {}
}
const response = await handler(event, context)
const response = await handler(event, { ...context })

t.deepEqual(response, {
statusCode: 204,
Expand All @@ -314,7 +321,7 @@ test('It should work with `http-error-handler` middleware', async (t) => {
const event = {
headers: {}
}
const response = await handler(event, context)
const response = await handler(event, { ...context })

t.deepEqual(response, {
statusCode: 422,
Expand All @@ -336,7 +343,7 @@ test('It should skip if the response is undefined form 502 error', async (t) =>
headers: {}
}
try {
await handler(event, context)
await handler(event, { ...context })
} catch (e) {
t.deepEqual(e.message, 'test')
}
Expand All @@ -352,8 +359,10 @@ test('It should return false when response body is falsey', async (t) => {
Accept: 'text/plain'
}
}
handler.use(httpResponseSerializer(standardConfiguration))
const response = await handler(event, context)
handler
.use(httpContentNegotiation())
.use(httpResponseSerializer(standardConfiguration))
const response = await handler(event, { ...context })

t.deepEqual(response, {
statusCode: 200,
Expand Down
2 changes: 1 addition & 1 deletion packages/http-response-serializer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const httpResponseSerializerMiddleware = (opts = {}) => {
types = [request.event.requiredContentType]
} else {
types = [
...request.context.preferredMediaTypes, // from @middy/http-content-negotiation
...(request.context.preferredMediaTypes ?? []), // from @middy/http-content-negotiation
defaultContentType
]
}
Expand Down

0 comments on commit 21ddf0e

Please sign in to comment.