Skip to content

Commit 2fdb93d

Browse files
committed
fix(response): 🐛 assertContentType() considers charset and boundary directives
1 parent da7d152 commit 2fdb93d

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.0.1 - 2025-05-25
4+
### Fixed
5+
- `assertContentType(string $contentType)` doesn't fail when the header contains charset or boundary directives
6+
37
## 2.0.0 - 2025-05-18
48
### Changed
59
- Trait and interface names: `TestCase -> ExtendedTestCase`, `ExtendedTestCase -> ExtendedTestCaseInterface`

src/Assert/ResponseAssert.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use function explode;
1212
use function join;
1313
use function preg_split;
14+
use function strcasecmp;
1415
use function trim;
1516
use const PREG_SPLIT_NO_EMPTY;
1617

@@ -130,7 +131,10 @@ public function assertContentNotRegex(string $regex): void {
130131
* ```
131132
*/
132133
public function assertContentType(string $contentType): void {
133-
$this->assertHeaderEquals('Content-Type', $contentType);
134+
$values = $this->response->getHeader('Content-Type');
135+
$header = $values ? $values[0] : '';
136+
[$actualType] = preg_split('/\\s*;\\s*/', $header);
137+
$this->test->assertEquals(0, strcasecmp($actualType, $contentType), "Expected the response to have the header \"Content-Type\" with value \"{$contentType}\", actual: \"{$actualType}\"");
134138
}
135139

136140
/**

test/Assert/ResponseAssertTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public function testAssertContentType(?string $exceptionMessage, ResponseInterfa
125125
public static function dataAssertContentType(): array {
126126
return [
127127
'passed' => [null, new Response(200, ['Content-Type' => 'text/html']), 'text/html'],
128+
'passed with charset without space' => [null, new Response(200, ['content-type' => 'text/html;charset=utf-8']), 'text/html'],
129+
'passed with charset and space' => [null, new Response(200, ['content-type' => 'text/html; charset=utf-8']), 'text/html'],
130+
'passed with boundary without space' => [null, new Response(200, ['content-type' => 'text/html;boundary=----']), 'text/html'],
131+
'passed with boundary and space' => [null, new Response(200, ['content-type' => 'text/html; boundary=----']), 'text/html'],
132+
'passed with charset and boundary' => [null, new Response(200, ['content-type' => 'text/html;charset=utf-8;boundary=----']), 'text/html'],
128133
'failed' => ['Expected the response to have the header "Content-Type" with value "text/html", actual: ""', new Response(200), 'text/html'],
129134
];
130135
}

0 commit comments

Comments
 (0)