Skip to content

[9.x] Fix inconsistent content type when using ResponseSequence - #43051

Merged
taylorotwell merged 1 commit into
laravel:9.xfrom
ksassnowski:9.x
Jul 5, 2022
Merged

[9.x] Fix inconsistent content type when using ResponseSequence#43051
taylorotwell merged 1 commit into
laravel:9.xfrom
ksassnowski:9.x

Conversation

@ksassnowski

@ksassnowski ksassnowski commented Jul 4, 2022

Copy link
Copy Markdown
Contributor

This PR fixes an inconsistency when using the ResponseSequence::push method.

Summary

The \Illuminate\Http\Client\Factory::response() method sets the response’s Content-Type header to application/json and JSON encodes the response body if an array was passed as the $body parameter.

Http::fake(fn () => Http::response(['foo' => 'bar']));
$response = Http::get('http://example.com');
$response->header('Content-Type');
// => "application/json"

The push method of the ResponseSequence internally calls Factory::response to create a new Response object. However, the ResponseSequence::push method also JSON encodes the request body if an array was passed before calling Factory::response. This means that the Content-Type header will not get set to application/json in this case because $body will be a string instead of an array.

Http::fakeSequence()->push(['foo' => 'bar']);
$response = Http::get('http://example.com');
$response->header('Content-Type');
// => ""

The fix is to simply not JSON serialize the $body variable inside ResponseSequence::push as Factory::response already does that.

@taylorotwell
taylorotwell merged commit 6de62a6 into laravel:9.x Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants