-
Notifications
You must be signed in to change notification settings - Fork 60
/
UserContextTestCase.php
110 lines (93 loc) · 4.58 KB
/
UserContextTestCase.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
/*
* 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\Functional\Varnish;
use FOS\HttpCache\Test\VarnishTestCase;
use PHPUnit\Framework\Attributes as PHPUnit;
#[PHPUnit\Group('webserver')]
#[PHPUnit\Group('varnish')]
abstract class UserContextTestCase extends VarnishTestCase
{
/**
* Assert that the context cache status is as expected.
*
* @param string $hashCache the cache status of the context request
*/
abstract protected function assertContextCache(string $hashCache): void;
/**
* Sending requests without an Accept: header so none should arrive at the
* backend for the actual request.
*/
public function testUserContextHash(): void
{
$response1 = $this->getResponse('/user_context.php', ['Cookie' => ['0=foo']]);
$this->assertEquals('foo', (string) $response1->getBody());
$this->assertEquals('MISS', $response1->getHeaderLine('X-HashCache'));
$response2 = $this->getResponse('/user_context.php', ['Cookie' => ['0=bar']]);
$this->assertEquals('bar', (string) $response2->getBody());
$this->assertEquals('MISS', $response2->getHeaderLine('X-HashCache'));
$cachedResponse1 = $this->getResponse('/user_context.php', ['Cookie' => ['0=foo']]);
$this->assertEquals('foo', (string) $cachedResponse1->getBody());
$this->assertContextCache($cachedResponse1->getHeaderLine('X-HashCache'));
$this->assertHit($cachedResponse1);
$cachedResponse2 = $this->getResponse('/user_context.php', ['Cookie' => ['0=bar']]);
$this->assertEquals('bar', $cachedResponse2->getBody());
$this->assertContextCache($cachedResponse2->getHeaderLine('X-HashCache'));
$this->assertHit($cachedResponse2);
$headResponse1 = $this->getResponse('/user_context.php', ['Cookie' => ['0=foo']], 'HEAD');
$this->assertEquals('foo', $headResponse1->getHeaderLine('X-HashTest'));
$this->assertContextCache($headResponse1->getHeaderLine('X-HashCache'));
$this->assertHit($headResponse1);
$headResponse2 = $this->getResponse('/user_context.php', ['Cookie' => ['0=bar']], 'HEAD');
$this->assertEquals('bar', $headResponse2->getHeaderLine('X-HashTest'));
$this->assertContextCache($headResponse2->getHeaderLine('X-HashCache'));
$this->assertHit($headResponse2);
}
/**
* Making sure that non-authenticated and authenticated cache are not mixed up.
*/
public function testUserContextNoAuth(): void
{
$response1 = $this->getResponse('/user_context_anon.php');
$this->assertEquals('anonymous', $response1->getBody());
$this->assertEquals('MISS', $response1->getHeaderLine('X-HashCache'));
$response1 = $this->getResponse('/user_context_anon.php', ['Cookie' => ['0=foo']]);
$this->assertEquals('foo', (string) $response1->getBody());
$this->assertEquals('MISS', $response1->getHeaderLine('X-HashCache'));
$cachedResponse1 = $this->getResponse('/user_context_anon.php');
$this->assertEquals('anonymous', (string) $cachedResponse1->getBody());
$this->assertHit($cachedResponse1);
$cachedResponse2 = $this->getResponse('/user_context_anon.php', ['Cookie' => ['0=foo']]);
$this->assertEquals('foo', (string) $cachedResponse2->getBody());
$this->assertContextCache($cachedResponse2->getHeaderLine('X-HashCache'));
$this->assertHit($cachedResponse2);
}
public function testAcceptHeader(): void
{
$response1 = $this->getResponse(
'/user_context.php?accept=text/plain',
[
'Accept' => 'text/plain',
'Cookie' => '0=foo',
]
);
$this->assertEquals('foo', $response1->getBody());
}
public function testUserContextUnauthorized(): void
{
$response = $this->getResponse('/user_context.php', ['Cookie' => ['0=miam']]);
$this->assertEquals(403, $response->getStatusCode());
$this->assertEquals('MISS', $response->getHeaderLine('X-HashCache'));
$this->assertEquals(403, $response->getStatusCode());
$response = $this->getResponse('/user_context.php', ['Cookie' => ['0=miam']]);
$this->assertEquals(403, $response->getStatusCode());
$this->assertContextCache($response->getHeaderLine('X-HashCache'));
$this->assertEquals(403, $response->getStatusCode());
}
}