Merge pull request #1985 from fancysupport/response-type-fix

headers were being set after they were written
pull/1982/merge
Philip O'Toole 2015-03-18 11:38:57 -07:00
commit 616cef62e6
2 changed files with 92 additions and 3 deletions

View File

@ -202,8 +202,8 @@ func (h *Handler) serveWrite(w http.ResponseWriter, r *http.Request, user *influ
} }
var writeError = func(result influxdb.Result, statusCode int) { var writeError = func(result influxdb.Result, statusCode int) {
w.WriteHeader(statusCode)
w.Header().Add("content-type", "application/json") w.Header().Add("content-type", "application/json")
w.WriteHeader(statusCode)
_ = json.NewEncoder(w).Encode(&result) _ = json.NewEncoder(w).Encode(&result)
return return
} }
@ -393,8 +393,8 @@ func (h *Handler) serveCreateDataNode(w http.ResponseWriter, r *http.Request) {
node := h.server.DataNodeByURL(u) node := h.server.DataNodeByURL(u)
// Write new node back to client. // Write new node back to client.
w.WriteHeader(http.StatusCreated)
w.Header().Add("content-type", "application/json") w.Header().Add("content-type", "application/json")
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(&dataNodeJSON{ID: node.ID, URL: node.URL.String()}) _ = json.NewEncoder(w).Encode(&dataNodeJSON{ID: node.ID, URL: node.URL.String()})
} }
@ -450,6 +450,8 @@ func isFieldNotFoundError(err error) bool {
// httpResult writes a Results array to the client. // httpResult writes a Results array to the client.
func httpResults(w http.ResponseWriter, results influxdb.Results, pretty bool) { func httpResults(w http.ResponseWriter, results influxdb.Results, pretty bool) {
w.Header().Add("content-type", "application/json")
if results.Error() != nil { if results.Error() != nil {
if isAuthorizationError(results.Error()) { if isAuthorizationError(results.Error()) {
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)
@ -462,7 +464,7 @@ func httpResults(w http.ResponseWriter, results influxdb.Results, pretty bool) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
} }
} }
w.Header().Add("content-type", "application/json")
var b []byte var b []byte
if pretty { if pretty {
b, _ = json.MarshalIndent(results, "", " ") b, _ = json.MarshalIndent(results, "", " ")

View File

@ -1217,6 +1217,93 @@ func TestHandler_serveWriteSeries_noDatabaseExists(t *testing.T) {
} }
} }
func TestHandler_serveWriteSeries_errorHasJsonContentType(t *testing.T) {
c := test.NewMessagingClient()
defer c.Close()
srvr := OpenAuthlessServer(c)
s := NewHTTPServer(srvr)
defer s.Close()
client := &http.Client{}
req, err := http.NewRequest("POST", s.URL+`/write`, bytes.NewBufferString("{}"))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept-Encoding", "gzip")
resp, err := client.Do(req)
if err != nil {
panic(err)
}
if ct := resp.Header.Get("Content-Type"); ct != "application/json" {
t.Fatalf("unexpected Content-Type. expected %q, actual: %q", "application/json", ct)
}
}
func TestHandler_serveWriteSeries_queryHasJsonContentType(t *testing.T) {
c := test.NewMessagingClient()
defer c.Close()
srvr := OpenAuthlessServer(c)
srvr.CreateDatabase("foo")
srvr.CreateRetentionPolicy("foo", influxdb.NewRetentionPolicy("bar"))
srvr.SetDefaultRetentionPolicy("foo", "bar")
s := NewHTTPServer(srvr)
defer s.Close()
status, _ := MustHTTP("POST", s.URL+`/write`, nil, nil, `{"database" : "foo", "retentionPolicy" : "bar", "points": [{"name": "cpu", "tags": {"host": "server01"},"timestamp": "2009-11-10T23:00:00Z", "fields": {"value": 100}}]}`)
if status != http.StatusOK {
t.Fatalf("unexpected status: %d", status)
}
time.Sleep(100 * time.Millisecond) // Ensure data node picks up write.
srvr.Restart() // Ensure data is queryable across restarts.
client := &http.Client{}
params := url.Values{}
params.Add("db", "foo")
params.Add("q", "select * from cpu")
req, err := http.NewRequest("GET", s.URL+`/query?`+params.Encode(), bytes.NewBufferString(""))
if err != nil {
panic(err)
}
req.Header.Set("Accept-Encoding", "gzip")
resp, err := client.Do(req)
if err != nil {
panic(err)
}
if ct := resp.Header.Get("Content-Type"); ct != "application/json" {
t.Fatalf("unexpected Content-Type. expected %q, actual: %q", "application/json", ct)
}
// now test a query error
params.Del("db")
req_error, err := http.NewRequest("GET", s.URL+`/query?`+params.Encode(), bytes.NewBufferString(""))
if err != nil {
panic(err)
}
req_error.Header.Set("Accept-Encoding", "gzip")
resp_error, err := client.Do(req_error)
if err != nil {
panic(err)
}
if cte := resp_error.Header.Get("Content-Type"); cte != "application/json" {
t.Fatalf("unexpected Content-Type. expected %q, actual: %q", "application/json", cte)
}
}
func TestHandler_serveWriteSeries_invalidJSON(t *testing.T) { func TestHandler_serveWriteSeries_invalidJSON(t *testing.T) {
c := test.NewMessagingClient() c := test.NewMessagingClient()
defer c.Close() defer c.Close()