Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 5, 2022

Conversation

ksassnowski
Copy link
Contributor

@ksassnowski ksassnowski commented Jul 4, 2022

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