forked from aws/aws-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuzzleV5HandlerTest.php
83 lines (72 loc) · 2.6 KB
/
GuzzleV5HandlerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace Aws\Test\Integ;
use Aws\Handler\GuzzleV5\GuzzleHandler;
use GuzzleHttp\Promise\RejectionException;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
class GuzzleV5HandlerTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!class_exists('GuzzleHttp\Ring\Core')) {
$this->markTestSkipped();
}
}
public function testSendRequest()
{
$handler = new GuzzleHandler();
$request = new Request(
'PUT',
"http://httpbin.org/put?a=1&b=2",
['c' => '3', 'd' => '4', 'user-agent' => 'AWS/3'],
Psr7\stream_for('{"f":6,"g":7}')
);
$sink = Psr7\stream_for();
/** @var \GuzzleHttp\Promise\Promise $responsePromise */
$responsePromise = $handler($request, ['sink' => $sink]);
$responsePromise = $responsePromise->then(
function (Response $resp) {
return $resp->withHeader('e', '5');
},
function (array $error) {
$this->fail('The request failed.');
}
);
/** @var Response $response */
$response = $responsePromise->wait();
$body = $response->getBody()->getContents();
$data = json_decode($body, true);
// Check response data.
$this->assertArrayHasKey('C', $data['headers']);
$this->assertArrayHasKey('D', $data['headers']);
$this->assertStringStartsWith('AWS/3 Guzzle/5', $data['headers']['User-Agent']);
$this->assertArrayHasKey('a', $data['args']);
$this->assertArrayHasKey('b', $data['args']);
$this->assertArrayHasKey('f', $data['json']);
$this->assertArrayHasKey('g', $data['json']);
// Check response data.
$this->assertTrue($response->hasHeader('E'));
// Check the sink.
$sink->seek(0);
$this->assertEquals($body, $sink->getContents());
}
public function testProduceErrorData()
{
$handler = new GuzzleHandler();
$request = new Request('GET', 'http://httpbin.org/delay/3');
try {
$handler($request, ['timeout' => 1])->wait();
} catch (RejectionException $e) {
$error = $e->getReason();
$this->assertInstanceOf(
'GuzzleHttp\Exception\ConnectException',
$error['exception']
);
$this->assertTrue($error['connection_error']);
$this->assertArrayHasKey('response', $error);
return;
}
$this->fail('A RejectionException should have been thrown.');
}
}