Skip to content

Change Request methods to be in line with PSR-7 #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,34 @@ It implements the `ReadableStreamInterface`.

See the above usage example and the class outline for details.

#### getMethod()

The `getMethod(): string` method can be used to
return the request method.

#### getPath()

The `getPath(): string` method can be used to
return the request path.

#### getQueryParams()

The `getQueryParams(): array` method can be used to
return an array with all query parameters ($_GET).

#### getProtocolVersion()

The `getProtocolVersion(): string` method can be used to
return the HTTP protocol version (such as "1.0" or "1.1").

#### getHeaders()

The `getHeaders(): array` method can be used to
return an array with ALL headers.

The keys represent the header name in the exact case in which they were
originally specified. The values will be a string if there's only a single
value for the respective header name or an array of strings if this header
has multiple values.

> Note that this differs from the PSR-7 implementation of this method,
which always returns an array for each header name, even if it only has a
single value.
originally specified. The values will be an array of strings for each
value for the respective header name.

#### getHeader()

Expand Down
33 changes: 24 additions & 9 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,42 @@ public function __construct($method, $path, $query = array(), $httpVersion = '1.
$this->headers = $headers;
}

/**
* Returns the request method
*
* @return string
*/
public function getMethod()
{
return $this->method;
}

/**
* Returns the request path
*
* @return string
*/
public function getPath()
{
return $this->path;
}

public function getQuery()
/**
* Returns an array with all query parameters ($_GET)
*
* @return array
*/
public function getQueryParams()
{
return $this->query;
}

public function getHttpVersion()
/**
* Returns the HTTP protocol version (such as "1.0" or "1.1")
*
* @return string
*/
public function getProtocolVersion()
{
return $this->httpVersion;
}
Expand All @@ -52,13 +72,8 @@ public function getHttpVersion()
* Returns an array with ALL headers
*
* The keys represent the header name in the exact case in which they were
* originally specified. The values will be a string if there's only a single
* value for the respective header name or an array of strings if this header
* has multiple values.
*
* Note that this differs from the PSR-7 implementation of this method,
* which always returns an array for each header name, even if it only has a
* single value.
* originally specified. The values will be an array of strings for each
* value for the respective header name.
*
* @return array
*/
Expand Down
10 changes: 1 addition & 9 deletions src/RequestHeaderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,12 @@ public function parseRequest($data)
parse_str($queryString, $parsedQuery);
}

$headers = array_map(function($val) {
if (1 === count($val)) {
$val = $val[0];
}

return $val;
}, $psrRequest->getHeaders());

$request = new Request(
$psrRequest->getMethod(),
$psrRequest->getUri()->getPath(),
$parsedQuery,
$psrRequest->getProtocolVersion(),
$headers
$psrRequest->getHeaders()
);

return array($request, $bodyBuffer);
Expand Down
22 changes: 11 additions & 11 deletions tests/RequestHeaderParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function testHeadersEventShouldReturnRequestAndBodyBuffer()
$this->assertInstanceOf('React\Http\Request', $request);
$this->assertSame('GET', $request->getMethod());
$this->assertSame('/', $request->getPath());
$this->assertSame(array(), $request->getQuery());
$this->assertSame('1.1', $request->getHttpVersion());
$this->assertSame(array('Host' => 'example.com:80', 'Connection' => 'close'), $request->getHeaders());
$this->assertSame(array(), $request->getQueryParams());
$this->assertSame('1.1', $request->getProtocolVersion());
$this->assertSame(array('Host' => array('example.com:80'), 'Connection' => array('close')), $request->getHeaders());

$this->assertSame('RANDOM DATA', $bodyBuffer);
}
Expand Down Expand Up @@ -86,12 +86,12 @@ public function testHeadersEventShouldParsePathAndQueryString()
$this->assertInstanceOf('React\Http\Request', $request);
$this->assertSame('POST', $request->getMethod());
$this->assertSame('/foo', $request->getPath());
$this->assertSame(array('bar' => 'baz'), $request->getQuery());
$this->assertSame('1.1', $request->getHttpVersion());
$this->assertSame(array('bar' => 'baz'), $request->getQueryParams());
$this->assertSame('1.1', $request->getProtocolVersion());
$headers = array(
'Host' => 'example.com:80',
'User-Agent' => 'react/alpha',
'Connection' => 'close',
'Host' => array('example.com:80'),
'User-Agent' => array('react/alpha'),
'Connection' => array('close'),
);
$this->assertSame($headers, $request->getHeaders());
}
Expand Down Expand Up @@ -139,9 +139,9 @@ public function testHeaderOverflowShouldNotEmitErrorWhenDataExceedsMaxHeaderSize
$parser->feed($data);

$headers = array(
'Host' => 'example.com:80',
'User-Agent' => 'react/alpha',
'Connection' => 'close',
'Host' => array('example.com:80'),
'User-Agent' => array('react/alpha'),
'Connection' => array('close'),
);
$this->assertSame($headers, $request->getHeaders());

Expand Down
10 changes: 5 additions & 5 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function expectsContinueShouldBeFalseByDefault()
/** @test */
public function expectsContinueShouldBeTrueIfContinueExpected()
{
$headers = array('Expect' => '100-continue');
$headers = array('Expect' => array('100-continue'));
$request = new Request('GET', '/', array(), '1.1', $headers);

$this->assertTrue($request->expectsContinue());
Expand All @@ -27,7 +27,7 @@ public function expectsContinueShouldBeTrueIfContinueExpected()
/** @test */
public function expectsContinueShouldBeTrueIfContinueExpectedCaseInsensitive()
{
$headers = array('EXPECT' => '100-CONTINUE');
$headers = array('EXPECT' => array('100-CONTINUE'));
$request = new Request('GET', '/', array(), '1.1', $headers);

$this->assertTrue($request->expectsContinue());
Expand All @@ -36,7 +36,7 @@ public function expectsContinueShouldBeTrueIfContinueExpectedCaseInsensitive()
/** @test */
public function expectsContinueShouldBeFalseForHttp10()
{
$headers = array('Expect' => '100-continue');
$headers = array('Expect' => array('100-continue'));
$request = new Request('GET', '/', array(), '1.0', $headers);

$this->assertFalse($request->expectsContinue());
Expand All @@ -55,10 +55,10 @@ public function testEmptyHeader()
public function testHeaderIsCaseInsensitive()
{
$request = new Request('GET', '/', array(), '1.1', array(
'TEST' => 'Yes',
'TEST' => array('Yes'),
));

$this->assertEquals(array('TEST' => 'Yes'), $request->getHeaders());
$this->assertEquals(array('TEST' => array('Yes')), $request->getHeaders());
$this->assertTrue($request->hasHeader('Test'));
$this->assertEquals(array('Yes'), $request->getHeader('Test'));
$this->assertEquals('Yes', $request->getHeaderLine('Test'));
Expand Down