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

allow empty responses #606

Merged
merged 36 commits into from
Sep 23, 2019
Merged

allow empty responses #606

merged 36 commits into from
Sep 23, 2019

Conversation

lag-of-death
Copy link
Contributor

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

Fixes #584
Fixes #592

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.

What is the current behavior? What is the new behavior?

No empty responses were allowed.

@lag-of-death lag-of-death changed the title [wip] fix: allow empty responses allow empty responses Sep 13, 2019
Copy link
Contributor

@karol-maciaszek karol-maciaszek left a comment

Choose a reason for hiding this comment

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

Discussion needed on support for q-factor in Accept header and empty content in invalid requests.

packages/http/src/mocker/negotiator/NegotiatorHelpers.ts Outdated Show resolved Hide resolved
packages/http/src/mocker/negotiator/NegotiatorHelpers.ts Outdated Show resolved Hide resolved
@XVincentX XVincentX mentioned this pull request Sep 17, 2019
2 tasks
@lag-of-death lag-of-death requested review from karol-maciaszek and XVincentX and removed request for XVincentX September 17, 2019 13:40
lag-of-death and others added 3 commits September 17, 2019 16:09
* chore: upgrade jsf

* test: add e2e

* chore: switch back to npm version

* docs: changelog

* test: dry
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 some comments. The code flow is still not clear but hopefully it'll be better after addressing these comments.

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

@XVincentX, I believe the feedback is addressed now, ready for another round

@XVincentX
Copy link
Contributor

@lag-of-death Can you rebase when you get a chance?

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 some other comments but in general I think we're getting close to the end of this 👍

packages/http/src/mocker/negotiator/InternalHelpers.ts Outdated Show resolved Hide resolved
packages/http/src/mocker/negotiator/NegotiatorHelpers.ts Outdated Show resolved Hide resolved
packages/http/src/mocker/negotiator/NegotiatorHelpers.ts Outdated Show resolved Hide resolved
@@ -7,6 +7,8 @@ swagger: "2.0"
paths:
/todos:
get:
produces:
- text/plain
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes!

@lag-of-death lag-of-death changed the title [wip] allow empty responses allow empty responses Sep 20, 2019
@lag-of-death lag-of-death added this to the Oct '19 milestone Sep 23, 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.

Dropped another bunch of comments. We're getting there but I am already anticipating this is going to require yet another round of review.

packages/http/src/mocker/negotiator/InternalHelpers.ts Outdated Show resolved Hide resolved
packages/http/src/mocker/negotiator/NegotiatorHelpers.ts Outdated Show resolved Hide resolved
Either.fromOption(() => {
logger.warn(warnMsg(mediaTypes));

return ProblemJsonError.fromTemplate(
Copy link
Contributor

Choose a reason for hiding this comment

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

You're right; this does not feel right but I might be the dude introducing this so I'll look into. Why the cast though? as Error

packages/http/src/mocker/negotiator/NegotiatorHelpers.ts Outdated Show resolved Hide resolved
packages/http/src/mocker/negotiator/NegotiatorHelpers.ts Outdated Show resolved Hide resolved
packages/http/src/mocker/negotiator/NegotiatorHelpers.ts Outdated Show resolved Hide resolved
return Either.left<Error, IHttpNegotiationResult>(
ProblemJsonError.fromTemplate(NOT_ACCEPTABLE, `Unable to find content for ${mediaTypes}`),
return pipe(
optionallyGetPayloadlessResponse(response, headers || [], mediaTypes),
Copy link
Contributor

Choose a reason for hiding this comment

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

findEmptyResponse

const noAcceptAndNoContentResponses =
!noContentResponses.length && !mediaTypes.filter((mt: string) => !mt.includes('*/*')).length;

return noAcceptAndNoContentResponses || ['400', '422'].includes(response.code);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you checking for 400 and 422 responses manually? Don't they follow the same flow?

Option.chain(responses => {
return Option.fromPredicate((noContentResponses: { length: number }) => {
const noAcceptAndNoContentResponses =
!noContentResponses.length && !mediaTypes.filter((mt: string) => !mt.includes('*/*')).length;
Copy link
Contributor

Choose a reason for hiding this comment

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

I really think we should put a comment on this condition, I still struggle to understand it correctly.

Also, with includes Accept: application/json,*/* will match. Is this intentional? I guess so (if the client accepts / then an empty response is viable, right?)

@lag-of-death
Copy link
Contributor Author

hi @XVincentX, I think I've addressed the feedback. The logic for findEmptyResponse is now greatly simplified.
The previous version was kind of redundant because some of the conditional checks were already performed up the chain - somewhere in findEmptyResponse's caller or higher.
There are also a couple of more tests so that hopefully no edge case is missed.

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.

I think we're finally there. Just one final change (pipe for fromPredicate); I'm checking out the tests but quite sure we'll merge this today.

mediaTypes: string[],
): Option.Option<IHttpNegotiationResult> => {
return pipe(
Option.fromPredicate((contentTypes: string[]) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

pipe(mediaTypes, Option.fromPredicate(…), Option.map

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.

We got this. Excellent test coverage.

@XVincentX XVincentX merged commit b65c945 into master Sep 23, 2019
@XVincentX XVincentX deleted the fix/empty-responses branch September 23, 2019 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants