Skip to content

Commit

Permalink
Add request header accessors (à la PSR-7)
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Feb 9, 2017
1 parent 1fa41f8 commit 490775b
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,48 @@ $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 ALL headers.

This will return an (possibly empty) assoc array with header names as
key and header values as value. The header value will be a string if
there's only a single value or an array of strings if this header has
multiple values.

Note that this differs from the PSR-7 implementation of this method.

#### 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.
56 changes: 56 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,67 @@ public function getHttpVersion()
return $this->httpVersion;
}

/**
* Returns ALL headers
*
* This will return an (possibly empty) assoc array with header names as
* key and header values as value. The header value will be a string if
* there's only a single value or an array of strings if this header has
* multiple values.
*
* Note that this differs from the PSR-7 implementation of this method.
*
* @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'));
}
}

0 comments on commit 490775b

Please sign in to comment.