Skip to content

Commit

Permalink
Make requestContext available in $request->value("context")
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Aug 29, 2021
1 parent 45ae64d commit 5bdf0a9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ AWS Lambda Webservices change log

## ?.?.? / ????-??-??

## 0.5.0 / 2021-08-29

* Made events' requestContext member accessible via *request* value
(@thekid)
* Made remote address available in HTTP headers, populated via events'
requestContext.http.sourceIp member
(@thekid)
Expand Down
14 changes: 7 additions & 7 deletions src/main/php/com/amazon/aws/lambda/HttpApi.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ public function target() {

// Return event handler
return function($event, $context) use($routing, $logging) {
$request= new Request(new FromApiGateway($event));
$response= new Response(new ResponseDocument());
$req= new Request(new FromApiGateway($event));
$res= new Response(new ResponseDocument());

try {
foreach ($routing->service($request->pass('context', $context), $response) ?? [] as $_) { }
$logging->log($request, $response);
return $response->output()->document;
foreach ($routing->service($req->pass('context', $context)->pass('request', $event['requestContext'] ?? []), $res) ?? [] as $_) { }
$logging->log($req, $res);
return $res->output()->document;
} catch (Throwable $t) {
$e= $t instanceof Error ? $t : new InternalServerError($t);
$logging->log($request, $response, $e);
return $response->output()->error($e);
$logging->log($req, $res, $e);
return $res->output()->error($e);
}
};
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/php/com/amazon/aws/lambda/unittest/HttpApiTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,29 @@ public function routes($env) {
);
}

#[Test]
public function has_access_to_request() {
$fixture= new class($this->environment) extends HttpApi {
public function routes($env) {
return ['/' => function($req, $res) {
$res->answer(200);
$res->send('Hello '.$req->param('name').' from '.$req->value('request')['apiId'], 'text/plain');
}];
}
};

Assert::equals(
[
'statusCode' => 200,
'statusDescription' => 'OK',
'isBase64Encoded' => false,
'multiValueHeaders' => ['Content-Type' => ['text/plain'], 'Content-Length' => ['26']],
'body' => 'Hello Test from r3pmxmplak',
],
$this->invoke($fixture->target(), 'GET', 'name=Test')
);
}

#[Test]
public function has_access_to_context() {
$fixture= new class($this->environment) extends HttpApi {
Expand Down

0 comments on commit 5bdf0a9

Please sign in to comment.