33namespace UMA \RedisSessions \Tests \E2E ;
44
55use GuzzleHttp \Psr7 \Request ;
6+ use GuzzleHttp \Psr7 \Response ;
67use UMA \SavePathParser ;
78
89class BasicTest extends EndToEndTestCase
@@ -22,8 +23,8 @@ public function testAnonymousRequest()
2223 );
2324
2425 $ this ->assertSame ('1 ' , (string ) $ response ->getBody ());
25- $ this -> assertTrue ( $ response -> hasHeader ( ' Set-Cookie ' ));
26- $ this ->assertStringStartsWith ( ' PHPSESSID= ' , $ response-> getHeaderLine ( ' Set-Cookie ' ) );
26+
27+ $ this ->assertCreatedNewSession ( $ response );
2728
2829 $ this ->assertSame (1 , $ this ->redis ->dbSize ());
2930 }
@@ -49,10 +50,9 @@ public function testUnrelatedAnonymousRequests()
4950
5051 $ this ->assertSame ('1 ' , (string ) $ firstResponse ->getBody ());
5152 $ this ->assertSame ('1 ' , (string ) $ secondResponse ->getBody ());
52- $ this ->assertTrue ($ firstResponse ->hasHeader ('Set-Cookie ' ));
53- $ this ->assertTrue ($ secondResponse ->hasHeader ('Set-Cookie ' ));
54- $ this ->assertStringStartsWith ('PHPSESSID= ' , $ firstResponse ->getHeaderLine ('Set-Cookie ' ));
55- $ this ->assertStringStartsWith ('PHPSESSID= ' , $ secondResponse ->getHeaderLine ('Set-Cookie ' ));
53+
54+ $ this ->assertCreatedNewSession ($ firstResponse );
55+ $ this ->assertCreatedNewSession ($ secondResponse );
5656 $ this ->assertNotSame ($ firstResponse ->getHeaderLine ('Set-Cookie ' ), $ secondResponse ->getHeaderLine ('Set-Cookie ' ));
5757
5858 $ this ->assertSame (2 , $ this ->redis ->dbSize ());
@@ -79,9 +79,9 @@ public function testRelatedRequests()
7979
8080 $ this ->assertSame ('1 ' , (string ) $ firstResponse ->getBody ());
8181 $ this ->assertSame ('2 ' , (string ) $ secondResponse ->getBody ());
82- $ this ->assertTrue ($ firstResponse ->hasHeader ('Set-Cookie ' ));
82+
83+ $ this ->assertCreatedNewSession ($ firstResponse );
8384 $ this ->assertFalse ($ secondResponse ->hasHeader ('Set-Cookie ' ));
84- $ this ->assertStringStartsWith ('PHPSESSID= ' , $ firstResponse ->getHeaderLine ('Set-Cookie ' ));
8585
8686 $ this ->assertSame (1 , $ this ->redis ->dbSize ());
8787 }
@@ -101,10 +101,51 @@ public function testMaliciousRequest()
101101 );
102102
103103 $ this ->assertSame ('1 ' , (string ) $ response ->getBody ());
104- $ this -> assertTrue ( $ response -> hasHeader ( ' Set-Cookie ' ));
105- $ this ->assertStringStartsWith ( ' PHPSESSID= ' , $ response-> getHeaderLine ( ' Set-Cookie ' ) );
104+
105+ $ this ->assertCreatedNewSession ( $ response );
106106
107107 $ this ->assertSame (1 , $ this ->redis ->dbSize ());
108108 $ this ->assertFalse ($ this ->redis ->get (SavePathParser::DEFAULT_PREFIX .'madeupkey ' ));
109109 }
110+
111+ /**
112+ * This test checks that the server behaves correctly when a previously valid
113+ * session ID is removed from Redis in between requests.
114+ *
115+ * @see https://github.com/1ma/RedisSessionHandler/issues/3
116+ */
117+ public function testFlushedDatabase ()
118+ {
119+ $ firstResponse = $ this ->http ->send (
120+ new Request ('GET ' , '/visit-counter.php ' )
121+ );
122+
123+ $ this ->assertSame ('1 ' , (string ) $ firstResponse ->getBody ());
124+ $ this ->assertCreatedNewSession ($ firstResponse );
125+
126+ $ this ->redis ->flushAll ();
127+
128+ $ this ->assertSame (0 , $ this ->redis ->dbSize ());
129+
130+ $ secondResponse = $ this ->http ->send (
131+ new Request ('GET ' , '/visit-counter.php ' , $ this ->prepareSessionHeader ($ firstResponse ))
132+ );
133+
134+ $ this ->assertSame ('1 ' , (string ) $ secondResponse ->getBody ());
135+ $ this ->assertCreatedNewSession ($ secondResponse );
136+
137+ $ this ->assertSame (1 , $ this ->redis ->dbSize ());
138+ }
139+
140+ /**
141+ * Asserts whether a received request triggered the creation of a new session.
142+ * It does so by searching for and inspecting the 'Set-Cookie' header.
143+ *
144+ * @param Response $response
145+ */
146+ protected function assertCreatedNewSession (Response $ response )
147+ {
148+ $ this ->assertTrue ($ response ->hasHeader ('Set-Cookie ' ));
149+ $ this ->assertStringStartsWith ('PHPSESSID= ' , $ response ->getHeaderLine ('Set-Cookie ' ));
150+ }
110151}
0 commit comments