Skip to content

Commit

Permalink
Merge pull request #1 from brefphp/basic-auth
Browse files Browse the repository at this point in the history
 #1325 Improve tests for basic auth and form data header
  • Loading branch information
georgeboot authored Nov 22, 2022
2 parents 74af099 + 2275a99 commit 31eb785
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 20 deletions.
4 changes: 0 additions & 4 deletions src/Event/Http/FpmHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,6 @@ private function eventToFastCgiRequest(HttpRequestEvent $event, Context $context
$request->setCustomVar('LAMBDA_INVOCATION_CONTEXT', json_encode($context));
$request->setCustomVar('LAMBDA_REQUEST_CONTEXT', json_encode($event->getRequestContext()));

[$user, $password] = $event->getBasicAuthCredentials();
$request->setCustomVar('PHP_AUTH_USER', $user);
$request->setCustomVar('PHP_AUTH_PW', $password);

$contentType = $event->getContentType();
if ($contentType) {
$request->setContentType($contentType);
Expand Down
31 changes: 19 additions & 12 deletions tests/Event/Http/CommonHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,20 @@ public function test POST request with form data(int $version)
]);
}

/**
* @dataProvider provide API Gateway versions
*/
public function test POST request with form data and content type(int $version)
{
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-body-form-content-type.json");

$this->assertContentType('application/x-www-form-urlencoded;charset=UTF-8');
$this->assertParsedBody([
'foo' => 'bar',
'bim' => 'baz',
]);
}

public function provideHttpMethodsWithRequestBodySupport(): array
{
return [
Expand Down Expand Up @@ -438,24 +452,17 @@ public function test path parameters(int $version)
]);
}

public function test request with basic auth contains a user and password()
/**
* @dataProvider provide API Gateway versions
*/
public function test request with basic auth(int $version)
{
$this->fromFixture(__DIR__ . '/Fixture/ag-v1-header-basic-auth.json');
$this->fromFixture(__DIR__ . "/Fixture/ag-v$version-header-basic-auth.json");

$this->assertBasicAuthUser('fake');
$this->assertBasicAuthPassword('secret');
}

public function test multipart form content type can have a suffix()
{
$this->fromFixture(__DIR__ . '/Fixture/ag-v1-body-base64-utf8.json');

$this->assertContentType('application/x-www-form-urlencoded;charset=UTF-8');
$this->assertParsedBody([
'foo' => 'bar',
]);
}

abstract protected function fromFixture(string $file): void;

abstract protected function assertBody(string $expected): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
"domainName": "example.org",
"apiId": "xxxxxxxxxx"
},
"body": "Zm9vPWJhcg==",
"isBase64Encoded": true
"body": "foo=bar&bim=baz",
"isBase64Encoded": false
}
3 changes: 1 addition & 2 deletions tests/Event/Http/Fixture/ag-v1-header-basic-auth.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"X-Amzn-Trace-Id": "Root=1-ffffffff-ffffffffffffffffffffffff",
"X-Forwarded-For": "1.1.1.1",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https",
"X-My-Header": "Hello world"
"X-Forwarded-Proto": "https"
},
"queryStringParameters": null,
"pathParameters": null,
Expand Down
42 changes: 42 additions & 0 deletions tests/Event/Http/Fixture/ag-v2-body-form-content-type.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0",
"routeKey": "ANY /path",
"rawPath": "/path",
"rawQueryString": "",
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Cache-Control": "no-cache",
"Content-Length": 15,
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"Host": "example.org",
"User-Agent": "PostmanRuntime/7.20.1",
"X-Amzn-Trace-Id": "Root=1-ffffffff-ffffffffffffffffffffffff",
"X-Forwarded-For": "1.1.1.1",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"queryStringParameters": null,
"stageVariables": null,
"requestContext": {
"accountId": "123400000000",
"apiId": "xxxxxxxxxx",
"domainName": "example.org",
"domainPrefix": "0000000000",
"http": {
"method": "POST",
"path": "/path",
"protocol": "HTTP/1.1",
"sourceIp": "1.1.1.1",
"userAgent": "PostmanRuntime/7.20.1"
},
"requestId": "JTHoQgr2oAMEPMg=",
"routeId": "47matwk",
"routeKey": "ANY /path",
"stage": "$default",
"time": "24/Nov/2019:18:55:08 +0000",
"timeEpoch": 1574621708700
},
"body": "foo=bar&bim=baz",
"isBase64Encoded": false
}
40 changes: 40 additions & 0 deletions tests/Event/Http/Fixture/ag-v2-header-basic-auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"version": "2.0",
"routeKey": "ANY /path",
"rawPath": "/path",
"rawQueryString": "",
"cookies": [],
"headers": {
"accept": "*/*",
"accept-encoding": "gzip, deflate",
"authorization": "Basic ZmFrZTpzZWNyZXQ=",
"cache-control": "no-cache",
"host": "example.org",
"user-agent": "PostmanRuntime/7.20.1",
"x-amzn-trace-id": "Root=1-ffffffff-ffffffffffffffffffffffff",
"x-forwarded-for": "1.1.1.1",
"x-forwarded-port": "443",
"x-forwarded-proto": "https"
},
"queryStringParameters": null,
"requestContext": {
"accountId": "123400000000",
"apiId": "xxxxxxxxxx",
"domainName": "example.org",
"domainPrefix": "0000000000",
"http": {
"method": "GET",
"path": "/path",
"protocol": "HTTP/1.1",
"sourceIp": "1.1.1.1",
"userAgent": "PostmanRuntime/7.20.1"
},
"requestId": "JTHoQgr2oAMEPMg=",
"routeId": "47matwk",
"routeKey": "ANY /path",
"stage": "$default",
"time": "24/Nov/2019:18:55:08 +0000",
"timeEpoch": 1574621708700
},
"isBase64Encoded": false
}
79 changes: 79 additions & 0 deletions tests/Handler/FpmHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,48 @@ public function test POST request with form data(int $version)
]);
}

/**
* @dataProvider provide API Gateway versions
*/
public function test POST request with form data and content type(int $version)
{
$event = [
'version' => '1.0',
'httpMethod' => 'POST',
'body' => 'foo=bar&bim=baz',
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
],
];
$this->assertGlobalVariables($event, [
'$_GET' => [],
'$_POST' => [
'foo' => 'bar',
'bim' => 'baz',
],
'$_FILES' => [],
'$_COOKIE' => [],
'$_REQUEST' => [
'foo' => 'bar',
'bim' => 'baz',
],
'$_SERVER' => [
'CONTENT_LENGTH' => '15',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded;charset=UTF-8',
'REQUEST_URI' => '/',
'PHP_SELF' => '/',
'PATH_INFO' => '/',
'REQUEST_METHOD' => 'POST',
'QUERY_STRING' => '',
'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded;charset=UTF-8',
'HTTP_CONTENT_LENGTH' => '15',
'LAMBDA_INVOCATION_CONTEXT' => json_encode($this->fakeContext),
'LAMBDA_REQUEST_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => 'foo=bar&bim=baz',
]);
}

public function provideHttpMethodsWithRequestBodySupport(): array
{
return [
Expand Down Expand Up @@ -975,6 +1017,43 @@ public function test OPTIONS request(int $version)
]);
}

/**
* @dataProvider provide API Gateway versions
*/
public function test request with basic auth(int $version)
{
$event = [
'version' => '1.0',
'httpMethod' => 'GET',
'headers' => [
'Authorization' => 'Basic ZmFrZTpzZWNyZXQ=',
],
];
$this->assertGlobalVariables($event, [
'$_GET' => [],
'$_POST' => [],
'$_FILES' => [],
'$_COOKIE' => [],
'$_REQUEST' => [],
'$_SERVER' => [
'REQUEST_URI' => '/',
'PHP_SELF' => '/',
'PATH_INFO' => '/',
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => '',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_INVOCATION_CONTEXT' => json_encode($this->fakeContext),
'LAMBDA_REQUEST_CONTEXT' => '[]',
'HTTP_AUTHORIZATION' => 'Basic ZmFrZTpzZWNyZXQ=',
// PHP-FPM automatically adds these variables
'PHP_AUTH_USER' => 'fake',
'PHP_AUTH_PW' => 'secret',
],
'HTTP_RAW_BODY' => '',
]);
}

/**
* @dataProvider provideStatusCodes
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/HttpRequestProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function test POST request with raw body(int $version);

public function test POST request with form data(int $version);

/**
* @see https://github.com/brefphp/bref/pull/1325
*/
public function test POST request with form data and content type(int $version);

/**
* @see https://github.com/brefphp/bref/issues/162
*/
Expand All @@ -51,4 +56,6 @@ public function test PATCH request(int $version);
public function test DELETE request(int $version);

public function test OPTIONS request(int $version);

public function test request with basic auth(int $version);
}

0 comments on commit 31eb785

Please sign in to comment.