20
20
21
21
class PersistentTokenContextTest extends TestCase
22
22
{
23
- /**
24
- * @dataProvider safeMethods
25
- *
26
- * @param $method
27
- */
28
- public function testMatchingSkippedForSafeMethodRequests ($ method )
23
+ public static function unsafeMethods (): iterable
24
+ {
25
+ return [['POST ' ], ['PUT ' ], ['DELETE ' ], ['PATCH ' ], ['TRACE ' ], ['CONNECT ' ]];
26
+ }
27
+
28
+ public static function safeMethods (): iterable
29
+ {
30
+ return [['GET ' ], ['HEAD ' ], ['OPTIONS ' ]];
31
+ }
32
+
33
+ /** @dataProvider safeMethods */
34
+ public function test_ForSafeMethodRequests_TokenIsIgnored (string $ method )
29
35
{
30
36
$ this ->assertResponse ($ this ->guard (), $ this ->request ($ method ));
31
37
$ this ->assertResponse ($ this ->guard ($ this ->token ('foo ' , 'x ' )), $ this ->request ($ method ));
32
38
$ this ->assertResponse ($ this ->guard ($ this ->token ('foo ' , 'x ' )), $ this ->request ($ method , ['bar ' => 'y ' ]));
33
39
}
34
40
35
- /**
36
- * @dataProvider unsafeMethods
37
- *
38
- * @param $method
39
- */
40
- public function testMissingSessionToken_ThrowsException ($ method )
41
+ /** @dataProvider unsafeMethods */
42
+ public function test_MissingSessionToken_ThrowsException (string $ method )
41
43
{
42
44
$ guard = $ this ->guard ();
43
45
$ request = $ this ->request ($ method );
44
46
$ this ->expectException (Exception \TokenMismatchException::class);
45
47
$ guard ->process ($ request , $ this ->handler ());
46
48
}
47
49
48
- /**
49
- * @dataProvider unsafeMethods
50
- *
51
- * @param $method
52
- */
53
- public function testMatchingRequestToken_ReturnsResponse ($ method )
50
+ /** @dataProvider unsafeMethods */
51
+ public function test_MatchingRequestToken_ReturnsResponse (string $ method )
54
52
{
55
53
$ this ->assertResponse ($ this ->guard ($ this ->token ('foo ' , 'hash ' )), $ this ->request ($ method , ['foo ' => 'hash ' ]));
56
54
}
57
55
58
- /**
59
- * @dataProvider unsafeMethods
60
- *
61
- * @param $method
62
- */
63
- public function testRequestTokenHashMismatch_ThrowsException ($ method )
56
+ /** @dataProvider unsafeMethods */
57
+ public function test_RequestTokenHashMismatch_ThrowsException (string $ method )
64
58
{
65
59
$ guard = $ this ->guard ($ this ->token ('name ' , 'hash-0001 ' ));
66
60
$ request = $ this ->request ($ method , ['name ' => 'hash-foo ' ]);
67
61
$ this ->expectException (Exception \TokenMismatchException::class);
68
62
$ guard ->process ($ request , $ this ->handler ());
69
63
}
70
64
71
- /**
72
- * @dataProvider unsafeMethods
73
- *
74
- * @param $method
75
- */
76
- public function testRequestTokenKeyMismatch_ThrowsException ($ method )
65
+ /** @dataProvider unsafeMethods */
66
+ public function test_RequestTokenKeyMismatch_ThrowsException (string $ method )
77
67
{
78
68
$ guard = $ this ->guard ($ this ->token ('foo ' , 'hash-0001 ' ));
79
69
$ request = $ this ->request ($ method , ['bar ' => 'hash-0001 ' ]);
80
70
$ this ->expectException (Exception \TokenMismatchException::class);
81
71
$ guard ->process ($ request , $ this ->handler ());
82
72
}
83
73
84
- public function testSessionTokenIsClearedOnTokenMismatch ()
74
+ public function test_OnTokenMismatch_SessionTokenIsCleared ()
85
75
{
86
76
$ token = $ this ->token ('foo ' , 'bar ' );
87
77
$ session = new Doubles \FakeSessionStorage ($ token + ['other_data ' => 'baz ' ]);
@@ -96,21 +86,17 @@ public function testSessionTokenIsClearedOnTokenMismatch()
96
86
}
97
87
}
98
88
99
- public function testSessionTokenIsPreservedForValidRequest ()
89
+ public function test_ForValidRequest_SessionTokenIsPreserved ()
100
90
{
101
91
$ token = $ this ->token ('foo ' , 'bar ' );
102
92
$ session = new Doubles \FakeSessionStorage ($ token );
103
93
$ guard = new PersistentTokenContext ($ session );
104
94
$ request = $ this ->request ('POST ' , ['foo ' => 'bar ' ]);
105
95
$ guard ->process ($ request , $ this ->handler ());
106
96
$ this ->assertTrue ($ session ->tokenExists ($ token ));
107
-
108
- $ request = $ this ->request ('GET ' );
109
- $ guard ->process ($ request , $ this ->handler ());
110
- $ this ->assertTrue ($ session ->tokenExists ($ token ));
111
97
}
112
98
113
- public function testGenerateTokenGeneratesTokenOnce ()
99
+ public function test_Token_IsGeneratedOnce ()
114
100
{
115
101
$ guard = $ this ->guard ($ this ->token ('name ' , 'hash ' ));
116
102
$ token = $ guard ->appSignature ();
@@ -121,7 +107,7 @@ public function testGenerateTokenGeneratesTokenOnce()
121
107
$ this ->assertSame ($ token , $ guard ->appSignature ());
122
108
}
123
109
124
- public function testResetTokenRemovesToken ()
110
+ public function test_ResetToken_RemovesToken ()
125
111
{
126
112
$ guard = $ this ->guard ();
127
113
$ token = $ guard ->appSignature ();
@@ -133,20 +119,9 @@ public function testResetTokenRemovesToken()
133
119
$ this ->assertNotEquals ($ token , $ newToken );
134
120
}
135
121
136
- public function unsafeMethods (): array
137
- {
138
- return [['POST ' ], ['PUT ' ], ['DELETE ' ], ['PATCH ' ], ['TRACE ' ], ['CONNECT ' ]];
139
- }
140
-
141
- public function safeMethods (): array
142
- {
143
- return [['GET ' ], ['HEAD ' ], ['OPTIONS ' ]];
144
- }
145
-
146
122
private function assertResponse (PersistentTokenContext $ guard , Doubles \FakeServerRequest $ request )
147
123
{
148
- $ handler = new Doubles \FakeRequestHandler (new Doubles \DummyResponse ());
149
- $ this ->assertInstanceOf (ResponseInterface::class, $ guard ->process ($ request , $ handler ));
124
+ $ this ->assertInstanceOf (ResponseInterface::class, $ guard ->process ($ request , $ this ->handler ()));
150
125
}
151
126
152
127
private function guard (array $ token = []): PersistentTokenContext
0 commit comments