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

[8.x] Prevent assertStatus() invalid JSON exception for valid JSON response content #38192

Merged

Conversation

derekmd
Copy link
Contributor

@derekmd derekmd commented Jul 31, 2021

Fixes: #38178
Replaces pull request: #38189 (this pull request adds tabuna's GZIP-encoding test case)

Test suite JSON validation message feedback added in v8.52.0 (#38046 #38088) is causing assertStatus() to throw "Invalid JSON was returned from the route" on some valid JSON responses. A few known cases:

  1. JSON response is a boolean false and not a key/value payload (described in the linked issue):
    • status code: 200 HTTP OK
    • header: Content-Type: application/json
    • body: false
  2. JSON response body is GZIP-encoded.

The new custom validation feedback must avoid the below error checking block intended for the assertJson*() methods that inspect key/value payloads:

if (is_null($decodedResponse) || $decodedResponse === false) {
if ($this->exception) {
throw $this->exception;
} else {
PHPUnit::fail('Invalid JSON was returned from the route.');
}
}

...since validation of a JSON payload is unrelated to the HTTP status code being asserted. Instead silently attempt to gather the JSON 'errors' validation messages and fallback gracefully to the generic PHPUnit failure message.

derekmd and others added 2 commits August 1, 2021 02:14
Don't throw exception "Invalid JSON was returned from the route."
when asserting a HTTP response status code on a JSON endpoint returning
'false'.

A HTTP response body can be:
* status code: 200 HTTP OK
* header: Content-Type: application/json
* body: false

Starting in v8.52.0, assertStatus() shows errors messages if any are
found in a JSON response body. The TestResponse@json() method should be
avoided since it applies additional key/value payload error checking
unrelated to the HTTP status code.

Instead silently detect JSON response body key 'errors' and only
throw the "Invalid JSON" exception when Laravel's JSON assertion methods
are called.
Avoid exception "Invalid JSON was returned from the route"
when the TestResponse class is unable to discover the
JSON response body 'errors' key.
@derekmd derekmd force-pushed the assert-status-doesnt-fail-on-json-false branch from e1c55d9 to 273dfae Compare August 1, 2021 06:15
@taylorotwell taylorotwell merged commit 6f21e2e into laravel:8.x Aug 1, 2021
@taylorotwell
Copy link
Member

Thanks!

@derekmd derekmd deleted the assert-status-doesnt-fail-on-json-false branch August 1, 2021 22:36
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.

[v8.52.0] Invalid JSON was returned from the route.
3 participants