Skip to content

Commit 9b0afc4

Browse files
committed
fix: resolve random order execution failures in HTTP test suite
1 parent ad8526f commit 9b0afc4

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
@@ -86,7 +86,7 @@ public function appendBody($data): self
8686
*/
8787
public function populateHeaders(): void
8888
{
89-
$contentType = service('superglobals')->server('CONTENT_TYPE', (string) getenv('CONTENT_TYPE'));
89+
$contentType = service('superglobals')->server('CONTENT_TYPE');
9090
if (! empty($contentType)) {
9191
$this->setHeader('Content-Type', $contentType);
9292
}
@@ -283,18 +283,25 @@ protected function getHeaderName(string $name): string
283283
*/
284284
public function setProtocolVersion(string $version): self
285285
{
286-
if (! is_numeric($version)) {
286+
// If empty or null, keep default protocol version (usually 1.1) and do nothing.
287+
if ($version === '' || $version === null) {
288+
return $this;
289+
}
290+
291+
// If a full protocol string (e.g., "HTTP/1.1") is provided, extract the numeric part.
292+
if (strpos($version, '/') !== false) {
287293
$version = substr($version, strpos($version, '/') + 1);
288294
}
289295

290-
// Make sure that version is in the correct format
291-
$version = number_format((float) $version, 1);
296+
// Normalize to a single decimal place as used in validProtocolVersions.
297+
$normalized = number_format((float) $version, 1);
292298

293-
if (! in_array($version, $this->validProtocolVersions, true)) {
299+
// Throw exception if the version is not recognized.
300+
if (! in_array($normalized, $this->validProtocolVersions, true)) {
294301
throw HTTPException::forInvalidHTTPProtocol($version);
295302
}
296303

297-
$this->protocolVersion = $version;
304+
$this->protocolVersion = $normalized;
298305

299306
return $this;
300307
}

‎system/HTTP/RedirectResponse.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ public function withCookies()
161161
*/
162162
public function withHeaders()
163163
{
164-
foreach (service('response')->headers() as $name => $value) {
164+
$source = service('response');
165+
foreach ($source->headers() as $name => $value) {
165166
if ($value instanceof Header) {
166167
$this->setHeader($name, $value->getValue());
167168
} else {
@@ -170,6 +171,10 @@ public function withHeaders()
170171
}
171172
}
172173
}
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+
}
173178

174179
return $this;
175180
}

‎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
@@ -252,7 +252,7 @@ public function testSetValidLocales(): void
252252
*/
253253
public function testNegotiatesLocale(): void
254254
{
255-
service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr-FR); q=1.0, en; q=0.5');
255+
service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr-FR; q=1.0, en; q=0.5');
256256

257257
$config = new App();
258258
$config->negotiateLocale = true;
@@ -267,7 +267,7 @@ public function testNegotiatesLocale(): void
267267

268268
public function testNegotiatesLocaleOnlyBroad(): void
269269
{
270-
service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr); q=1.0, en; q=0.5');
270+
service('superglobals')->setServer('HTTP_ACCEPT_LANGUAGE', 'fr; q=1.0, en; q=0.5');
271271

272272
$config = new App();
273273
$config->negotiateLocale = true;
@@ -321,7 +321,7 @@ public function testNegotiatesEncoding(): void
321321
public function testNegotiatesLanguage(): void
322322
{
323323
$this->request->setHeader('Accept-Language', 'da, en-gb;q=0.8, en;q=0.7');
324-
$this->assertSame('en', $this->request->negotiate('language', ['en', 'da']));
324+
$this->assertSame('da', $this->request->negotiate('language', ['en', 'da']));
325325
}
326326

327327
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)