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

Respect Accept header when processing requests #333

Merged
merged 21 commits into from
Jun 4, 2019
Merged

Conversation

XVincentX
Copy link
Contributor

@XVincentX XVincentX commented Jun 3, 2019

  • Run test harness AFTER the normal tests.
  • Modify the OAS example document to put JSON first, otherwise it will try to produce xml when requesting */* (and this is something we do not to support yet)
  • Use fastify-accept-serialiser to use the normal stringify not only on application/json but to anything that can be serialised as JSON anyway
  • Correctly end the request in case there's an error while serialising the payload
  • Correctly use the accept header when selecting the content

Closes #332
Closes #288

@XVincentX XVincentX force-pushed the SO-98-accept branch 2 times, most recently from 0fb4864 to 97f6608 Compare June 3, 2019 14:17
@XVincentX XVincentX changed the title So 98 accept Respect Accept header when processing requests Jun 3, 2019
@XVincentX XVincentX force-pushed the SO-98-accept branch 3 times, most recently from 354c49c to 7487ab8 Compare June 3, 2019 15:31
@XVincentX XVincentX marked this pull request as ready for review June 3, 2019 15:32
@XVincentX
Copy link
Contributor Author

XVincentX commented Jun 3, 2019

@philsturgeon

Given the requirement changes, if we can figure out WHEN exactly send a 406, this could close #288 as well.

Right now, in case the content is not found, we're returning an empty body with the requested status code. Maybe we should return a 406?

@XVincentX XVincentX force-pushed the SO-98-accept branch 2 times, most recently from 3dfd882 to fa254ba Compare June 3, 2019 15:38
Copy link
Contributor

@pytlesk4 pytlesk4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if this is the produces for an operation?

['application/xml', 'text/plain']

And the accepts header is:

'image/jpeg;q=0.8, application/xml;q=0.3, text/plain;q=0.2'

Prism should try to mock image/jpeg, but it isn't defined in produces. So we should then try to find application/xml and because it is defined in produces we should mock it. Is that how it works?

@philsturgeon
Copy link
Contributor

So the "returning an empty body" thing is when we have confirmed that a code should be sent back and we've just failed to find an example.

We want to add a layer in, before the example/empty stuff even comes into play, which is "are they accepting for a valid content type. If they ask for a content type the spec doesnt support according to accepts package, errors.

If they ask for a content type that is supported but doesnt have any example or schema, then empty body.

Does that make sense?

@XVincentX
Copy link
Contributor Author

@pytlesk4 Good point on the multiple http headers — I'll write a test right now and fix it if necessary.
@philsturgeon Yes, that explains. On it!

@XVincentX
Copy link
Contributor Author

@pytlesk4 3090fb3 and 2969c4d are checking out multiple accept headers.

pytlesk4
pytlesk4 previously approved these changes Jun 3, 2019
@XVincentX
Copy link
Contributor Author

@philsturgeon d120bad implements the requested behaviour. I'd suggest you to pull out the branch and check it out really quickly.

@philsturgeon
Copy link
Contributor

Really close!

$ http GET  http://127.0.0.1:4010/no_auth/pets\?name\=234 'Accept: image/jpeg;q=0.8, application/xml;q=0.3, text/plain;q=0.2'
HTTP/1.1 406 Not Acceptable
Connection: keep-alive
Date: Mon, 03 Jun 2019 19:05:00 GMT
content-length: 89
content-type: application/json; charset=utf-8

{
    "error": "Not Acceptable",
    "message": "Allowed: /json$/,application/json",
    "statusCode": 406
}

It should be returning a problems JSON and that message is looking a bit funky.

@XVincentX
Copy link
Contributor Author

XVincentX commented Jun 3, 2019

@philsturgeon Try it again, it should be ok now.

Vincenzos-MacBook-Stoplight:prism vncz$ curl -s -D "/dev/stderr" http://127.0.0.1:4010/no_auth/pets\?name\=234 -H 'Accept: image/jpeg;q=0.8, application/xml;q=0.3, text/plain;q=0.2' | json_pp
HTTP/1.1 406 Not Acceptable
content-type: application/problem+json
content-length: 271
Date: Mon, 03 Jun 2019 19:16:34 GMT
Connection: keep-alive

{
   "type" : "https://stoplight.io/prism/errors#NOT_ACCEPTABLE",
   "detail" : "Could not find any content that satisfies image/jpeg;q=0.8, application/xml;q=0.3, text/plain;q=0.2",
   "status" : 406,
   "title" : "The server cannot produce a response matching your content negotiation header"
}
Vincenzos-MacBook-Stoplight:prism vncz$ 

XVincentX and others added 2 commits June 3, 2019 21:22
Co-Authored-By: Phil Sturgeon <me@philsturgeon.uk>
@XVincentX XVincentX requested a review from philsturgeon June 4, 2019 07:26
@XVincentX
Copy link
Contributor Author

@philsturgeon Harness for 406 response added.

@philsturgeon
Copy link
Contributor

amazing!

@XVincentX XVincentX merged commit 91749c9 into master Jun 4, 2019
@XVincentX XVincentX deleted the SO-98-accept branch June 4, 2019 09:40
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.

Prism is not respecting the Accept header Accept Header
3 participants