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

handle head requests #603

Merged
merged 26 commits into from
Sep 18, 2019
Merged

handle head requests #603

merged 26 commits into from
Sep 18, 2019

Conversation

lag-of-death
Copy link
Contributor

@lag-of-death lag-of-death commented Sep 11, 2019

Fixes #575

Checklist

  • Tests have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce?

Bug fix

@lag-of-death lag-of-death changed the title [wip] feat: handle head requests handle head requests Sep 11, 2019
Copy link
Contributor

@XVincentX XVincentX left a comment

Choose a reason for hiding this comment

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

This looks good.

  1. 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.

  2. We should probably add some unit tests in the code since we have a pretty good tests battery.

  3. Please add a changelog line

packages/http/src/mocker/negotiator/NegotiatorHelpers.ts Outdated Show resolved Hide resolved
@lag-of-death
Copy link
Contributor Author

@philsturgeon, could you take a look at the questions ^ ? thanks!

Copy link
Contributor

@XVincentX XVincentX left a 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.

CHANGELOG.md Outdated Show resolved Hide resolved
chain(result => assembleResponse(result, payloadGenerator)),
);
const isHead = input.data.method === 'head';
const r = get(resource, ['responses', '0'], { code: 200, headers: [] });
Copy link
Contributor

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.

Copy link
Contributor

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.

  1. We still need input validation (query string and whatever)
  2. 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:

https://github.com/stoplightio/prism/blob/master/packages/http/src/mocker/negotiator/NegotiatorHelpers.ts#L140

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?

packages/http/src/mocker/HttpMocker.ts Outdated Show resolved Hide resolved
packages/http/src/mocker/HttpMocker.ts Outdated Show resolved Hide resolved
curl --head http://localhost:4010/todos
====expect====
HTTP/1.1 200 OK
nonce: abc
Copy link
Contributor

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 :)

@lag-of-death lag-of-death requested review from XVincentX and removed request for philsturgeon September 18, 2019 10:11
Copy link
Contributor

@XVincentX XVincentX left a 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.`);
Copy link
Contributor

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;
Copy link
Contributor

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(
Copy link
Contributor

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
Copy link
Contributor

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

@XVincentX XVincentX merged commit e04eb2e into master Sep 18, 2019
@XVincentX XVincentX deleted the feat/head-requests branch September 18, 2019 16:57
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.

HEAD requests must not expect to find a response body schema
3 participants