Skip to content
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

cleanup assertions from #250 #272

Merged
merged 1 commit into from Jun 6, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/MessageTrait.php
Expand Up @@ -138,6 +138,11 @@ private function setHeaders(array $headers)
{
$this->headerNames = $this->headers = [];
foreach ($headers as $header => $value) {
if (is_int($header)) {
// Numeric array keys are converted to int by PHP but having a header name '123' is not forbidden by the spec
// and also allowed in withHeader(). So we need to cast it to string again for the following assertion to pass.
$header = (string) $header;
}
$this->assertHeader($header);
$value = $this->normalizeHeaderValue($value);
$normalized = strtolower($header);
Expand All @@ -154,10 +159,6 @@ private function setHeaders(array $headers)
private function normalizeHeaderValue($value)
{
if (!is_array($value)) {
if (!is_string($value)) {
throw new \InvalidArgumentException('Header value must be a string or an array of strings.');
}

return $this->trimHeaderValues([$value]);
}

Expand Down Expand Up @@ -185,17 +186,28 @@ private function normalizeHeaderValue($value)
private function trimHeaderValues(array $values)
{
return array_map(function ($value) {
if (!is_string($value)) {
throw new \InvalidArgumentException('Header value must be a string.');
if (!is_string($value) && !is_numeric($value)) {
throw new \InvalidArgumentException(sprintf(
'Header value must be a string or numeric but %s provided.',
is_object($value) ? get_class($value) : gettype($value)
));
}
return trim($value, " \t");

return trim((string) $value, " \t");
}, $values);
}

private function assertHeader($header)
{
if (!is_string($header) || $header === '') {
throw new \InvalidArgumentException('Header must be a non empty string.');
if (!is_string($header)) {
throw new \InvalidArgumentException(sprintf(
'Header name must be a string but %s provided.',
is_object($header) ? get_class($header) : gettype($header)
));
}

if ($header === '') {
throw new \InvalidArgumentException('Header name can not be empty.');
}
}
}
2 changes: 1 addition & 1 deletion src/Request.php
Expand Up @@ -145,7 +145,7 @@ private function updateHostFromUri()
private function assertMethod($method)
{
if (!is_string($method) || $method === '') {
throw new \InvalidArgumentException('Method should be a non empty string.');
throw new \InvalidArgumentException('Method must be a non-empty string.');
}
}
}
2 changes: 1 addition & 1 deletion src/Response.php
Expand Up @@ -97,7 +97,7 @@ public function __construct(
$status = (int) $status;
$this->assertStatusCodeRange($status);

$this->statusCode = (int) $status;
$this->statusCode = $status;

if ($body !== '' && $body !== null) {
$this->stream = stream_for($body);
Expand Down
29 changes: 14 additions & 15 deletions tests/ResponseTest.php
Expand Up @@ -237,6 +237,12 @@ public function testSameInstanceWhenRemovingMissingHeader()
$this->assertSame($r, $r->withoutHeader('foo'));
}

public function testPassNumericHeaderNameInConstructor()
{
$r = new Response(200, ['Location' => 'foo', '123' => 'bar']);
$this->assertSame('bar', $r->getHeaderLine('123'));
}

/**
* @dataProvider invalidHeaderProvider
*/
Expand All @@ -250,11 +256,9 @@ public function invalidHeaderProvider()
{
return [
['foo', [], 'Header value can not be an empty array.'],
['', '', 'Header must be a non empty string.'],
['foo', false, 'Header value must be a string or an array of strings.'],
[false, 'foo', 'Header must be a non empty string.'],
['foo', new \stdClass(), 'Header value must be a string or an array of strings.'],
['foo', new \ArrayObject(), 'Header value must be a string or an array of strings.'],
['', '', 'Header name can not be empty.'],
['foo', false, 'Header value must be a string or numeric but boolean provided'],
['foo', new \stdClass(), 'Header value must be a string or numeric but stdClass provided.'],
];
}

Expand All @@ -270,16 +274,11 @@ public function testWithInvalidHeader($header, $headerValue, $expectedMessage)

public function invalidWithHeaderProvider()
{
return [
[[], 'foo', 'Header must be a non empty string.'],
['foo', [], 'Header value can not be an empty array.'],
['', '', 'Header must be a non empty string.'],
['foo', false, 'Header value must be a string or an array of strings.'],
[false, 'foo', 'Header must be a non empty string.'],
['foo', new \stdClass(), 'Header value must be a string or an array of strings.'],
['foo', new \ArrayObject(), 'Header value must be a string or an array of strings.'],
[new \stdClass(), 'foo', 'Header must be a non empty string.'],
];
return array_merge($this->invalidHeaderProvider(), [
[[], 'foo', 'Header name must be a string but array provided.'],
[false, 'foo', 'Header name must be a string but boolean provided.'],
[new \stdClass(), 'foo', 'Header name must be a string but stdClass provided.'],
]);
}

public function testHeaderValuesAreTrimmed()
Expand Down