Add correct content type to query responses. Fixes #1873

This commit is contained in:
Cody Soyland 2019-04-10 14:28:18 -05:00
parent 8041a9fa5f
commit 7bb6fdffcb
2 changed files with 7 additions and 1 deletions

View file

@ -964,10 +964,12 @@ func (h *Handler) readURLQueryRequest(r *http.Request) (*pilosa.QueryRequest, er
}
// writeQueryResponse writes the response from the executor to w.
func (h *Handler) writeQueryResponse(w io.Writer, r *http.Request, resp *pilosa.QueryResponse) error {
func (h *Handler) writeQueryResponse(w http.ResponseWriter, r *http.Request, resp *pilosa.QueryResponse) error {
if !validHeaderAcceptJSON(r.Header) {
w.Header().Set("Content-Type", "application/protobuf")
return h.writeProtobufQueryResponse(w, resp)
}
w.Header().Set("Content-Type", "application/json")
return h.writeJSONQueryResponse(w, resp)
}

View file

@ -248,6 +248,8 @@ func TestHandler_Endpoints(t *testing.T) {
t.Fatalf("unexpected status code: %d", w.Code)
} else if body := w.Body.String(); body != `{"results":[2]}`+"\n" {
t.Fatalf("unexpected body: %q", body)
} else if w.Header().Get("Content-Type") != "application/json" {
t.Fatalf("unexpected header: %q", w.Header().Get("Content-Type"))
}
})
@ -286,6 +288,8 @@ func TestHandler_Endpoints(t *testing.T) {
t.Fatal(err)
} else if rt, ok := resp.Results[0].(uint64); !ok || rt != 3 {
t.Fatalf("unexpected response type: %#v", resp.Results[0])
} else if w.Header().Get("Content-Type") != "application/protobuf" {
t.Fatalf("unexpected header: %q", w.Header().Get("Content-Type"))
}
})