1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\Middleware; |
4
|
|
|
|
5
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
6
|
|
|
use Nyholm\Psr7\Response; |
7
|
|
|
use Nyholm\Psr7\ServerRequest; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Yiisoft\Http\Method; |
13
|
|
|
use Yiisoft\Yii\Web\Middleware\HttpCache; |
14
|
|
|
|
15
|
|
|
class HttpCacheTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @test |
19
|
|
|
*/ |
20
|
|
|
public function notCacheableMethods(): void |
21
|
|
|
{ |
22
|
|
|
$time = \time(); |
23
|
|
|
$middleware = $this->createMiddlewareWithLastModified($time + 1); |
24
|
|
|
$response = $middleware->process($this->createServerRequest(Method::PATCH), $this->createRequestHandler()); |
25
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
26
|
|
|
$this->assertFalse($response->hasHeader('Last-Modified')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @test |
31
|
|
|
*/ |
32
|
|
|
public function modifiedResultWithLastModified(): void |
33
|
|
|
{ |
34
|
|
|
$time = \time(); |
35
|
|
|
$middleware = $this->createMiddlewareWithLastModified($time + 1); |
36
|
|
|
$headers = [ |
37
|
|
|
'If-Modified-Since' => gmdate('D, d M Y H:i:s', $time) . 'GMT', |
38
|
|
|
]; |
39
|
|
|
$response = $middleware->process($this->createServerRequest(Method::GET, $headers), $this->createRequestHandler()); |
40
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @test |
45
|
|
|
*/ |
46
|
|
|
public function modifiedResultWithEtag(): void |
47
|
|
|
{ |
48
|
|
|
$etag = 'test-etag'; |
49
|
|
|
$middleware = $this->createMiddlewareWithETag($etag); |
50
|
|
|
$headers = [ |
51
|
|
|
'If-None-Match' => implode(',', [$etag]), |
52
|
|
|
]; |
53
|
|
|
$response = $middleware->process($this->createServerRequest(Method::GET, $headers), $this->createRequestHandler()); |
54
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
55
|
|
|
$this->assertEquals($response->getHeaderLine('Etag'), $this->generateEtag($etag)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @test |
60
|
|
|
*/ |
61
|
|
|
public function notModifiedResultWithLastModified(): void |
62
|
|
|
{ |
63
|
|
|
$time = \time(); |
64
|
|
|
$middleware = $this->createMiddlewareWithLastModified($time - 1); |
65
|
|
|
$headers = [ |
66
|
|
|
'If-Modified-Since' => gmdate('D, d M Y H:i:s', $time) . 'GMT', |
67
|
|
|
]; |
68
|
|
|
$response = $middleware->process($this->createServerRequest(Method::GET, $headers), $this->createRequestHandler()); |
69
|
|
|
$this->assertEquals(304, $response->getStatusCode()); |
70
|
|
|
$this->assertEmpty((string)$response->getBody()); |
71
|
|
|
$this->assertEquals(gmdate('D, d M Y H:i:s', $time - 1) . ' GMT', $response->getHeaderLine('Last-Modified')); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
*/ |
77
|
|
|
public function notModifiedResultWithEtag(): void |
78
|
|
|
{ |
79
|
|
|
$etag = 'test-etag'; |
80
|
|
|
$middleware = $this->createMiddlewareWithETag($etag); |
81
|
|
|
$headers = [ |
82
|
|
|
'If-None-Match' => implode(',', [$this->generateEtag($etag)]), |
83
|
|
|
]; |
84
|
|
|
$response = $middleware->process($this->createServerRequest(Method::GET, $headers), $this->createRequestHandler()); |
85
|
|
|
$this->assertEquals(304, $response->getStatusCode()); |
86
|
|
|
$this->assertEmpty((string)$response->getBody()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function createMiddlewareWithLastModified(int $lastModified): HttpCache |
90
|
|
|
{ |
91
|
|
|
$middleware = new HttpCache(new Psr17Factory()); |
92
|
|
|
$middleware->setLastModified(static function (ServerRequestInterface $request, $params) use ($lastModified) { |
|
|
|
|
93
|
|
|
return $lastModified; |
94
|
|
|
}); |
95
|
|
|
return $middleware; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function createMiddlewareWithETag(string $etag): HttpCache |
99
|
|
|
{ |
100
|
|
|
$middleware = new HttpCache(new Psr17Factory()); |
101
|
|
|
$middleware->setEtagSeed(static function (ServerRequestInterface $request, $params) use ($etag) { |
|
|
|
|
102
|
|
|
return $etag; |
103
|
|
|
}); |
104
|
|
|
return $middleware; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function createRequestHandler(): RequestHandlerInterface |
108
|
|
|
{ |
109
|
|
|
return new class implements RequestHandlerInterface { |
110
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
111
|
|
|
{ |
112
|
|
|
return new Response(200); |
113
|
|
|
} |
114
|
|
|
}; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function createServerRequest(string $method = Method::GET, $headers = []): ServerRequestInterface |
118
|
|
|
{ |
119
|
|
|
return new ServerRequest($method, '/', $headers); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function generateEtag(string $seed, ?string $weakEtag = null): string |
123
|
|
|
{ |
124
|
|
|
$etag = '"' . rtrim(base64_encode(sha1($seed, true)), '=') . '"'; |
125
|
|
|
return $weakEtag ? 'W/' . $etag : $etag; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.