Skip to content

Commit

Permalink
feat: Add httpEvent.operationId to the request context (#1325)
Browse files Browse the repository at this point in the history
  • Loading branch information
qswinson committed Feb 2, 2022
1 parent 5df70c8 commit e217fcb
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/events/http/HttpServer.js
Expand Up @@ -544,6 +544,11 @@ export default class HttpServer {
}
}

const additionalRequestContext = {}
if (httpEvent.operationId) {
additionalRequestContext.operationName = httpEvent.operationId
}

hapiOptions.tags = ['api']

const hapiHandler = async (request, h) => {
Expand Down Expand Up @@ -740,6 +745,7 @@ export default class HttpServer {
stage,
endpoint.routeKey,
stageVariables,
additionalRequestContext,
this.v3Utils,
)
: new LambdaProxyIntegrationEvent(
Expand All @@ -748,6 +754,7 @@ export default class HttpServer {
requestPath,
stageVariables,
endpoint.isHttpApi ? endpoint.routeKey : null,
additionalRequestContext,
this.v3Utils,
)

Expand Down
15 changes: 13 additions & 2 deletions src/events/http/lambda-events/LambdaProxyIntegrationEvent.js
Expand Up @@ -23,13 +23,23 @@ export default class LambdaProxyIntegrationEvent {
#request = null
#stage = null
#stageVariables = null

constructor(request, stage, path, stageVariables, routeKey = null, v3Utils) {
#additionalRequestContext = null

constructor(
request,
stage,
path,
stageVariables,
routeKey = null,
additionalRequestContext = null,
v3Utils,
) {
this.#path = path
this.#routeKey = routeKey
this.#request = request
this.#stage = stage
this.#stageVariables = stageVariables
this.#additionalRequestContext = additionalRequestContext || {}
if (v3Utils) {
this.log = v3Utils.log
this.progress = v3Utils.progress
Expand Down Expand Up @@ -202,6 +212,7 @@ export default class LambdaProxyIntegrationEvent {
userAgent: _headers['user-agent'] || '',
userArn: 'offlineContext_userArn',
},
operationName: this.#additionalRequestContext.operationName,
path: this.#path,
protocol: 'HTTP/1.1',
requestId: createUniqueId(),
Expand Down
14 changes: 12 additions & 2 deletions src/events/http/lambda-events/LambdaProxyIntegrationEventV2.js
Expand Up @@ -17,12 +17,21 @@ export default class LambdaProxyIntegrationEventV2 {
#request = null
#stage = null
#stageVariables = null

constructor(request, stage, routeKey, stageVariables, v3Utils) {
#additionalRequestContext = null

constructor(
request,
stage,
routeKey,
stageVariables,
additionalRequestContext,
v3Utils,
) {
this.#routeKey = routeKey
this.#request = request
this.#stage = stage
this.#stageVariables = stageVariables
this.#additionalRequestContext = additionalRequestContext || {}
if (v3Utils) {
this.log = v3Utils.log
this.progress = v3Utils.progress
Expand Down Expand Up @@ -164,6 +173,7 @@ export default class LambdaProxyIntegrationEventV2 {
sourceIp: remoteAddress,
userAgent: _headers['user-agent'] || '',
},
operationName: this.#additionalRequestContext.operationName,
requestId: 'offlineContext_resourceId',
routeKey: this.#routeKey,
stage: this.#stage,
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/lambda-integration/handler.js
Expand Up @@ -14,3 +14,13 @@ exports.lambdaIntegrationStringified =
foo: 'bar',
})
}

exports.lambdaIntegrationWithOperationName =
async function lambdaIntegrationWithOperationName(event) {
return {
body: stringify({
operationName: event.requestContext.operationName,
}),
statusCode: 200,
}
}
Expand Up @@ -36,6 +36,14 @@ describe('lambda integration tests', () => {
path: '/dev/lambda-integration-stringified',
status: 200,
},
{
description: 'should return operation name from request context',
expected: {
operationName: 'getIntegrationWithOperationName',
},
path: '/dev/lambda-integration-with-operation-name',
status: 200,
},
].forEach(({ description, expected, path, status }) => {
test(description, async () => {
const url = joinUrl(TEST_BASE_URL, path)
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/lambda-integration/serverless.yml
Expand Up @@ -31,3 +31,11 @@ functions:
method: get
path: '/lambda-integration-stringified'
handler: handler.lambdaIntegrationStringified

integrationWithOperationName:
events:
- http:
method: get
operationId: getIntegrationWithOperationName
path: '/lambda-integration-with-operation-name'
handler: handler.lambdaIntegrationWithOperationName
30 changes: 30 additions & 0 deletions tests/old-unit/LambdaProxyIntegrationEvent.test.js
Expand Up @@ -75,6 +75,12 @@ describe('LambdaProxyIntegrationEvent', () => {
test('should match fixed attributes', () => {
expectFixedAttributes(lambdaProxyIntegrationEvent)
})

test('should not have operation name', () => {
expect(lambdaProxyIntegrationEvent.requestContext.operationName).toEqual(
undefined,
)
})
})

describe('with a GET /fn1 request with headers', () => {
Expand Down Expand Up @@ -698,4 +704,28 @@ describe('LambdaProxyIntegrationEvent', () => {
)
})
})

describe('with operation name', () => {
const requestBuilder = new RequestBuilder('GET', '/fn1')
const request = requestBuilder.toObject()

let lambdaProxyIntegrationEvent

beforeEach(() => {
lambdaProxyIntegrationEvent = new LambdaProxyIntegrationEvent(
request,
stage,
null,
null,
null,
{ operationName: 'getFunctionOne' },
).create()
})

test('should have operation name', () => {
expect(lambdaProxyIntegrationEvent.requestContext.operationName).toEqual(
'getFunctionOne',
)
})
})
})

0 comments on commit e217fcb

Please sign in to comment.