Skip to content

Commit

Permalink
[Bref] Add invocation and request context to the Request ServerBag (#78)
Browse files Browse the repository at this point in the history
* Add invocation and request context to the Request ServerBag

* Added tests

Co-authored-by: Nyholm <tobias.nyholm@gmail.com>
  • Loading branch information
dmerchier and Nyholm authored Sep 10, 2021
1 parent 8108bfc commit d43faf4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
}
],
"require": {
"ext-json": "*",
"bref/bref": "^1.2",
"clue/arguments": "^2.1",
"psr/http-server-handler": "^1.0",
Expand Down
2 changes: 2 additions & 0 deletions src/SymfonyRequestBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public static function convertRequest(HttpRequestEvent $event, Context $context)
'REQUEST_TIME_FLOAT' => microtime(true),
'REQUEST_URI' => $event->getUri(),
'REMOTE_ADDR' => '127.0.0.1',
'LAMBDA_INVOCATION_CONTEXT' => json_encode($context),
'LAMBDA_REQUEST_CONTEXT' => json_encode($event->getRequestContext()),
], fn ($value) => null !== $value);

foreach ($event->getHeaders() as $name => $values) {
Expand Down
13 changes: 13 additions & 0 deletions tests/SymfonyRequestBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ public function testEmptyUploadedFile()
$this->assertSame('bar', $post['foo']);
}

public function testLambdaContext()
{
$requestContext = ['http' => ['method' => 'GET']];
$request = SymfonyRequestBridge::convertRequest(new HttpRequestEvent([
'requestContext' => $requestContext,
]), $invocationContext = $this->getContext());
$this->assertTrue($request->server->has('LAMBDA_INVOCATION_CONTEXT'));
$this->assertTrue($request->server->has('LAMBDA_REQUEST_CONTEXT'));

$this->assertSame(json_encode($invocationContext), $request->server->get('LAMBDA_INVOCATION_CONTEXT'));
$this->assertSame(json_encode($requestContext), $request->server->get('LAMBDA_REQUEST_CONTEXT'));
}

private function getContext()
{
return new Context('id', 1000, 'function', 'trace');
Expand Down

0 comments on commit d43faf4

Please sign in to comment.