Skip to content

Commit

Permalink
Merge pull request #1985 from fancysupport/response-type-fix
Browse files Browse the repository at this point in the history
headers were being set after they were written
  • Loading branch information
otoolep committed Mar 18, 2015
2 parents f644ba2 + 24cade1 commit 616cef6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
8 changes: 5 additions & 3 deletions httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ func (h *Handler) serveWrite(w http.ResponseWriter, r *http.Request, user *influ
}

var writeError = func(result influxdb.Result, statusCode int) {
w.WriteHeader(statusCode)
w.Header().Add("content-type", "application/json")
w.WriteHeader(statusCode)
_ = json.NewEncoder(w).Encode(&result)
return
}
Expand Down Expand Up @@ -393,8 +393,8 @@ func (h *Handler) serveCreateDataNode(w http.ResponseWriter, r *http.Request) {
node := h.server.DataNodeByURL(u)

// Write new node back to client.
w.WriteHeader(http.StatusCreated)
w.Header().Add("content-type", "application/json")
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(&dataNodeJSON{ID: node.ID, URL: node.URL.String()})
}

Expand Down Expand Up @@ -450,6 +450,8 @@ func isFieldNotFoundError(err error) bool {

// httpResult writes a Results array to the client.
func httpResults(w http.ResponseWriter, results influxdb.Results, pretty bool) {
w.Header().Add("content-type", "application/json")

if results.Error() != nil {
if isAuthorizationError(results.Error()) {
w.WriteHeader(http.StatusUnauthorized)
Expand All @@ -462,7 +464,7 @@ func httpResults(w http.ResponseWriter, results influxdb.Results, pretty bool) {
w.WriteHeader(http.StatusInternalServerError)
}
}
w.Header().Add("content-type", "application/json")

var b []byte
if pretty {
b, _ = json.MarshalIndent(results, "", " ")
Expand Down
87 changes: 87 additions & 0 deletions httpd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
c := test.NewMessagingClient()
defer c.Close()
Expand Down

0 comments on commit 616cef6

Please sign in to comment.