Skip to content

Add request header accessors (à la PSR-7) #103

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 2 commits into from
Feb 10, 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
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,53 @@ $loop->run();

See also the [examples](examples).

## Usage

### Server

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

### Request

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

#### 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.

#### getHeader()

The `getHeader(string $name): string[]` method can be used to
retrieve a message header value by the given case-insensitive name.

Returns a list of all values for this header name or an empty array if header was not found

#### getHeaderLine()

The `getHeaderLine(string $name): string` method can be used to
retrieve a comma-separated string of the values for a single header.

Returns a comma-separated list of all values for this header name or an empty string if header was not found

#### hasHeader()

The `hasHeader(string $name): bool` method can be used to
check if a header exists by the given case-insensitive name.

### Response

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

## Install

The recommended way to install this library is [through Composer](http://getcomposer.org).
Expand Down
58 changes: 58 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,69 @@ public function getHttpVersion()
return $this->httpVersion;
}

/**
* 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.
*
* @return array
*/
public function getHeaders()
{
return $this->headers;
}

/**
* Retrieves a message header value by the given case-insensitive name.
*
* @param string $name
* @return string[] a list of all values for this header name or an empty array if header was not found
*/
public function getHeader($name)
{
$found = array();

$name = strtolower($name);
foreach ($this->headers as $key => $value) {
if (strtolower($key) === $name) {
foreach((array)$value as $one) {
$found[] = $one;
}
}
}

return $found;
}

/**
* Retrieves a comma-separated string of the values for a single header.
*
* @param string $name
* @return string a comma-separated list of all values for this header name or an empty string if header was not found
*/
public function getHeaderLine($name)
{
return implode(', ', $this->getHeader($name));
}

/**
* Checks if a header exists by the given case-insensitive name.
*
* @param string $name
* @return bool
*/
public function hasHeader($name)
{
return !!$this->getHeader($name);
}

public function expectsContinue()
{
return isset($this->headers['Expect']) && '100-continue' === $this->headers['Expect'];
Expand Down
34 changes: 34 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,38 @@ public function expectsContinueShouldBeTrueIfContinueExpected()

$this->assertTrue($request->expectsContinue());
}

public function testEmptyHeader()
{
$request = new Request('GET', '/');

$this->assertEquals(array(), $request->getHeaders());
$this->assertFalse($request->hasHeader('Test'));
$this->assertEquals(array(), $request->getHeader('Test'));
$this->assertEquals('', $request->getHeaderLine('Test'));
}

public function testHeaderIsCaseInsensitive()
{
$request = new Request('GET', '/', array(), '1.1', array(
'TEST' => 'Yes',
));

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

public function testHeaderWithMultipleValues()
{
$request = new Request('GET', '/', array(), '1.1', array(
'Test' => array('a', 'b'),
));

$this->assertEquals(array('Test' => array('a', 'b')), $request->getHeaders());
$this->assertTrue($request->hasHeader('Test'));
$this->assertEquals(array('a', 'b'), $request->getHeader('Test'));
$this->assertEquals('a, b', $request->getHeaderLine('Test'));
}
}