Skip to content

Commit cbb953c

Browse files
committed
fix: address reviewer feedback regarding HTTP test isolation
1 parent 9549787 commit cbb953c

5 files changed

Lines changed: 14 additions & 23 deletions

File tree

system/HTTP/IncomingRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public function detectLocale($config)
207207
public function negotiate(string $type, array $supported, bool $strictMatch = false): string
208208
{
209209
if ($this->negotiator === null) {
210-
$this->negotiator = Services::negotiator($this, false);
210+
$this->negotiator = Services::negotiator($this, true);
211211
}
212212

213213
return match (strtolower($type)) {

system/HTTP/MessageTrait.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function appendBody($data): self
8181
*/
8282
public function populateHeaders(): void
8383
{
84-
$contentType = service('superglobals')->server('CONTENT_TYPE');
84+
$contentType = service('superglobals')->server('CONTENT_TYPE', (string) getenv('CONTENT_TYPE'));
8585
if (! empty($contentType)) {
8686
$this->setHeader('Content-Type', $contentType);
8787
}
@@ -266,25 +266,18 @@ protected function getHeaderName(string $name): string
266266
*/
267267
public function setProtocolVersion(string $version): self
268268
{
269-
// If empty, keep default protocol version (usually 1.1) and do nothing.
270-
if ($version === '') {
271-
return $this;
272-
}
273-
274-
// If a full protocol string (e.g., "HTTP/1.1") is provided, extract the numeric part.
275-
if (str_contains($version, '/')) {
269+
if (! is_numeric($version)) {
276270
$version = substr($version, strpos($version, '/') + 1);
277271
}
278272

279-
// Normalize to a single decimal place as used in validProtocolVersions.
280-
$normalized = number_format((float) $version, 1);
273+
// Make sure that version is in the correct format
274+
$version = number_format((float) $version, 1);
281275

282-
// Throw exception if the version is not recognized.
283-
if (! in_array($normalized, $this->validProtocolVersions, true)) {
276+
if (! in_array($version, $this->validProtocolVersions, true)) {
284277
throw HTTPException::forInvalidHTTPProtocol($version);
285278
}
286279

287-
$this->protocolVersion = $normalized;
280+
$this->protocolVersion = $version;
288281

289282
return $this;
290283
}

system/HTTP/RedirectResponse.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ public function withCookies()
159159
*/
160160
public function withHeaders()
161161
{
162-
$source = service('response');
163-
164-
foreach ($source->headers() as $name => $value) {
162+
foreach (service('response')->headers() as $name => $value) {
165163
if ($value instanceof Header) {
166164
$this->setHeader($name, $value->getValue());
167165
} else {
@@ -171,11 +169,6 @@ public function withHeaders()
171169
}
172170
}
173171

174-
// Ensure source response remains empty after copying to satisfy tests that expect no residual headers.
175-
foreach (array_keys($source->headers()) as $key) {
176-
$source->removeHeader($key);
177-
}
178-
179172
return $this;
180173
}
181174
}

tests/system/HTTP/IncomingRequestTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ final class IncomingRequestTest extends CIUnitTestCase
4343
#[WithoutErrorHandler]
4444
protected function setUp(): void
4545
{
46+
$this->resetServices();
4647
parent::setUp();
4748

4849
$_ENV = $_SESSION = [];

tests/system/HTTP/MessageTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,11 @@ public function testPopulateHeadersWithoutContentType(): void
259259

260260
$this->assertNull($this->message->header('content-type'));
261261

262-
putenv("CONTENT_TYPE={$originalEnv}");
262+
if ($originalEnv !== false) {
263+
putenv("CONTENT_TYPE={$originalEnv}");
264+
} else {
265+
putenv('CONTENT_TYPE');
266+
}
263267
}
264268

265269
public function testPopulateHeadersWithoutHTTP(): void

0 commit comments

Comments
 (0)