Skip to content

Commit 904caa7

Browse files
committed
test: isolate bug #72
1 parent 5e12783 commit 904caa7

3 files changed

Lines changed: 89 additions & 4 deletions

File tree

test/phpunit/Header/HeadersTest.php

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ public function testAddMultiple() {
6262
public function testAddMultipleCommaHeader() {
6363
$headers = new Headers(self::HEADER_ARRAY);
6464
$headers->add(
65-
"Cookie-set",
65+
"Set-Cookie",
6666
"language=en; expires=Thu, 1-Jan-1970 00:00:00 UTC; path=/; domain=example.com",
6767
"id=123; expires=Thu, 1-Jan-1970 00:00:00 UTC; path=/; domain=example.com httponly"
6868
);
6969
$headerArray = $headers->asArray();
70-
$cookie = explode("\n", $headerArray["Cookie-set"]);
70+
$cookie = explode("\n", $headerArray["Set-Cookie"]);
7171
self::assertContains("language=en; expires=Thu, 1-Jan-1970 00:00:00 UTC; path=/; domain=example.com", $cookie);
7272
self::assertContains("id=123; expires=Thu, 1-Jan-1970 00:00:00 UTC; path=/; domain=example.com httponly", $cookie);
7373
}
@@ -115,15 +115,66 @@ public function testGetMultiple() {
115115
public function testGetMultipleCommas() {
116116
$headers = new Headers(self::HEADER_ARRAY);
117117
$headers->add(
118-
"Cookie-set",
118+
"Set-Cookie",
119119
"language=en; expires=Thu, 1-Jan-1970 00:00:00 UTC; path=/; domain=example.com",
120120
"id=123; expires=Thu, 1-Jan-1970 00:00:00 UTC; path=/; domain=example.com httponly"
121121
);
122122
self::assertEquals(
123123
"language=en; expires=Thu, 1-Jan-1970 00:00:00 UTC; path=/; domain=example.com"
124124
. "\n"
125125
. "id=123; expires=Thu, 1-Jan-1970 00:00:00 UTC; path=/; domain=example.com httponly",
126-
$headers->get("Cookie-set")
126+
$headers->get("Set-Cookie")
127+
);
128+
}
129+
130+
public function testAddMultipleSetCookieWithoutCommaPreservesSeparateLines():void {
131+
$headers = new Headers(self::HEADER_ARRAY);
132+
$headers->add("Set-Cookie", "language=en; Path=/");
133+
$headers->add("Set-Cookie", "id=123; HttpOnly");
134+
135+
self::assertCount(5, $headers);
136+
self::assertSame(
137+
"language=en; Path=/\nid=123; HttpOnly",
138+
$headers->asArray()["Set-Cookie"]
139+
);
140+
}
141+
142+
public function testGetAllAggregatesValuesAcrossSeparateHeaderLines():void {
143+
$firstValue = "Digest realm=\"example.com\", qop=\"auth\"";
144+
$secondValue = "Digest realm=\"api.example.com\", qop=\"auth-int\"";
145+
$headers = new Headers(self::HEADER_ARRAY);
146+
$headers->add("WWW-Authenticate", $firstValue);
147+
$headers->add("WWW-Authenticate", $secondValue);
148+
149+
self::assertSame(
150+
[$firstValue, $secondValue],
151+
$headers->getAll("WWW-Authenticate")
152+
);
153+
}
154+
155+
public function testAsArrayPreservesAllDuplicateSpecialCaseHeaderLines():void {
156+
$firstValue = "Digest realm=\"example.com\", qop=\"auth\"";
157+
$secondValue = "Digest realm=\"api.example.com\", qop=\"auth-int\"";
158+
$headers = new Headers(self::HEADER_ARRAY);
159+
$headers->add("WWW-Authenticate", $firstValue);
160+
$headers->add("WWW-Authenticate", $secondValue);
161+
162+
self::assertSame(
163+
$firstValue . "\n" . $secondValue,
164+
$headers->asArray()["WWW-Authenticate"]
165+
);
166+
}
167+
168+
public function testAsArrayNestedPreservesAllDuplicateHeaderLineValues():void {
169+
$firstValue = "Digest realm=\"example.com\", qop=\"auth\"";
170+
$secondValue = "Digest realm=\"api.example.com\", qop=\"auth-int\"";
171+
$headers = new Headers(self::HEADER_ARRAY);
172+
$headers->add("WWW-Authenticate", $firstValue);
173+
$headers->add("WWW-Authenticate", $secondValue);
174+
175+
self::assertSame(
176+
[$firstValue, $secondValue],
177+
$headers->asArray(true)["WWW-Authenticate"]
127178
);
128179
}
129180

test/phpunit/Header/ParserTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,18 @@ public function testGetKeyValuesResponse() {
7373
self::assertArrayHasKey("X-Test-For", $keyValues);
7474
self::assertEquals("PHP.Gt", $keyValues["X-Test-For"]);
7575
}
76+
77+
public function testGetKeyValuesCombinesRepeatedListValuedHeaders():void {
78+
$headers = <<<HEADERS
79+
HTTP/1.1 200 OK
80+
Accept: text/html
81+
Accept: application/json
82+
HEADERS;
83+
$parser = new Parser($headers);
84+
85+
self::assertSame(
86+
"text/html, application/json",
87+
$parser->getKeyValues()["Accept"]
88+
);
89+
}
7690
}

test/phpunit/RequestTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ public function testWithFormDataBodyAutomaticallySetsContentTypeHeader():void {
115115
);
116116
}
117117

118+
public function testGetHeaderReturnsAllValuesAcrossSeparateHeaderLines():void {
119+
$headers = new RequestHeaders();
120+
$headers->add("WWW-Authenticate", "Digest realm=\"example.com\", qop=\"auth\"");
121+
$headers->add("WWW-Authenticate", "Digest realm=\"api.example.com\", qop=\"auth-int\"");
122+
123+
$request = new Request(
124+
"GET",
125+
self::getUriMock("/"),
126+
$headers
127+
);
128+
129+
self::assertSame(
130+
[
131+
"Digest realm=\"example.com\", qop=\"auth\"",
132+
"Digest realm=\"api.example.com\", qop=\"auth-int\"",
133+
],
134+
$request->getHeader("WWW-Authenticate")
135+
);
136+
}
137+
118138
/** @return MockObject|Uri */
119139
protected function getUriMock(string $uriPath = ""):MockObject {
120140
$partPath = parse_url($uriPath, PHP_URL_PATH);

0 commit comments

Comments
 (0)