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 HTTP code to Connect code mappings #762
Conversation
errorWriter := connect.NewErrorWriter() | ||
return http.HandlerFunc(func(respWriter http.ResponseWriter, req *http.Request) { | ||
testCaseName := req.Header.Get("x-test-case-name") | ||
// This is the only hard failure. Without it, we cannot provide feedback. | ||
// All other checks below write to stderr to provide feedback. | ||
if testCaseName == "" { | ||
// This is the only hard failure. Without it, we cannot provide feedback. | ||
// All other checks below write to stderr to provide feedback. | ||
http.Error(respWriter, "missing x-test-case-name header", http.StatusBadRequest) | ||
msg := "missing x-test-case-name header" | ||
if errorWriter.IsSupported(req) { | ||
invalidArg := connect.NewError(connect.CodeInvalidArgument, errors.New(msg)) | ||
err := errorWriter.Write(respWriter, req, invalidArg) | ||
if err != nil { | ||
http.Error(respWriter, err.Error(), http.StatusInternalServerError) | ||
} | ||
} else { | ||
http.Error(respWriter, msg, http.StatusBadRequest) | ||
} |
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 part LGTM. If you want to pull this out into a separate PR, we can go ahead and get it merged. (Though not a big deal if you just want to leave it here -- if this isn't obviously valuable outside of this PR, then maybe no need to do the work to carve out separate PR for it...).
if def.RawResponse != nil { | ||
// TODO - Handle other raw response values | ||
// If an HTTP error status was specified in the raw response, then the test case is | ||
// forcing the return of an HTTP error, which should be mapped to a Connect error | ||
// according to the protocol. | ||
if def.RawResponse.StatusCode >= 400 && def.RawResponse.StatusCode <= 599 { | ||
expected.Error = &conformancev1.Error{ | ||
Code: mapHTTPtoConnectCode(def.RawResponse.StatusCode), | ||
} |
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.
Not quite sure about this whole block. I know we discussed that you could specify a raw response and the standard response definition fields, but I was curious about two things:
- The proto comment mentions it should be one or the other in test cases
- If the comment is incorrect, then how should we best handle the case where an HTTP error code is specified in the raw response along with a defined
Error
to be returned? Should the definedError
override the raw response error inexpected
?
Also, will need to write some unit tests depending on what we decide there.
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.
I don't have a strong opinion here. I can see, for clarity, it would be best to just disallow the standard response definition.
But I also think, for consistency, there are things we'll want to tweak in how raw requests work (since it is currently necessary to specify both there).
Having said all that, I don't think we want this block. For raw responses, we should just require that the expected response be specified in the YAML file. That makes the test cases much more clear.
func mapHTTPtoConnectCode(httpCode uint32) int32 { | ||
var connectCode connect.Code | ||
switch httpCode { | ||
case 400: | ||
connectCode = connect.CodeInvalidArgument | ||
case 401: | ||
connectCode = connect.CodeUnauthenticated | ||
case 403: | ||
connectCode = connect.CodePermissionDenied | ||
case 404: | ||
connectCode = connect.CodeUnimplemented | ||
case 408: | ||
connectCode = connect.CodeDeadlineExceeded | ||
case 409: | ||
connectCode = connect.CodeAborted | ||
case 412: | ||
connectCode = connect.CodeFailedPrecondition | ||
case 413, 431: | ||
connectCode = connect.CodeResourceExhausted | ||
case 415: | ||
connectCode = connect.CodeInternal | ||
case 429, 502, 503, 504: | ||
connectCode = connect.CodeUnavailable | ||
default: | ||
connectCode = connect.CodeUnknown | ||
} | ||
return int32(connectCode) | ||
} |
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.
Per above remark, instead of trying to calculate anything about an expected response from a raw response definition, we should just specify this in the test case itself, in the expected response field.
Co-authored-by: Joshua Humphries <2035234+jhump@users.noreply.github.com>
Bumps [github.com/klauspost/compress](https://github.com/klauspost/compress) from 1.17.4 to 1.17.5. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="https://github.com/klauspost/compress/releases">github.com/klauspost/compress's releases</a>.</em></p> <blockquote> <h2>v1.17.5</h2> <h2>What's Changed</h2> <ul> <li>flate: Fix reset with dictionary on custom window encodes by <a href="https://github.com/klauspost"><code>@klauspost</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/912">klauspost/compress#912</a></li> <li>zstd: Limit better/best default window to 8MB by <a href="https://github.com/klauspost"><code>@klauspost</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/913">klauspost/compress#913</a></li> <li>zstd: Shorter and faster asm for decSymbol.newState by <a href="https://github.com/greatroar"><code>@greatroar</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/896">klauspost/compress#896</a></li> <li>zstd: Add Frame header encoding and stripping by <a href="https://github.com/klauspost"><code>@klauspost</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/908">klauspost/compress#908</a></li> <li>zstd: Tweak noasm FSE decoder by <a href="https://github.com/greatroar"><code>@greatroar</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/910">klauspost/compress#910</a></li> <li>s2: Fix callbacks for skippable blocks and disallow 0xfe (Padding) for custom use by <a href="https://github.com/Jille"><code>@Jille</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/916">klauspost/compress#916</a></li> <li>s2: Fix incorrect length encoded by writer.AddSkippableBlock by <a href="https://github.com/Jille"><code>@Jille</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/917">klauspost/compress#917</a></li> <li>s2: Fix up AddSkippableBlock more by <a href="https://github.com/klauspost"><code>@klauspost</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/919">klauspost/compress#919</a></li> <li>s2: Document and test how to peek the stream for skippable blocks by <a href="https://github.com/Jille"><code>@Jille</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/918">klauspost/compress#918</a></li> <li>internal/race,s2: add some race instrumentation by <a href="https://github.com/egonelbre"><code>@egonelbre</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/903">klauspost/compress#903</a></li> <li>build(deps): bump the github-actions group with 4 updates by <a href="https://github.com/dependabot"><code>@dependabot</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/900">klauspost/compress#900</a></li> <li>CI: Hash pin sensitive actions and configure Dependabot to automatically update them by <a href="https://github.com/diogoteles08"><code>@diogoteles08</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/899">klauspost/compress#899</a></li> <li>Update generator and executable go.mod by <a href="https://github.com/klauspost"><code>@klauspost</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/904">klauspost/compress#904</a></li> <li>Update README.md by <a href="https://github.com/pelenium"><code>@pelenium</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/905">klauspost/compress#905</a></li> <li>build(deps): bump the github-actions group with 1 update by <a href="https://github.com/dependabot"><code>@dependabot</code></a> in <a href="https://redirect.github.com/klauspost/compress/pull/906">klauspost/compress#906</a></li> </ul> <h2>New Contributors</h2> <ul> <li><a href="https://github.com/pelenium"><code>@pelenium</code></a> made their first contribution in <a href="https://redirect.github.com/klauspost/compress/pull/905">klauspost/compress#905</a></li> <li><a href="https://github.com/Jille"><code>@Jille</code></a> made their first contribution in <a href="https://redirect.github.com/klauspost/compress/pull/916">klauspost/compress#916</a></li> </ul> <p><strong>Full Changelog</strong>: <a href="https://github.com/klauspost/compress/compare/v1.17.4...v1.17.5">https://github.com/klauspost/compress/compare/v1.17.4...v1.17.5</a></p> </blockquote> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/klauspost/compress/commit/6662a21faa70ec6f4a73856dfc309252d1fad3a6"><code>6662a21</code></a> s2: Document and test how to peek the stream for skippable blocks (<a href="https://redirect.github.com/klauspost/compress/issues/918">#918</a>)</li> <li><a href="https://github.com/klauspost/compress/commit/3deb8783d3f366c0eabe0c53073fd1bf13cc1d6c"><code>3deb878</code></a> s2: Fix up AddSkippableBlock more (<a href="https://redirect.github.com/klauspost/compress/issues/919">#919</a>)</li> <li><a href="https://github.com/klauspost/compress/commit/6ac58c95d96e12fa57c2ff03df9b022daef34f4d"><code>6ac58c9</code></a> s2: Fix incorrect length encoded by writer.AddSkippableBlock (<a href="https://redirect.github.com/klauspost/compress/issues/917">#917</a>)</li> <li><a href="https://github.com/klauspost/compress/commit/515f1535cb6858d21669eb5ba97a3f3cf5753b77"><code>515f153</code></a> s2: Fix callbacks for skippable blocks and disallow 0xfe (Padding) for custom...</li> <li><a href="https://github.com/klauspost/compress/commit/01b2a79f9e8fdaeda055d7e9c19c6d45b677ba12"><code>01b2a79</code></a> zstd: Limit default window to 8MB (<a href="https://redirect.github.com/klauspost/compress/issues/913">#913</a>)</li> <li><a href="https://github.com/klauspost/compress/commit/32312d57f3c73b61c6b4cb5e3eff422fa6263e6d"><code>32312d5</code></a> flate: Fix reset with dictionary on custom window encodes (<a href="https://redirect.github.com/klauspost/compress/issues/912">#912</a>)</li> <li><a href="https://github.com/klauspost/compress/commit/d9b6e1ec4f1bb7e2f52441b91030bd80db608af2"><code>d9b6e1e</code></a> zstd: Tweak noasm FSE decoder (<a href="https://redirect.github.com/klauspost/compress/issues/910">#910</a>)</li> <li><a href="https://github.com/klauspost/compress/commit/85452d2f6200e5e76592ffc4269c45d5ad9d13f3"><code>85452d2</code></a> zstd: Add Frame header encoding and stripping (<a href="https://redirect.github.com/klauspost/compress/issues/908">#908</a>)</li> <li><a href="https://github.com/klauspost/compress/commit/eabf71d7a39b45cf1194f918b181099b27421053"><code>eabf71d</code></a> build(deps): bump the github-actions group with 1 update (<a href="https://redirect.github.com/klauspost/compress/issues/906">#906</a>)</li> <li><a href="https://github.com/klauspost/compress/commit/f6216d378057bcf4b287090b4d7640966680c62d"><code>f6216d3</code></a> Update README.md (<a href="https://redirect.github.com/klauspost/compress/issues/905">#905</a>)</li> <li>Additional commits viewable in <a href="https://github.com/klauspost/compress/compare/v1.17.4...v1.17.5">compare view</a></li> </ul> </details> <br /> [![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=github.com/klauspost/compress&package-manager=go_modules&previous-version=1.17.4&new-version=1.17.5)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) </details> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.
LGTM!
This adds some additional tests to test that the mapping of HTTP code to Connect code is correct for clients according to the protocol: https://connectrpc.com/docs/protocol#http-to-error-code
This also adds usage of
connect.ErrorWriter
to the raw response middleware (although is admittedly maybe no longer needed since the header issue has been fixed).