Skip to content

Commit ed421fd

Browse files
committed
Parse cookie for request object
1 parent 2d1f10a commit ed421fd

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

src/RequestHeaderParser.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ private function parseRequest($data)
118118
);
119119
$request = $request->withRequestTarget($target);
120120

121+
$cookies = ServerRequest::parseCookie($request->getHeaderLine('Cookie'));
122+
if ($cookies !== false) {
123+
$request = $request->withCookieParams($cookies);
124+
}
125+
121126
// re-apply actual request target from above
122127
if ($originalTarget !== null) {
123128
$uri = $request->getUri()->withPath('');

src/ServerRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public static function parseCookie($cookie)
132132
return false;
133133
}
134134

135-
$cookieArray = explode(';', $cookie);
135+
$cookieArray = explode('; ', $cookie);
136136
$result = array();
137137

138138
foreach ($cookieArray as $pair) {

tests/ServerRequestTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function testParseSingleCookieNameValuePairWillReturnValidArray()
115115

116116
public function testParseMultipleCookieNameValuePaiWillReturnValidArray()
117117
{
118-
$cookieString = 'hello=world;test=abc';
118+
$cookieString = 'hello=world; test=abc';
119119
$cookies = ServerRequest::parseCookie($cookieString);
120120
$this->assertEquals(array('hello' => 'world', 'test' => 'abc'), $cookies);
121121
}
@@ -136,7 +136,7 @@ public function testParseMultipleCookieNameValuePairWillReturnFalse()
136136

137137
public function testOnlyFirstSetWillBeAddedToCookiesArray()
138138
{
139-
$cookieString = 'hello=world;hello=abc';
139+
$cookieString = 'hello=world; hello=abc';
140140
$cookies = ServerRequest::parseCookie($cookieString);
141141
$this->assertEquals(array('hello' => 'abc'), $cookies);
142142
}
@@ -157,21 +157,21 @@ public function testSingleCookieValueInCookiesReturnsEmptyArray()
157157

158158
public function testSingleMutlipleCookieValuesReturnsEmptyArray()
159159
{
160-
$cookieString = 'world;test';
160+
$cookieString = 'world; test';
161161
$cookies = ServerRequest::parseCookie($cookieString);
162162
$this->assertEquals(array(), $cookies);
163163
}
164164

165165
public function testSingleValueIsValidInMultipleValueCookieWillReturnValidArray()
166166
{
167-
$cookieString = 'world;test=php';
167+
$cookieString = 'world; test=php';
168168
$cookies = ServerRequest::parseCookie($cookieString);
169169
$this->assertEquals(array('test' => 'php'), $cookies);
170170
}
171171

172172
public function testUrlEncodingForValueWillReturnValidArray()
173173
{
174-
$cookieString = 'hello=world%21;test=100%25%20coverage';
174+
$cookieString = 'hello=world%21; test=100%25%20coverage';
175175
$cookies = ServerRequest::parseCookie($cookieString);
176176
$this->assertEquals(array('hello' => 'world!', 'test' => '100% coverage'), $cookies);
177177
}

tests/ServerTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,6 +2369,68 @@ public function testServerRequestParams()
23692369
$this->assertNotNull($serverParams['REQUEST_TIME_FLOAT']);
23702370
}
23712371

2372+
public function testCookieWillBeAddedToServerRequest()
2373+
{
2374+
$requestValidation = null;
2375+
$server = new Server($this->socket, function (ServerRequestInterface $request) use (&$requestValidation) {
2376+
$requestValidation = $request;
2377+
return new Response();
2378+
});
2379+
2380+
$this->socket->emit('connection', array($this->connection));
2381+
2382+
$data = "GET / HTTP/1.1\r\n";
2383+
$data .= "Host: example.com:80\r\n";
2384+
$data .= "Connection: close\r\n";
2385+
$data .= "Cookie: hello=world\r\n";
2386+
$data .= "\r\n";
2387+
2388+
$this->connection->emit('data', array($data));
2389+
2390+
$this->assertEquals(array('hello' => 'world'), $requestValidation->getCookieParams());
2391+
}
2392+
2393+
public function testMultipleCookiesWontBeAddedToServerRequest()
2394+
{
2395+
$requestValidation = null;
2396+
$server = new Server($this->socket, function (ServerRequestInterface $request) use (&$requestValidation) {
2397+
$requestValidation = $request;
2398+
return new Response();
2399+
});
2400+
2401+
$this->socket->emit('connection', array($this->connection));
2402+
2403+
$data = "GET / HTTP/1.1\r\n";
2404+
$data .= "Host: example.com:80\r\n";
2405+
$data .= "Connection: close\r\n";
2406+
$data .= "Cookie: hello=world\r\n";
2407+
$data .= "Cookie: test=failed\r\n";
2408+
$data .= "\r\n";
2409+
2410+
$this->connection->emit('data', array($data));
2411+
$this->assertEquals(array(), $requestValidation->getCookieParams());
2412+
}
2413+
2414+
public function testCookieWithSepeartorWillBeAddedToServerRequest()
2415+
{
2416+
$requestValidation = null;
2417+
$server = new Server($this->socket, function (ServerRequestInterface $request) use (&$requestValidation) {
2418+
$requestValidation = $request;
2419+
return new Response();
2420+
});
2421+
2422+
$this->socket->emit('connection', array($this->connection));
2423+
2424+
$data = "GET / HTTP/1.1\r\n";
2425+
$data .= "Host: example.com:80\r\n";
2426+
$data .= "Connection: close\r\n";
2427+
$data .= "Cookie: hello=world; test=abc\r\n";
2428+
$data .= "\r\n";
2429+
2430+
$this->connection->emit('data', array($data));
2431+
$this->assertEquals(array('hello' => 'world', 'test' => 'abc'), $requestValidation->getCookieParams());
2432+
}
2433+
23722434
private function createGetRequest()
23732435
{
23742436
$data = "GET / HTTP/1.1\r\n";

0 commit comments

Comments
 (0)