-
Notifications
You must be signed in to change notification settings - Fork 338
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
handle head requests #603
handle head requests #603
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good.
-
We just need to understand from a product perspective what we want to do with the HEAD method in general (skipping the whole negotiation process); this is up to @philsturgeon and in such case we might need to change a little bit the code, and move this check outside.
-
We should probably add some unit tests in the code since we have a pretty good tests battery.
-
Please add a changelog line
@philsturgeon, could you take a look at the questions ^ ? thanks! |
…o master # Conflicts: # packages/http/src/mocker/negotiator/NegotiatorHelpers.ts
ee7cc95
to
2733487
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dropped another bunch of comments. We're getting closer, we need to make some other changes though.
chain(result => assembleResponse(result, payloadGenerator)), | ||
); | ||
const isHead = input.data.method === 'head'; | ||
const r = get(resource, ['responses', '0'], { code: 200, headers: [] }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should only be happening if isHead
is true, otherwise the result will never be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't be enough, it's simply relying on the first response; imagine my first response is a 500 instead of a 200 (or also a 204) — this is not what we want to return. We still need to go through some parts of the negotiation process. It would be a good idea to add a test to make sure it behaves correctly on this.
- We still need input validation (query string and whatever)
- We need to still go through the regular response checking.
I guess I was kind of wrong when I was saying we can skip most of the negotiation process, I am sorry.
I think the best way to plug the code would be here:
We can detect here if we're dealing with an head
request and in such case early return a right with an empty
body skipping all the code we see. If we could write a log line as well, that'd be great.
Does that make sense?
curl --head http://localhost:4010/todos | ||
====expect==== | ||
HTTP/1.1 200 OK | ||
nonce: abc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also add a test to make sure that if we request an HEAD
and this is not defined in the document, we get a 404?
Just to make sure the router stops the request entirely. I know this is covered in unit tests as well, but you never know I guess :)
36e129c
to
5968a89
Compare
# Conflicts: # CHANGELOG.md
5968a89
to
b733051
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4 very very minor things and we can finally merge this 🚀
@@ -135,6 +134,15 @@ const helpers = { | |||
const { mediaTypes, dynamic, exampleKey } = desiredOptions; | |||
|
|||
return withLogger(logger => { | |||
if (_httpOperation.method === 'head') { | |||
logger.warn(`Responding with an empty body to a HEAD request.`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be a warn, there's nothing to be worried about. We can use info
@@ -4,7 +4,7 @@ import { IHttpOperationConfig, JSONSchema } from '../../'; | |||
|
|||
export interface IHttpNegotiationResult { | |||
code: string; | |||
mediaType: string; | |||
mediaType?: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok I understand now, if the request is HEAD
there is no media type we're effectively returning.
|
||
expect(Either.isRight(actualResponse)).toBeTruthy(); | ||
|
||
pipe( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use the assertRight
function.
assertRight(
actualResponse,
response => {
expect(response).not.toHaveProperty('bodyExample');
expect(response).not.toHaveProperty('mediaType');
expect(response).not.toHaveProperty('schema');
},
);
CHANGELOG.md
Outdated
@@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | |||
- Prism is correctly handling the `csv` collection format argument property in OAS2 documents [#577](https://github.com/stoplightio/prism/pulls/577) | |||
- Prism is correctly returning the response when the request has `*/*` as Accept header [#578](https://github.com/stoplightio/prism/pulls/578) | |||
- Prism is correctly returning a single root node with the payload for XML data [#578](https://github.com/stoplightio/prism/pulls/578) | |||
- HEAD requests no longer fail with 406 Not Acceptable #603 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not in the right section. It should go In the Fixed
part of Unreleased
section
07cd1fb
to
21f034a
Compare
Fixes #575
Checklist
What kind of change does this PR introduce?
Bug fix