Skip to content

Commit

Permalink
Test entire request (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
jderusse authored Mar 14, 2020
1 parent a5aac3a commit b47d586
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct(string $method, string $uri, array $query, array $he
{
$this->method = $method;
$this->uri = $uri;
$this->headers = [];
foreach ($headers as $key => $value) {
$this->headers[\strtolower($key)] = $value;
}
Expand Down
16 changes: 16 additions & 0 deletions src/Test/InternalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,20 @@ class InternalTestCase
public static function assertEqualsCanonicalizing($expected, $actual, string $message = ''): void
{
}

public static function assertEqualsIgnoringCase($expected, $actual, string $message = ''): void
{
}

public static function assertSame($expected, $actual, string $message = ''): void
{
}

public static function assertJsonStringEqualsJsonString($expected, $actual, string $message = ''): void
{
}

public static function assertXmlStringEqualsXmlString($expected, $actual, string $message = ''): void
{
}
}
80 changes: 80 additions & 0 deletions src/Test/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AsyncAws\Core\Test;

use AsyncAws\Core\Request;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;

class TestCase extends PHPUnitTestCase
Expand All @@ -16,6 +17,85 @@ public static function assertHttpFormEqualsHttpForm(string $expected, string $ac

self::assertEqualsCanonicalizing($expectedArray, $actualArray, $message);
}

/**
* Asserts that two Body documents are equal.
*/
public static function assertUrlEqualsUrl(string $expected, string $actual, string $message = '')
{
$actualUrl = \parse_url($actual);
$expectedUrl = \parse_url($expected);
self::assertSame($expectedUrl['path'] ?? '/', $actualUrl['path'] ?? '/');

$expectedQuery = [];
foreach (array_filter(\explode('&', $expectedUrl['query'] ?? '')) as $item) {
$item = explode('=', $item);
$expectedQuery[$item[0]] = \urldecode($item[1] ?? '');
}

$actualQuery = [];
foreach (array_filter(\explode('&', $actualUrl['query'] ?? '')) as $item) {
$item = explode('=', $item);
$actualQuery[$item[0]] = \urldecode($item[1] ?? '');
}
self::assertEqualsIgnoringCase($expectedQuery, $actualQuery);
}

/**
* Asserts that two Body documents are equal.
*/
public static function assertRequestEqualsHttpRequest(string $expected, Request $actual, string $message = '')
{
$expected = \explode("\n\n", trim($expected));
$headers = $expected[0];
$body = $expected[1] ?? '';
$headers = explode("\n", $headers);
\array_map('trim', $headers);
[$method, $url] = explode(' ', \array_shift($headers));

self::assertSame($method, $actual->getMethod());

$actualUrl = $actual->getUri();
if ($actual->getQuery()) {
$actualUrl .= false !== \strpos($actual->getUri(), '?') ? '&' : '?';
$actualUrl .= \http_build_query($actual->getQuery());
}
self::assertUrlEqualsUrl($url, $actualUrl);

$expectedHeaders = [];
foreach ($headers as $header) {
[$key, $value] = \explode(':', trim($header), 2);
$expectedHeaders[\strtolower($key)] = trim($value);
}
self::assertEqualsIgnoringCase($expectedHeaders, $actual->getHeaders(), $message);

switch ($expectedHeaders['content-type'] ?? null) {
case 'application/x-www-form-urlencoded':
self::assertHttpFormEqualsHttpForm(\trim($body), $actual->getBody()->stringify(), $message);

break;
case 'application/json':
if ('' === \trim($body)) {
self::assertSame($body, $actual->getBody()->stringify());
} else {
self::assertJsonStringEqualsJsonString(\trim($body), $actual->getBody()->stringify(), $message);
}

break;
case 'application/xml':
if ('' === \trim($body)) {
self::assertSame($body, $actual->getBody()->stringify());
} else {
self::assertXmlStringEqualsXmlString(\trim($body), $actual->getBody()->stringify(), $message);
}

break;
default:
self::assertSame(trim($body), $actual->getBody()->stringify());

break;
}
}
}
if (!\class_exists(PHPUnitTestCase::class)) {
\class_alias(InternalTestCase::class, PHPUnitTestCase::class);
Expand Down
41 changes: 22 additions & 19 deletions tests/Unit/Input/AssumeRoleRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class AssumeRoleRequestTest extends TestCase
{
public function testRequestBody(): void
public function testRequest(): void
{
$input = new AssumeRoleRequest([
'RoleArn' => 'arn:aws::iam::123456789012:role/demo',
Expand All @@ -36,25 +36,28 @@ public function testRequestBody(): void

/** @see https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html */
$expected = '
Action=AssumeRole
&Version=2011-06-15
&RoleArn=arn%3Aaws%3A%3Aiam%3A%3A123456789012%3Arole%2Fdemo
&RoleSessionName=John-session
&PolicyArns.member.1.arn=arn%3Aaws%3Aiam%3A%3A123456789012%3Apolicy%2Fdemopolicy1
&PolicyArns.member.2.arn=arn%3Aaws%3Aiam%3A%3A123456789012%3Apolicy%2Fdemopolicy2
&Policy=%7B%22Version%22%3A%222012-10-17%22%2C%22Statement%22%3A%5B%7B%22Sid%22%3A+%22Stmt1%22%2C%22Effect%22%3A+%22Allow%22%2C%22Action%22%3A+%22s3%3A%2A%22%2C%22Resource%22%3A+%22%2A%22%7D%5D%7D
&DurationSeconds=1800
&Tags.member.1.Key=Project
&Tags.member.1.Value=Pegasus
&Tags.member.2.Key=Team
&Tags.member.2.Value=Engineering
&Tags.member.3.Key=Cost-Center
&Tags.member.3.Value=12345
&TransitiveTagKeys.member.1=Project
&TransitiveTagKeys.member.2=Cost-Center
&ExternalId=123ABC
POST / HTTP/1.0
Content-Type: application/x-www-form-urlencoded
Action=AssumeRole
&Version=2011-06-15
&RoleArn=arn%3Aaws%3A%3Aiam%3A%3A123456789012%3Arole%2Fdemo
&RoleSessionName=John-session
&PolicyArns.member.1.arn=arn%3Aaws%3Aiam%3A%3A123456789012%3Apolicy%2Fdemopolicy1
&PolicyArns.member.2.arn=arn%3Aaws%3Aiam%3A%3A123456789012%3Apolicy%2Fdemopolicy2
&Policy=%7B%22Version%22%3A%222012-10-17%22%2C%22Statement%22%3A%5B%7B%22Sid%22%3A+%22Stmt1%22%2C%22Effect%22%3A+%22Allow%22%2C%22Action%22%3A+%22s3%3A%2A%22%2C%22Resource%22%3A+%22%2A%22%7D%5D%7D
&DurationSeconds=1800
&Tags.member.1.Key=Project
&Tags.member.1.Value=Pegasus
&Tags.member.2.Key=Team
&Tags.member.2.Value=Engineering
&Tags.member.3.Key=Cost-Center
&Tags.member.3.Value=12345
&TransitiveTagKeys.member.1=Project
&TransitiveTagKeys.member.2=Cost-Center
&ExternalId=123ABC
';

self::assertHttpFormEqualsHttpForm($expected, $input->request()->getBody()->stringify());
self::assertRequestEqualsHttpRequest($expected, $input->request());
}
}
7 changes: 5 additions & 2 deletions tests/Unit/Input/AssumeRoleWithWebIdentityRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class AssumeRoleWithWebIdentityRequestTest extends TestCase
{
public function testRequestBody(): void
public function testRequest(): void
{
$input = new AssumeRoleWithWebIdentityRequest([
'RoleArn' => 'arn:aws:iam::123456789012:role/FederatedWebIdentityRole',
Expand All @@ -25,6 +25,9 @@ public function testRequestBody(): void

// see https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRoleWithWebIdentity.html
$expected = '
POST / HTTP/1.0
Content-Type: application/x-www-form-urlencoded
Action=AssumeRoleWithWebIdentity
&Version=2011-06-15
&DurationSeconds=3600
Expand All @@ -36,6 +39,6 @@ public function testRequestBody(): void
&WebIdentityToken=FooBarBz
';

self::assertHttpFormEqualsHttpForm($expected, $input->request()->getBody()->stringify());
self::assertRequestEqualsHttpRequest($expected, $input->request());
}
}
11 changes: 7 additions & 4 deletions tests/Unit/Input/GetCallerIdentityRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@

class GetCallerIdentityRequestTest extends TestCase
{
public function testRequestBody(): void
public function testRequest(): void
{
$input = new GetCallerIdentityRequest();

/** @see https://docs.aws.amazon.com/STS/latest/APIReference/API_GetCallerIdentity.html */
$expected = '
Action=GetCallerIdentity
&Version=2011-06-15
POST / HTTP/1.0
Content-Type: application/x-www-form-urlencoded
Action=GetCallerIdentity
&Version=2011-06-15
';

self::assertHttpFormEqualsHttpForm($expected, $input->request()->getBody()->stringify());
self::assertRequestEqualsHttpRequest($expected, $input->request());
}
}

0 comments on commit b47d586

Please sign in to comment.