Repository navigation
Expand file tree
/
Copy pathCloudflareTest.php
More file actions
149 lines (119 loc) · 5.34 KB
/
Copy pathCloudflareTest.php
File metadata and controls
149 lines (119 loc) · 5.34 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\HttpCache\Tests\Unit\ProxyClient;
use FOS\HttpCache\ProxyClient\Cloudflare;
use FOS\HttpCache\ProxyClient\HttpDispatcher;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery\MockInterface;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
class CloudflareTest extends TestCase
{
use MockeryPHPUnitIntegration;
public const AUTH_TOKEN = 'abc123';
public const ZONE_IDENTIFIER = 'abcdef123abcdef123';
private HttpDispatcher&MockInterface $httpDispatcher;
protected function setUp(): void
{
parent::setUp();
$this->httpDispatcher = \Mockery::mock(HttpDispatcher::class);
}
protected function tearDown(): void
{
unset($this->httpDispatcher);
parent::tearDown();
}
protected function getProxyClient(array $options = []): Cloudflare
{
$options = [
'authentication_token' => self::AUTH_TOKEN,
'zone_identifier' => self::ZONE_IDENTIFIER,
] + $options;
return new Cloudflare($this->httpDispatcher, $options);
}
public function testInvalidateTagsPurge(): void
{
$cloudflare = $this->getProxyClient();
$this->httpDispatcher->shouldReceive('invalidate')->once()->with(
\Mockery::on(
function (RequestInterface $request) {
$this->assertEquals('POST', $request->getMethod());
$this->assertEquals('Bearer '.self::AUTH_TOKEN, current($request->getHeader('Authorization')));
$this->assertEquals(sprintf('/client/v4/zones/%s/purge_cache', self::ZONE_IDENTIFIER), $request->getRequestTarget());
$this->assertEquals('{"tags":["tag-one","tag-two"]}', $request->getBody()->getContents());
return true;
}
),
false
);
$cloudflare->invalidateTags(['tag-one', 'tag-two']);
}
public function testInvalidatePrefixesPurge(): void
{
$cloudflare = $this->getProxyClient();
$this->httpDispatcher->shouldReceive('invalidate')->once()->with(
\Mockery::on(
function (RequestInterface $request) {
$this->assertEquals('POST', $request->getMethod());
$this->assertEquals('Bearer '.self::AUTH_TOKEN, current($request->getHeader('Authorization')));
$this->assertEquals(sprintf('/client/v4/zones/%s/purge_cache', self::ZONE_IDENTIFIER), $request->getRequestTarget());
$this->assertEquals('{"prefixes":["example.com/one/","example.com/two/"]}', $request->getBody()->getContents());
return true;
}
),
false
);
$cloudflare->invalidatePrefixes(['example.com/one/', 'example.com/two/']);
}
public function testPurge(): void
{
$cloudflare = $this->getProxyClient();
$expected = '{"files":["http://example.com/url-one","http://example.com/url-two",{"url":"http://example.com/url-three","headers":{"Origin":"https://www.cloudflare.com","CF-IPCountry":"US","CF-Device-Type":"desktop"}}]}';
$this->httpDispatcher->shouldReceive('invalidate')->once()->with(
\Mockery::on(
function (RequestInterface $request) use ($expected) {
$this->assertEquals('POST', $request->getMethod());
$this->assertEquals('Bearer '.self::AUTH_TOKEN, current($request->getHeader('Authorization')));
$this->assertEquals(sprintf('/client/v4/zones/%s/purge_cache', self::ZONE_IDENTIFIER), $request->getRequestTarget());
$this->assertEquals($expected, $request->getBody()->getContents());
return true;
}
),
false
);
$this->httpDispatcher->shouldReceive('flush')->once();
$cloudflare->purge('http://example.com/url-one');
$cloudflare->purge('http://example.com/url-two');
$cloudflare->purge('http://example.com/url-three', [
'Origin' => 'https://www.cloudflare.com',
'CF-IPCountry' => 'US',
'CF-Device-Type' => 'desktop',
]);
$cloudflare->flush();
}
public function testClear(): void
{
$cloudflare = $this->getProxyClient();
$this->httpDispatcher->shouldReceive('invalidate')->once()->with(
\Mockery::on(
function (RequestInterface $request) {
$this->assertEquals('POST', $request->getMethod());
$this->assertEquals('Bearer '.self::AUTH_TOKEN, current($request->getHeader('Authorization')));
$this->assertEquals('application/json', $request->getHeaderLine('Accept'));
$this->assertEquals(sprintf('/client/v4/zones/%s/purge_cache', self::ZONE_IDENTIFIER), $request->getRequestTarget());
$this->assertEquals('{"purge_everything":true}', $request->getBody()->getContents());
return true;
}
),
false
);
$cloudflare->clear();
}
}