Skip to content

Commit db9856f

Browse files
[5.x] Fix failing tests due to lowercase utf-8 charset (#13213)
Co-authored-by: Jason Varga <jason@pixelfear.com>
1 parent 2d5a049 commit db9856f

4 files changed

Lines changed: 43 additions & 20 deletions

File tree

src/Http/Responses/DataResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,13 @@ public static function contentType($type)
216216
{
217217
switch ($type) {
218218
case 'html':
219-
return 'text/html; charset=UTF-8';
219+
return 'text/html; charset=utf-8';
220220
case 'xml':
221221
return 'text/xml';
222222
case 'rss':
223223
return 'application/rss+xml';
224224
case 'atom':
225-
return 'application/atom+xml; charset=UTF-8';
225+
return 'application/atom+xml; charset=utf-8';
226226
case 'json':
227227
return 'application/json';
228228
case 'text':

tests/FrontendTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -464,34 +464,34 @@ public function changes_content_type_to_xml()
464464
{
465465
$this->createPage('about', ['with' => ['content_type' => 'xml']]);
466466

467-
// Laravel adds utf-8 if the content-type starts with text/
468-
$this->get('about')->assertHeader('Content-Type', 'text/xml; charset=UTF-8');
467+
// Symfony adds utf-8 if the content-type starts with text/
468+
$this->get('about')->assertContentType('text/xml; charset=utf-8');
469469
}
470470

471471
#[Test]
472472
public function changes_content_type_to_atom()
473473
{
474474
$this->createPage('about', ['with' => ['content_type' => 'atom']]);
475475

476-
// Laravel adds utf-8 if the content-type starts with text/
477-
$this->get('about')->assertHeader('Content-Type', 'application/atom+xml; charset=UTF-8');
476+
// Symfony adds utf-8 if the content-type starts with text/
477+
$this->get('about')->assertContentType('application/atom+xml; charset=utf-8');
478478
}
479479

480480
#[Test]
481481
public function changes_content_type_to_json()
482482
{
483483
$this->createPage('about', ['with' => ['content_type' => 'json']]);
484484

485-
$this->get('about')->assertHeader('Content-Type', 'application/json');
485+
$this->get('about')->assertContentType('application/json');
486486
}
487487

488488
#[Test]
489489
public function changes_content_type_to_text()
490490
{
491491
$this->createPage('about', ['with' => ['content_type' => 'text']]);
492492

493-
// Laravel adds utf-8 if the content-type starts with text/
494-
$this->get('about')->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
493+
// Symfony adds utf-8 if the content-type starts with text/
494+
$this->get('about')->assertContentType('text/plain; charset=utf-8');
495495
}
496496

497497
#[Test]
@@ -504,7 +504,7 @@ public function xml_antlers_template_with_xml_layout_will_use_both_and_change_th
504504

505505
$response = $this
506506
->get('about')
507-
->assertHeader('Content-Type', 'text/xml; charset=UTF-8');
507+
->assertContentType('text/xml; charset=utf-8');
508508

509509
$this->assertEquals('<?xml ?><foo></foo>', $response->getContent());
510510
}
@@ -519,7 +519,7 @@ public function xml_antlers_template_with_non_xml_layout_will_change_content_typ
519519

520520
$response = $this
521521
->get('about')
522-
->assertHeader('Content-Type', 'text/xml; charset=UTF-8');
522+
->assertContentType('text/xml; charset=utf-8');
523523

524524
$this->assertEquals('<foo></foo>', $response->getContent());
525525
}
@@ -534,7 +534,7 @@ public function xml_antlers_layout_will_change_the_content_type()
534534

535535
$response = $this
536536
->get('about')
537-
->assertHeader('Content-Type', 'text/xml; charset=UTF-8');
537+
->assertContentType('text/xml; charset=utf-8');
538538

539539
$this->assertEquals('<?xml ?><foo></foo>', $response->getContent());
540540
}
@@ -551,7 +551,7 @@ public function xml_blade_template_will_not_change_content_type()
551551

