1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Buzz\Test\Unit\Cookie; |
6
|
|
|
|
7
|
|
|
use Buzz\Util\Cookie; |
8
|
|
|
use Nyholm\Psr7\Request; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
class CookieTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function testFromSetCookieHeaderSetsCookieAttributes() |
14
|
|
|
{ |
15
|
|
|
$cookie = new Cookie(); |
16
|
|
|
$cookie->fromSetCookieHeader('SESSION=asdf; expires='.date('r', strtotime('2000-01-01 00:00:00')).'; path=/; domain=.example.com; secure', 'www.example.com'); |
17
|
|
|
|
18
|
|
|
$this->assertEquals('SESSION', $cookie->getName()); |
19
|
|
|
$this->assertEquals('asdf', $cookie->getValue()); |
20
|
|
|
$this->assertEquals([ |
21
|
|
|
'expires' => date('r', strtotime('2000-01-01 00:00:00')), |
22
|
|
|
'path' => '/', |
23
|
|
|
'domain' => '.example.com', |
24
|
|
|
'secure' => null, |
25
|
|
|
], $cookie->getAttributes()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testFromSetCookieHeaderFallsBackToIssuingDomain() |
29
|
|
|
{ |
30
|
|
|
$cookie = new Cookie(); |
31
|
|
|
$cookie->fromSetCookieHeader('SESSION=asdf', 'example.com'); |
32
|
|
|
|
33
|
|
|
$this->assertEquals('example.com', $cookie->getAttribute(Cookie::ATTR_DOMAIN)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testToCookieHeaderFormatsACookieHeader() |
37
|
|
|
{ |
38
|
|
|
$cookie = new Cookie(); |
39
|
|
|
$cookie->setName('SESSION'); |
40
|
|
|
$cookie->setValue('asdf'); |
41
|
|
|
|
42
|
|
|
$this->assertEquals('SESSION=asdf', $cookie->toCookieHeader()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testMatchesDomainMatchesSimpleDomains() |
46
|
|
|
{ |
47
|
|
|
$cookie = new Cookie(); |
48
|
|
|
$cookie->setAttribute('domain', 'nytimes.com'); |
49
|
|
|
|
50
|
|
|
$this->assertTrue($cookie->matchesDomain('nytimes.com')); |
51
|
|
|
$this->assertFalse($cookie->matchesDomain('google.com')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testMatchesDomainMatchesSubdomains() |
55
|
|
|
{ |
56
|
|
|
$cookie = new Cookie(); |
57
|
|
|
$cookie->setAttribute('domain', '.nytimes.com'); |
58
|
|
|
|
59
|
|
|
$this->assertTrue($cookie->matchesDomain('nytimes.com')); |
60
|
|
|
$this->assertTrue($cookie->matchesDomain('blogs.nytimes.com')); |
61
|
|
|
$this->assertFalse($cookie->matchesDomain('google.com')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testIsExpiredChecksMaxAge() |
65
|
|
|
{ |
66
|
|
|
$cookie = new Cookie(); |
67
|
|
|
$cookie->setAttribute('max-age', '60'); |
68
|
|
|
|
69
|
|
|
$this->assertFalse($cookie->isExpired()); |
70
|
|
|
|
71
|
|
|
$cookie = new Cookie(); |
72
|
|
|
$cookie->setCreatedAt(strtotime('-1 hour')); |
73
|
|
|
$cookie->setAttribute('max-age', '60'); |
74
|
|
|
|
75
|
|
|
$this->assertTrue($cookie->isExpired()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testIsExpiredChecksExpires() |
79
|
|
|
{ |
80
|
|
|
$cookie = new Cookie(); |
81
|
|
|
$cookie->setAttribute('expires', date('r', strtotime('+1 week'))); |
82
|
|
|
|
83
|
|
|
$this->assertFalse($cookie->isExpired()); |
84
|
|
|
|
85
|
|
|
$cookie = new Cookie(); |
86
|
|
|
$cookie->setAttribute('expires', date('r', strtotime('-1 month'))); |
87
|
|
|
|
88
|
|
|
$this->assertTrue($cookie->isExpired()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testMatchesPathChecksPath() |
92
|
|
|
{ |
93
|
|
|
$cookie = new Cookie(); |
94
|
|
|
$cookie->setAttribute('path', '/resource'); |
95
|
|
|
|
96
|
|
|
$this->assertTrue($cookie->matchesPath('/resource/123')); |
97
|
|
|
$this->assertFalse($cookie->matchesPath('/login')); |
98
|
|
|
|
99
|
|
|
$cookie = new Cookie(); |
100
|
|
|
$this->assertTrue($cookie->matchesPath('/resource/123')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testMatchesRequestChecksDomain() |
104
|
|
|
{ |
105
|
|
|
$request = new Request('GET', 'http://example.com'); |
106
|
|
|
|
107
|
|
|
$cookie = new Cookie(); |
108
|
|
|
$cookie->setAttribute(Cookie::ATTR_DOMAIN, 'example.com'); |
109
|
|
|
|
110
|
|
|
$this->assertTrue($cookie->matchesRequest($request)); |
111
|
|
|
|
112
|
|
|
$cookie = new Cookie(); |
113
|
|
|
$cookie->setAttribute(Cookie::ATTR_DOMAIN, 'foo.com'); |
114
|
|
|
|
115
|
|
|
$this->assertFalse($cookie->matchesRequest($request)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testMatchesRequestChecksPath() |
119
|
|
|
{ |
120
|
|
|
$request = new Request('GET', 'http://example.com/foo/bar'); |
121
|
|
|
|
122
|
|
|
$cookie = new Cookie(); |
123
|
|
|
$cookie->setAttribute(Cookie::ATTR_DOMAIN, 'example.com'); |
124
|
|
|
$cookie->setAttribute(Cookie::ATTR_PATH, '/foo'); |
125
|
|
|
|
126
|
|
|
$this->assertTrue($cookie->matchesRequest($request)); |
127
|
|
|
|
128
|
|
|
$cookie = new Cookie(); |
129
|
|
|
$cookie->setAttribute(Cookie::ATTR_DOMAIN, 'example.com'); |
130
|
|
|
$cookie->setAttribute(Cookie::ATTR_PATH, '/foo/bar/baz'); |
131
|
|
|
|
132
|
|
|
$this->assertFalse($cookie->matchesRequest($request)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testMatchesRequestChecksSecureAttribute() |
136
|
|
|
{ |
137
|
|
|
$request = new Request('GET', 'https://example.com'); |
138
|
|
|
|
139
|
|
|
$cookie = new Cookie(); |
140
|
|
|
$cookie->setAttribute(Cookie::ATTR_DOMAIN, 'example.com'); |
141
|
|
|
$cookie->setAttribute(Cookie::ATTR_SECURE, null); |
142
|
|
|
|
143
|
|
|
$this->assertTrue($cookie->matchesRequest($request)); |
144
|
|
|
|
145
|
|
|
$request = new Request('GET', 'http://example.com'); |
146
|
|
|
$this->assertFalse($cookie->matchesRequest($request)); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|