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

headers were being set after they were written #1985

Merged
merged 2 commits into from
Mar 18, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -449,6 +449,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 @@ -461,7 +463,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 @@ -1192,6 +1192,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