Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit b084898

Browse files
committed
Fixed normalizing headers in HeadersConstraint (#16)
1 parent d86c2c9 commit b084898

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

src/PhpUnit/HeadersConstraint.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function matches($actualHeaders)
5757
foreach ($this->expectedHeadersSchemas as $name => $expectedSchema) {
5858
if (isset($actualHeaders[$name])) {
5959
$errors = $this->validator->validate(
60-
$this->normalizeHeaderValue($actualHeaders[$name]),
60+
$this->normalizeHeaderValue($actualHeaders[$name], $expectedSchema->type),
6161
$expectedSchema,
6262
$name
6363
);
@@ -110,18 +110,25 @@ private static function normalizeJsonSchema($schema)
110110
}
111111

112112
/**
113-
* Ensure header value is array.
113+
* The PSR-7 says that the header values MUST be an array of strings,
114+
* but OpenAPI allow scalar values. So if scalar value expected,
115+
* we try to cast array to scalar, using first element.
114116
*
115-
* @param mixed $value
117+
* @param array $value
118+
* @param string $type
116119
*
117120
* @return array
118121
*/
119-
private static function normalizeHeaderValue($value)
122+
private static function normalizeHeaderValue(array $value, $type)
120123
{
121-
if (is_string($value) && strpos($value, ',') !== false) {
122-
return preg_split('#\s*,\s*#', $value, -1, PREG_SPLIT_NO_EMPTY);
123-
} else {
124-
return $value;
124+
if ($type !== 'array') {
125+
$value = reset($value) ?: null;
126+
127+
if (is_numeric($value)) {
128+
$value += 0;
129+
}
125130
}
131+
132+
return $value;
126133
}
127134
}

tests/PhpUnit/HeadersConstraintTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ public function passUnexpectedValue()
6565
/**
6666
* @test
6767
*/
68-
public function AssertValidHeaders()
68+
public function assertValidHeaders()
6969
{
7070
$headers = [
71-
'Content-Type' => 'application/json',
72-
'Allow' => 'OPTIONS, HEAD',
73-
'X-Timestamp' => time(),
71+
'Content-Type' => ['application/json'],
72+
'Allow' => ['OPTIONS', 'HEAD'],
73+
'X-Timestamp' => [time()],
7474
];
7575

7676
$this->assertThat($headers, $this->constraint);
@@ -107,15 +107,15 @@ public function provideInvalidHeaders()
107107
'Missing required header' => [
108108
[],
109109
],
110-
'[Allow] String value found, but an array is required' => [
110+
'[Allow] There must be a minimum of 2 items in the array' => [
111111
[
112-
'Content-Type' => 'application/json',
113-
'Allow' => 'OPTIONS',
112+
'Content-Type' => ['application/json'],
113+
'Allow' => ['OPTIONS'],
114114
],
115115
],
116116
'[X-Timestamp] String value found, but an integer is required' => [
117117
[
118-
'X-Timestamp' => 'foo',
118+
'X-Timestamp' => ['foo'],
119119
],
120120
],
121121
];

0 commit comments

Comments
 (0)