Skip to content

Commit bdd2ab9

Browse files
authored
Handle lowercase Content-Type header for JSON and XML (#534)
Fix: Handle lowercase Content-Type header for JSON and XML
1 parent 634be16 commit bdd2ab9

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

src/Http/Response.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,14 +494,12 @@ public function header(string $header): string|array|null
494494
*/
495495
public function isJson(): bool
496496
{
497-
$contentType = $this->header('Content-Type');
497+
$contentType = $this->psrResponse->getHeaderLine('Content-Type');
498498

499-
if (is_null($contentType)) {
499+
if ($contentType === '') {
500500
return false;
501501
}
502502

503-
$contentType = is_array($contentType) ? $contentType[0] : $contentType;
504-
505503
return str_contains($contentType, 'json');
506504
}
507505

@@ -510,14 +508,12 @@ public function isJson(): bool
510508
*/
511509
public function isXml(): bool
512510
{
513-
$contentType = $this->header('Content-Type');
511+
$contentType = $this->psrResponse->getHeaderLine('Content-Type');
514512

515-
if (is_null($contentType)) {
513+
if ($contentType === '') {
516514
return false;
517515
}
518516

519-
$contentType = is_array($contentType) ? $contentType[0] : $contentType;
520-
521517
return str_contains($contentType, 'xml');
522518
}
523519

tests/Unit/ResponseTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@
415415
MockResponse::make(['foo' => 'bar'], 200, ['Content-Type' => 'application/json']),
416416
// JSON with charset
417417
MockResponse::make(['foo' => 'bar'], 200, ['Content-Type' => 'application/json; charset=utf-8']),
418+
// JSON with lowercase header (e.g. FastAPI, HTTP/2)
419+
MockResponse::make(['foo' => 'bar'], 200, ['content-type' => 'application/json']),
418420
// Non-JSON content type
419421
MockResponse::make('plain text', 200, ['Content-Type' => 'text/plain']),
420422
// No content type
@@ -429,6 +431,9 @@
429431
$response = $connector->send(new UserRequest, $mockClient);
430432
expect($response->isJson())->toBeTrue();
431433

434+
$response = $connector->send(new UserRequest, $mockClient);
435+
expect($response->isJson())->toBeTrue();
436+
432437
$response = $connector->send(new UserRequest, $mockClient);
433438
expect($response->isJson())->toBeFalse();
434439

@@ -442,6 +447,8 @@
442447
MockResponse::make('<?xml version="1.0"?><root></root>', 200, ['Content-Type' => 'application/xml']),
443448
// XML with charset
444449
MockResponse::make('<?xml version="1.0"?><root></root>', 200, ['Content-Type' => 'text/xml; charset=utf-8']),
450+
// XML with lowercase header (e.g. FastAPI, HTTP/2)
451+
MockResponse::make('<?xml version="1.0"?><root></root>', 200, ['content-type' => 'application/xml']),
445452
// Non-XML content type
446453
MockResponse::make('plain text', 200, ['Content-Type' => 'text/plain']),
447454
// No content type
@@ -456,6 +463,9 @@
456463
$response = $connector->send(new UserRequest, $mockClient);
457464
expect($response->isXml())->toBeTrue();
458465

466+
$response = $connector->send(new UserRequest, $mockClient);
467+
expect($response->isXml())->toBeTrue();
468+
459469
$response = $connector->send(new UserRequest, $mockClient);
460470
expect($response->isXml())->toBeFalse();
461471

0 commit comments

Comments
 (0)