Skip to content

Commit 5085060

Browse files
committed
Merge branch 'master' of github.com:php-vcr/php-vcr into 1.5
1 parent 1e9b539 commit 5085060

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

src/VCR/Request.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,14 @@ public function getHeaders(): array
171171

172172
/**
173173
* @param string $key
174-
* @return string
174+
* @return string|null
175175
*/
176-
public function getHeader(string $key): string
176+
public function getHeader(string $key): ?string
177177
{
178+
if (!isset($this->headers[$key])) {
179+
return null;
180+
}
181+
178182
return $this->headers[$key];
179183
}
180184

src/VCR/Util/HttpUtil.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,22 @@ class HttpUtil
1414
*/
1515
public static function parseHeaders(array $headers): array
1616
{
17-
$headerGroups = array();
18-
$headerList = array();
19-
2017
// Collect matching headers into groups
21-
foreach ($headers as $line) {
18+
foreach ($headers as $i => $line) {
2219
list($key, $value) = explode(': ', $line, 2);
23-
if (!isset($headerGroups[$key])) {
24-
$headerGroups[$key] = array();
20+
if (isset($headers[$key])) {
21+
if (is_array($headers[$key])) {
22+
$headers[$key][] = $value;
23+
} else {
24+
$headers[$key] = array($headers[$key], $value);
25+
}
26+
} else {
27+
$headers[$key] = $value;
2528
}
26-
$headerGroups[$key][] = $value;
27-
}
28-
29-
// Collapse groups
30-
foreach ($headerGroups as $key => $values) {
31-
$headerList[$key] = implode(', ', $values);
29+
unset($headers[$i]);
3230
}
3331

34-
return $headerList;
32+
return $headers;
3533
}
3634

3735
/**
@@ -66,7 +64,7 @@ public static function parseStatus(string $status): array
6664
public static function parseResponse(string $response): array
6765
{
6866
$response = str_replace("HTTP/1.1 100 Continue\r\n\r\n", '', $response);
69-
67+
7068
list($rawHeader, $rawBody) = explode("\r\n\r\n", $response, 2);
7169

7270
// Parse headers and status.
@@ -90,15 +88,21 @@ public static function parseRawHeader(string $rawHeader): array
9088
/**
9189
* Returns a list of headers from a key/value paired array.
9290
*
93-
* @param array<string,string> $headers Headers as key/value pairs.
91+
* @param array<string,string|array<string,string>> $headers Headers as key/value pairs.
9492
* @return string[] List of headers ['Content-Type: text/html', '...'].
9593
*/
9694
public static function formatHeadersForCurl(array $headers): array
9795
{
9896
$curlHeaders = array();
9997

100-
foreach ($headers as $key => $value) {
101-
$curlHeaders[] = $key . ': ' . $value;
98+
foreach ($headers as $key => $values) {
99+
if (is_array($values)) {
100+
foreach ($values as $value) {
101+
$curlHeaders[] = $key . ': ' . $value;
102+
}
103+
} else {
104+
$curlHeaders[] = $key . ': ' . $values;
105+
}
102106
}
103107

104108
return $curlHeaders;

tests/VCR/Util/HttpUtilTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function testParseHeadersMultiple()
124124
$excpetedHeaders = array(
125125
'Content-Type' => 'text/html',
126126
'Date' => 'Fri, 19 Jun 2015 16:05:18 GMT',
127-
'Vary' => 'Accept, Accept-Language, Expect, Accept-Encoding',
127+
'Vary' => array('Accept, Accept-Language, Expect', 'Accept-Encoding'),
128128
'Content-Length' => '0'
129129
);
130130
$outputArray = HttpUtil::parseHeaders($inputArray);

0 commit comments

Comments
 (0)