[7.x] Add ArrayAccess support for Http client get requests #32401
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #32398.
TL;DR
Fixes an
Undefined index: Content-Type
exception and allows access to get request params via array access$request['foo']
and in turn allows you to make assertions such as$request['foo'] === 'bar'
.Description:
I was writing a test similar to the below and when running it was greeted with an
ErrorException
ofUndefined index: Content-Type
.Cause
Looking at the line causing the error it seems it just needs to check if the key it is searching for exists in the headers array before trying to access it:
Illuminate/Http/Client/Request.php:
Initial fix & New error found
So I initially set out to fix
hasHeader
so that it checks the$key
exists in thearray
returned by$this->headers()
and I ran the tests and got the same errorUndefined index: Content-Type
but from a different location:Illuminate/Http/Client/Request.php:
Adding ArrayAccess Support
I then added
Arr::get
check in theheader($key)
method and then discovered that there is not any existing support for accessing get query parameters via$request
and was met with anotherUndefined index: foo
in the below test:So this change gets the query parameters either from
$options['query']
or by extracting them from the$url
. It handles the below with the tests adjusted to prove it.Then you can do assertions on the
$request
data: