1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\RestETagBundle\Tests\Functional; |
10
|
|
|
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author John Kleijn <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class FunctionalTest extends WebTestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @test |
21
|
|
|
*/ |
22
|
|
|
public function willAddETagForNewResources() |
23
|
|
|
{ |
24
|
|
|
$client = self::createClient(); |
25
|
|
|
$client->request('POST', '/foo/bar'); |
26
|
|
|
$response = $client->getResponse(); |
27
|
|
|
$this->assertRegExp('/\d{10}\.\d+/', (string)$response->getEtag()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @test |
32
|
|
|
*/ |
33
|
|
|
public function willReturnNotModifiedResponseWhenUsingETag() |
34
|
|
|
{ |
35
|
|
|
$client = self::createClient(); |
36
|
|
|
$client->disableReboot(); |
37
|
|
|
$client->request('POST', '/foo/bar'); |
38
|
|
|
$response = $client->getResponse(); |
39
|
|
|
$client->request('GET', '/foo/bar', [], [], ['HTTP_IF_NONE_MATCH' => $response->getEtag()]); |
40
|
|
|
$response = $client->getResponse(); |
41
|
|
|
$this->assertSame(Response::HTTP_NOT_MODIFIED, $response->getStatusCode()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @test |
46
|
|
|
*/ |
47
|
|
|
public function willInvalidateWhenPostingToParent() |
48
|
|
|
{ |
49
|
|
|
$this->assertWillInvalidateWhenPostingToParent('/foo/bar', '/foo/bar/doh'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @test |
54
|
|
|
*/ |
55
|
|
|
public function willTreatQueryAsASegment() |
56
|
|
|
{ |
57
|
|
|
$this->assertWillInvalidateWhenPostingToParent('/foo/bar', '/foo/bar?doh=1'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @test |
62
|
|
|
*/ |
63
|
|
|
public function willAddETagForKnownResources() |
64
|
|
|
{ |
65
|
|
|
$client = self::createClient(); |
66
|
|
|
$client->disableReboot(); |
67
|
|
|
$client->request('POST', '/foo/bar'); |
68
|
|
|
$response = $client->getResponse(); |
69
|
|
|
$eTag = (string)$response->getEtag(); |
70
|
|
|
$client->request('GET', '/foo/bar'); |
71
|
|
|
$response = $client->getResponse(); |
72
|
|
|
$this->assertSame($eTag, (string)$response->getEtag()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @test |
77
|
|
|
*/ |
78
|
|
|
public function willDoResourceLocking() |
79
|
|
|
{ |
80
|
|
|
$client = self::createClient(); |
81
|
|
|
$client->disableReboot(); |
82
|
|
|
$client->request('POST', '/foo/bar'); |
83
|
|
|
$staleETag = (string)$client->getResponse()->getEtag(); |
84
|
|
|
$client->request('POST', '/foo/bar', [], [], ['HTTP_IF_MATCH' => $staleETag]); |
85
|
|
|
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode()); |
86
|
|
|
$client->request('POST', '/foo/bar', [], [], ['HTTP_IF_MATCH' => $staleETag]); |
87
|
|
|
$this->assertSame(Response::HTTP_PRECONDITION_FAILED, $client->getResponse()->getStatusCode()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $parentUrl |
92
|
|
|
* @param string $childUrl |
93
|
|
|
*/ |
94
|
|
|
public function assertWillInvalidateWhenPostingToParent($parentUrl, $childUrl) |
95
|
|
|
{ |
96
|
|
|
$client = self::createClient(); |
97
|
|
|
$client->disableReboot(); |
98
|
|
|
$client->request('GET', $childUrl); |
99
|
|
|
$response = $client->getResponse(); |
100
|
|
|
$originalEtag = $response->getEtag(); |
101
|
|
|
$this->assertNotEmpty($originalEtag); |
102
|
|
|
|
103
|
|
|
// Sanity check |
104
|
|
|
$client->request('GET', $childUrl, [], [], ['HTTP_IF_NONE_MATCH' => $originalEtag]); |
105
|
|
|
$response = $client->getResponse(); |
106
|
|
|
$this->assertSame(Response::HTTP_NOT_MODIFIED, $response->getStatusCode()); |
107
|
|
|
|
108
|
|
|
// Validate that when we post to what should be the parent, the resource is marked as modified |
109
|
|
|
// Strict concurrency control is turned on, so we always need to pass the current E-Tag. |
110
|
|
|
// Since we just registered a child path, the correct key is the same |
111
|
|
|
$client->request('POST', $parentUrl, [], [], ['HTTP_IF_MATCH' => $originalEtag]); |
112
|
|
|
|
113
|
|
|
// Sanity check |
114
|
|
|
$response = $client->getResponse(); |
115
|
|
|
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
116
|
|
|
|
117
|
|
|
// Now validate the child is re-evaluated |
118
|
|
|
$client->request('GET', $childUrl, [], [], ['HTTP_IF_NONE_MATCH' => $originalEtag]); |
119
|
|
|
$this->assertSame(Response::HTTP_OK, $response->getStatusCode()); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|