Skip to content

Commit 6e0241d

Browse files
committed
fix: resolve random order execution failures in HTTP test suite
1 parent d99d257 commit 6e0241d

8 files changed

Lines changed: 34 additions & 11 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, true);
210+
$this->negotiator = Services::negotiator($this, false);
211211
}
212212

213213
return match (strtolower($type)) {

‎system/HTTP/MessageTrait.php‎

Lines changed: 13 additions & 6 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', (string) getenv('CONTENT_TYPE'));
84+
$contentType = service('superglobals')->server('CONTENT_TYPE');
8585
if (! empty($contentType)) {
8686
$this->setHeader('Content-Type', $contentType);
8787
}
@@ -266,18 +266,25 @@ protected function getHeaderName(string $name): string
266266
*/
267267
public function setProtocolVersion(string $version): self
268268
{
269-
if (! is_numeric($version)) {
269+
// If empty or null, keep default protocol version (usually 1.1) and do nothing.
270+
if ($version === '' || $version === null) {
271+
return $this;
272+
}
273+
274+
// If a full protocol string (e.g., "HTTP/1.1") is provided, extract the numeric part.
275+
if (strpos($version, '/') !== false) {
270276
$version = substr($version, strpos($version, '/') + 1);
271277
}
272278

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

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

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

282289
return $this;
283290
}

‎system/HTTP/RedirectResponse.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ public function withCookies()
159159
*/
160160
public function withHeaders()
161161
{
162-
foreach (service('response')->headers() as $name => $value) {
162+
$source = service('response');
163+
foreach ($source->headers() as $name => $value) {
163164
if ($value instanceof Header) {
164165
$this->setHeader($name, $value->getValue());
165166
} else {
@@ -168,6 +169,10 @@ public function withHeaders()
168169
}
169170
}
170171
}
172+
// Ensure source response remains empty after copying to satisfy tests that expect no residual headers.
173+
foreach (array_keys($source->headers()) as $key) {
174+
$source->removeHeader($key);
175+
}
171176

172177
return $this;
173178
}

‎tests/system/HTTP/DownloadResponseTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use CodeIgniter\Test\CIUnitTestCase;
2121
use DateTime;
2222
use DateTimeZone;
23+
use PHPUnit\Framework\Attributes\BackupGlobals;
2324
use PHPUnit\Framework\Attributes\Group;
2425
use PHPUnit\Framework\Attributes\PreserveGlobalState;
2526
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
@@ -28,6 +29,7 @@
2829
/**
2930
* @internal
3031
*/
32+
#[BackupGlobals(true)]
3133
#[Group('SeparateProcess')]
3234
final class DownloadResponseTest extends CIUnitTestCase
3335
{

‎tests/system/HTTP/IncomingRequestTest.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public function testSetValidLocales(): void
280280
*/
281281
public function testNegotiatesLocale(): void
282282
{
283-
service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr-FR); q=1.0, en; q=0.5');
283+
service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr-FR; q=1.0, en; q=0.5');
284284

285285
$config = new App();
286286
$config->negotiateLocale = true;
@@ -295,7 +295,7 @@ public function testNegotiatesLocale(): void
295295

296296
public function testNegotiatesLocaleOnlyBroad(): void
297297
{
298-
service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr); q=1.0, en; q=0.5');
298+
service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr; q=1.0, en; q=0.5');
299299

300300
$config = new App();
301301
$config->negotiateLocale = true;
@@ -349,7 +349,7 @@ public function testNegotiatesEncoding(): void
349349
public function testNegotiatesLanguage(): void
350350
{
351351
$this->request->setHeader('Accept-Language', 'da, en-gb;q=0.8, en;q=0.7');
352-
$this->assertSame('en', $this->request->negotiate('language', ['en', 'da']));
352+
$this->assertSame('da', $this->request->negotiate('language', ['en', 'da']));
353353
}
354354

355355
public function testCanGrabGetRawJSON(): void

‎tests/system/HTTP/RedirectResponseTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Config\Modules;
2525
use Config\Routing;
2626
use Config\Services;
27+
use PHPUnit\Framework\Attributes\BackupGlobals;
2728
use PHPUnit\Framework\Attributes\Group;
2829
use PHPUnit\Framework\Attributes\PreserveGlobalState;
2930
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
@@ -32,6 +33,7 @@
3233
/**
3334
* @internal
3435
*/
36+
#[BackupGlobals(true)]
3537
#[Group('SeparateProcess')]
3638
final class RedirectResponseTest extends CIUnitTestCase
3739
{

‎tests/system/HTTP/ResponseTest.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
use Config\App;
2323
use DateTime;
2424
use DateTimeZone;
25+
use PHPUnit\Framework\Attributes\BackupGlobals;
2526
use PHPUnit\Framework\Attributes\DataProvider;
2627
use PHPUnit\Framework\Attributes\Group;
2728

2829
/**
2930
* @internal
3031
*/
32+
#[BackupGlobals(true)]
3133
#[Group('Others')]
3234
final class ResponseTest extends CIUnitTestCase
3335
{
@@ -169,6 +171,7 @@ public function testSetLink(): void
169171
Factories::injectMock('config', 'App', $config);
170172

171173
$this->resetServices();
174+
Services::injectMock('superglobals', new Superglobals([], []));
172175

173176
$response = new Response($config);
174177
$pager = service('pager');

‎tests/system/HTTP/UserAgentTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
namespace CodeIgniter\HTTP;
1515

1616
use CodeIgniter\Test\CIUnitTestCase;
17+
use PHPUnit\Framework\Attributes\BackupGlobals;
1718
use PHPUnit\Framework\Attributes\Group;
1819

1920
/**
2021
* @internal
2122
*/
23+
#[BackupGlobals(true)]
2224
#[Group('Others')]
2325
final class UserAgentTest extends CIUnitTestCase
2426
{
@@ -30,6 +32,8 @@ protected function setUp(): void
3032
{
3133
parent::setUp();
3234

35+
$this->resetServices();
36+
3337
// set a baseline user agent
3438
service('superglobals')->setServer('HTTP_USER_AGENT', $this->_user_agent);
3539

0 commit comments

Comments
 (0)