552552
$response = $this
553553
->get('about')
554-
->assertHeader('Content-Type', 'text/html; charset=UTF-8');
554+
->assertContentType('text/html; charset=utf-8');
555555

556556
$this->assertEquals('<foo></foo>', $response->getContent());
557557
}
@@ -566,7 +566,7 @@ public function xml_template_with_custom_content_type_does_not_change_to_xml()
566566

567567
$this
568568
->get('about')
569-
->assertHeader('Content-Type', 'application/json');
569+
->assertContentType('application/json');
570570
}
571571

572572
#[Test]

tests/Routing/RoutesTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ public function it_renders_a_view_with_custom_content_type()
473473

474474
$this->get('/route-with-custom-content-type')
475475
->assertOk()
476-
->assertHeader('Content-Type', 'application/json')
476+
->assertContentType('application/json')
477477
->assertExactJson(['hello' => 'world']);
478478
}
479479

@@ -486,7 +486,7 @@ public function xml_antlers_template_with_xml_layout_will_use_both_and_change_th
486486

487487
$response = $this
488488
->get('/xml')
489-
->assertHeader('Content-Type', 'text/xml; charset=UTF-8');
489+
->assertContentType('text/xml; charset=utf-8');
490490

491491
$this->assertEquals('<?xml ?><foo></foo>', $response->getContent());
492492
}
@@ -500,7 +500,7 @@ public function xml_antlers_template_with_non_xml_layout_will_change_content_typ
500500

501501
$response = $this
502502
->get('/xml')
503-
->assertHeader('Content-Type', 'text/xml; charset=UTF-8');
503+
->assertContentType('text/xml; charset=utf-8');
504504

505505
$this->assertEquals('<foo></foo>', $response->getContent());
506506
}
@@ -514,7 +514,7 @@ public function xml_antlers_layout_will_change_the_content_type()
514514

515515
$response = $this
516516
->get('/xml')
517-
->assertHeader('Content-Type', 'text/xml; charset=UTF-8');
517+
->assertContentType('text/xml; charset=utf-8');
518518

519519
$this->assertEquals('<?xml ?><foo></foo>', $response->getContent());
520520
}
@@ -530,7 +530,7 @@ public function xml_blade_template_will_not_change_content_type()
530530

531531
$response = $this
532532
->get('/xml')
533-
->assertHeader('Content-Type', 'text/html; charset=UTF-8');
533+
->assertContentType('text/html; charset=utf-8');
534534

535535
$this->assertEquals('<foo></foo>', $response->getContent());
536536
}
@@ -544,7 +544,7 @@ public function xml_template_with_custom_content_type_does_not_change_to_xml()
544544

545545
$this
546546
->get('/xml-with-custom-type')
547-
->assertHeader('Content-Type', 'application/json');
547+
->assertContentType('application/json');
548548
}
549549

550550
#[Test]

tests/TestCase.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,29 @@ private function addGqlMacros()
238238

239239
return $this;
240240
});
241+
242+
// Symfony 7.4.0 changed "UTF-8" to "utf-8".
243+
// https://github.com/symfony/symfony/pull/60685
244+
// While we continue to support lower versions, we'll do a case-insensitive check.
245+
// This macro is essentially assertHeader but with case-insensitive value check.
246+
TestResponse::macro('assertContentType', function (string $value) {
247+
$headerName = 'Content-Type';
248+
249+
Assert::assertTrue(
250+
$this->headers->has($headerName), "Header [{$headerName}] not present on response."
251+
);
252+
253+
$actual = $this->headers->get($headerName);
254+
255+
if (! is_null($value)) {
256+
Assert::assertEquals(
257+
strtolower($value), strtolower($this->headers->get($headerName)),
258+
"Header [{$headerName}] was found, but value [{$actual}] does not match [{$value}]."
259+
);
260+
}
261+
262+
return $this;
263+
});
241264
}
242265

243266
public function __call($name, $arguments)

0 commit comments

Comments
 (0)