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

test for API\ResponseTrait::format() with format is not json or xml #2793

Merged

Conversation

samsonasik
Copy link
Member

By this, CodeIgniter\API\ResponseTrait will have 100% test coverage.

Checklist:

  • Securely signed commits
  • Unit testing, with >80% coverage

@MGatner
Copy link
Member

MGatner commented Apr 3, 2020

Using MockIncomingRequest means that header('accept') won't ever be set, so that means the format will always be set to the first formatter available (per https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/HTTP/Negotiate.php#L213). I expect RESTful features like this to have a major pass soon, so my preference would be to change the test to check that it is matching the first formatter, instead of explicitly assuming application/json. Does that make sense, or am I being crazy?

@samsonasik
Copy link
Member Author

the application/json is based on detection of data, if the data is a "xml like", it will marked as application/xml. See that I changed format to "txt" in controller __construct that it means formatting will find best match of negotiation with check supported format

@samsonasik
Copy link
Member Author

@MGatner
Copy link
Member

MGatner commented Apr 6, 2020

I understand the test, my concern is that the function call in question:

$this->request->negotiate('media', $config->supportedResponseFormats, false);

... ends up calling Negotiator::media() which uses getBestMatch():

return $this->getBestMatch($supported, $this->request->getHeaderLine('accept'), true, $strictMatch);

And in our test scenario getHeaderLine('accept') will always be empty. Without a header getBestMatch() simply falls back to the first format on the list:

return $strictMatch ? '' : $supported[0];

... so instead of checking for 'application/json; charset=UTF-8' should we check against something like config('App')->supportedResponseFormats[0]?

@samsonasik samsonasik force-pushed the test-response-trait-negotiate branch from a1b9fdf to bd2282c Compare April 7, 2020 07:01
@samsonasik
Copy link
Member Author

Ok, I updated with check against : config('Format')->supportedResponseFormats[0]

@samsonasik
Copy link
Member Author

merge-able?

@lonnieezell
Copy link
Member

@MGatner?

@MGatner
Copy link
Member

MGatner commented Apr 15, 2020

Sorry! My email is a total train wreck. Thanks for the mention, they help!

@MGatner MGatner merged commit 611d3bb into codeigniter4:develop Apr 15, 2020
@samsonasik samsonasik deleted the test-response-trait-negotiate branch April 15, 2020 23:53
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.

3 participants