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

[query] Fix incorrect content type in m3query/ error response #2917

Merged
merged 4 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cmd/tools/dtest/docker/harness/carbon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

func findVerifier(expected string) resources.ResponseVerifier {
return func(status int, s string, err error) error {
return func(status int, _ map[string][]string, s string, err error) error {
if err != nil {
return err
}
Expand All @@ -55,7 +55,7 @@ func renderVerifier(v float64) resources.ResponseVerifier {
Datapoints [][]float64 `json:"datapoints"`
}

return func(status int, s string, err error) error {
return func(status int, _ map[string][]string, s string, err error) error {
if err != nil {
return err
}
Expand Down
11 changes: 8 additions & 3 deletions src/cmd/tools/dtest/docker/harness/query_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func testInvalidQueryReturns400(t *testing.T, tests []urlTest) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.NoError(t, coord.RunQuery(verifyStatus(400), tt.url), "for query '%v'", tt.url)
assert.NoError(t, coord.RunQuery(verifyResponse(400), tt.url), "for query '%v'", tt.url)
})
}
}
Expand Down Expand Up @@ -129,8 +129,8 @@ func queryString(params map[string]string) string {
return strings.Join(p, "&")
}

func verifyStatus(expectedStatus int) resources.ResponseVerifier {
return func(status int, resp string, err error) error {
func verifyResponse(expectedStatus int) resources.ResponseVerifier {
return func(status int, headers map[string][]string, resp string, err error) error {
if err != nil {
return err
}
Expand All @@ -139,6 +139,11 @@ func verifyStatus(expectedStatus int) resources.ResponseVerifier {
return fmt.Errorf("expeceted %v status code, got %v", expectedStatus, status)
}

contentType := headers["Content-Type"][0]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please make this safer with value, ok := map[key], and also check the length of slice before accessing it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

if contentType != "application/json" {
return fmt.Errorf("expected json content type, got %v", contentType)
}

return nil
}
}
4 changes: 2 additions & 2 deletions src/cmd/tools/dtest/docker/harness/resources/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var (
)

// ResponseVerifier is a function that checks if the query response is valid.
type ResponseVerifier func(int, string, error) error
type ResponseVerifier func(int, map[string][]string, string, error) error

// Coordinator is a wrapper for a coordinator. It provides a wrapper on HTTP
// endpoints that expose cluster management APIs as well as read and write
Expand Down Expand Up @@ -363,7 +363,7 @@ func (c *coordinator) query(
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)

return verifier(resp.StatusCode, string(b), err)
return verifier(resp.StatusCode, resp.Header, string(b), err)
}

func (c *coordinator) RunQuery(
Expand Down
29 changes: 17 additions & 12 deletions src/x/net/http/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,30 @@ func WriteError(w http.ResponseWriter, err error, opts ...WriteErrorOption) {
fn(&o)
}

statusCode := getStatusCode(err)
if o.response == nil {
linasm marked this conversation as resolved.
Show resolved Hide resolved
w.Header().Set(HeaderContentType, ContentTypeJSON)
w.WriteHeader(statusCode)
Comment on lines +97 to +98
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why in one case it is Header().Set(....), and in another - WriteHeader(...), what is the difference here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

WriteHeader is deceiving, it means write all the headers and a status code. Header().Set is preparing a single header to be written when WriteHeader is called.

json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
} else {
w.WriteHeader(statusCode)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: looks like statusCode gets set in both branches, can probably move it outside?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The order of ResponseWriter method calls is important. From the docs:

<...>
Changing the header map after a call to WriteHeader (or Write) has no effect unless the modified headers are trailers.
<...>
If WriteHeader is not called explicitly, the first call to Write will trigger an implicit WriteHeader(http.StatusOK).
<...>

It could be done, but either two ifs on o.response == nil would be needed (one before WriteHeader() and one after), or introducing defers for Encode/Write calls.

w.Write(o.response)
}
}

func getStatusCode(err error) int {
switch v := err.(type) {
case Error:
w.WriteHeader(v.Code())
return v.Code()
case error:
if xerrors.IsInvalidParams(v) {
w.WriteHeader(http.StatusBadRequest)
return http.StatusBadRequest
} else if errors.Is(err, context.DeadlineExceeded) {
w.WriteHeader(http.StatusGatewayTimeout)
return http.StatusGatewayTimeout
} else {
w.WriteHeader(http.StatusInternalServerError)
return http.StatusInternalServerError
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I guess you can avoid both this else and the default case and just return the StatusInternalServerError at the end of the function.

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

}
default:
w.WriteHeader(http.StatusInternalServerError)
return http.StatusInternalServerError
}

if o.response != nil {
w.Write(o.response)
return
}

json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
}