forked from aws/aws-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplyChecksumMiddlewareTest.php
110 lines (102 loc) · 3.38 KB
/
ApplyChecksumMiddlewareTest.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Aws\Test\S3;
use Aws\Middleware;
use Aws\Test\UsesServiceTrait;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;
/**
* @covers Aws\S3\ApplyChecksumMiddleware
*/
class ApplyChecksumMiddlewareTest extends \PHPUnit_Framework_TestCase
{
use UsesServiceTrait;
/**
* @dataProvider getContentMd5UseCases
*/
public function testAddsContentMd5AsAppropriate($options, $operation, $args, $md5Added, $md5Value)
{
$s3 = $this->getTestClient('s3', $options);
$this->addMockResults($s3, [[]]);
$command = $s3->getCommand($operation, $args);
$command->getHandlerList()->appendBuild(
Middleware::tap(function ($cmd, RequestInterface $request) use ($md5Added, $md5Value) {
$this->assertSame($md5Added, $request->hasHeader('Content-MD5'));
$this->assertEquals($md5Value, $request->getHeaderLine('Content-MD5'));
})
);
$s3->execute($command);
}
public function getContentMd5UseCases()
{
$md5 = '/12roh/ATpPMcGD9Rj4ZlQ==';
return [
// Do nothing if Content MD5 was explicitly provided.
[
[],
'DeleteObjects',
['Bucket' => 'foo', 'Delete' => ['Objects' => [['Key' => 'bar']]]],
true,
$md5
],
// Gets added for operations that require it
[
[],
'DeleteObjects',
['Bucket' => 'foo', 'Delete' => ['Objects' => [['Key' => 'bar']]]],
true,
$md5
],
// Not added for operations that does not require it
[
[],
'GetObject',
['Bucket' => 'foo', 'Key' => 'bar'],
false,
null,
],
];
}
/**
* @dataProvider getContentSha256UseCases
*/
public function testAddsContentSHA256AsAppropriate($operation, $args, $hashAdded, $hashValue)
{
$s3 = $this->getTestClient('s3');
$this->addMockResults($s3, [[]]);
$command = $s3->getCommand($operation, $args);
$command->getHandlerList()->appendBuild(
Middleware::tap(function ($cmd, RequestInterface $request) use ($hashAdded, $hashValue) {
$this->assertSame($hashAdded, $request->hasHeader('x-amz-content-sha256'));
$this->assertEquals($hashValue, $request->getHeaderLine('x-amz-content-sha256'));
})
);
$s3->execute($command);
}
public function getContentSha256UseCases()
{
$hash = 'SHA256HASH';
return [
// Do nothing if ContentSHA256 was not provided.
[
'PutObject',
['Bucket' => 'foo', 'Key' => 'bar', 'Body' => 'baz'],
false,
''
],
// Gets added for operations that allow it.
[
'PutObject',
['Bucket' => 'foo', 'Key' => 'bar', 'Body' => 'baz', 'ContentSHA256' => $hash],
true,
$hash
],
// Not added for operations that do not allow it.
[
'GetObject',
['Bucket' => 'foo', 'Key' => 'bar', 'ContentSHA256' => $hash],
false,
'',
],
];
}
}