-
Notifications
You must be signed in to change notification settings - Fork 60
/
UserContextFailureTest.php
100 lines (87 loc) · 3.06 KB
/
UserContextFailureTest.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
<?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')]
class UserContextFailureTest extends VarnishTestCase
{
/**
* Can be "cache" or "failure" and is used to determine the correct .vcl file.
*/
private string $mode = 'cache';
public function setUp(): void
{
// needs to be decided before doing the setup
// phpunit 9 calls the method getName(), phpunit 10 name()
$name = method_exists($this, 'name') ? $this->name() : $this->getName(); /* @phpstan-ignore-line */
$this->mode = 'testHashRequestFailure' === $name ? 'failure' : 'cache';
parent::setUp();
}
/**
* The user hash must not be exposed to the client under any circumstances.
*/
public function testUserContextNoExposeHash(): void
{
$response = $this->getResponse(
'/user_context_hash_nocache.php',
[
'Accept' => 'application/vnd.fos.user-context-hash',
'Cookie' => ['0=miam'],
]
);
$this->assertEquals(400, $response->getStatusCode());
$this->assertFalse($response->hasHeader('X-User-Context-Hash'));
}
/**
* A hash sent by the client must not be used by varnish.
*/
public function testUserContextNoForgedHash(): void
{
$response = $this->getResponse(
'/user_context_hash_nocache.php',
[
'X-User-Context-Hash' => 'miam',
'Cookie' => ['0=miam'],
]
);
$this->assertEquals(400, $response->getStatusCode());
}
/**
* A request on POST should not use the context.
*/
public function testUserContextNotUsed(): void
{
// First request in GET
$this->getResponse('/user_context.php', ['Cookie' => '0=foo']);
// Second request in HEAD or POST
$postResponse = $this->getResponse(
'/user_context.php',
['Cookie' => '0=foo'],
'POST'
);
$this->assertEquals('POST', $postResponse->getBody());
$this->assertEquals('MISS', $postResponse->getHeaderLine('X-HashCache'));
$this->assertMiss($postResponse);
}
public function testHashRequestFailure(): void
{
$response = $this->getResponse('/user_context.php', ['Cookie' => '0=foo']);
$this->assertEquals(503, $response->getStatusCode());
}
protected function getConfigFile(): string
{
return match ((int) $this->getVarnishVersion()) {
3 => sprintf(dirname(__DIR__).'/Fixtures/varnish-3/user_context_%s.vcl', $this->mode),
default => sprintf(dirname(__DIR__).'/Fixtures/varnish/user_context_%s.vcl', $this->mode),
};
}
}