Skip to content

Commit

Permalink
Add test for incalculable payload exception
Browse files Browse the repository at this point in the history
  • Loading branch information
howardlopez committed May 23, 2019
1 parent 52759d8 commit 03e5dfb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/StreamRequestPayloadMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public function __invoke(CommandInterface $command, RequestInterface $request)
$size = $request->getBody()->getSize();
if (is_null($size)) {
throw new IncalculablePayloadException('Payload'
. 'content lenggth is required and can not be'
. 'calculated.');
. ' content length is required and can not be'
. ' calculated.');
}
$request = $request->withHeader(
'content-length',
Expand Down
66 changes: 54 additions & 12 deletions tests/StreamRequestPayloadMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,7 @@ public function testAddsProperHeaders(
array $expectedHeaders,
array $expectedNonHeaders
) {
$service = $this->generateTestService();
$serializer = ClientResolver::_default_serializer([
'api' => $service,
'endpoint' => ''
]);

$list = new HandlerList();
$list->prependBuild(Middleware::requestBuilder($serializer), 'builder');
$list->prependSign(
StreamRequestPayloadMiddleware::wrap($service),
'StreamRequestPayloadMiddleware'
);
$list = $this->generateTestHandlerList();

$list->setHandler(function (
CommandInterface $command,
Expand Down Expand Up @@ -123,6 +112,59 @@ public function generateTestCases()
];
}

/**
* @expectedException \Aws\Exception\IncalculablePayloadException
* @expectedExceptionMessage Payload content length is required and can not be calculated.
*/
public function testThrowsExceptionOnIncalculableSize()
{
$service = $this->generateTestService();
$client = $this->generateTestClient($service);
$command = $client->getCommand(
'StreamingOp',
[
'InputStream' => Psr7\stream_for('test'),
]
);
$middleware = StreamRequestPayloadMiddleware::wrap($service);
$invokable = $middleware(function($cmd, $req) {});

// Mock a request with a body whose size returns null
$streamMock = $this->getMockBuilder(\stdClass::class)
->setMethods(['getSize'])
->getMock();
$streamMock->expects($this->any())
->method('getSize')
->willReturn(null);
$requestMock = $this->getMockBuilder(Request::class)
->setConstructorArgs(['POST', 'https://foo.com'])
->setMethods(['getBody'])
->getMock();
$requestMock->expects($this->any())
->method('getBody')
->willReturn($streamMock);

$invokable($command, $requestMock);
}

private function generateTestHandlerList()
{
$service = $this->generateTestService();
$serializer = ClientResolver::_default_serializer([
'api' => $service,
'endpoint' => ''
]);

$list = new HandlerList();
$list->prependBuild(Middleware::requestBuilder($serializer), 'builder');
$list->prependSign(
StreamRequestPayloadMiddleware::wrap($service),
'StreamRequestPayloadMiddleware'
);

return $list;
}

private function generateTestClient(Service $service, $args = [])
{
return new AwsClient(
Expand Down

0 comments on commit 03e5dfb

Please sign in to comment.