Fix Content-Type Header not improperly being set for stats

This commit is contained in:
Jimmy Zelinskie 2013-11-23 18:04:23 -05:00
parent 493d40da3d
commit 86f8199bef
2 changed files with 58 additions and 50 deletions

View file

@ -19,15 +19,16 @@ type stats struct {
} }
func (s *Server) serveStats(w http.ResponseWriter, r *http.Request) { func (s *Server) serveStats(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Connection", "close")
stats, _ := json.Marshal(&stats{ stats, _ := json.Marshal(&stats{
config.Duration{time.Now().Sub(s.startTime)}, config.Duration{time.Now().Sub(s.startTime)},
s.rpm, s.rpm,
}) })
length, _ := w.Write(stats) length, _ := w.Write(stats)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", string(length)) w.Header().Set("Content-Length", string(length))
w.Header().Set("Connection", "close")
w.(http.Flusher).Flush() w.(http.Flusher).Flush()
} }

View file

@ -36,13 +36,20 @@ func TestStats(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
r, err := http.NewRequest("GET", "127.0.0.1:80/stats", nil) r, err := http.NewRequest("GET", "127.0.0.1:80/stats", nil)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
w := httptest.NewRecorder() w := httptest.NewRecorder()
s.serveStats(w, r) s.serveStats(w, r)
if w.Code != 200 { if w.Code != 200 {
t.Error(errors.New("/stats did not return HTTP 200")) t.Error(errors.New("/stats did not return HTTP 200"))
} }
if w.Header()["Content-Type"][0] != "application/json" {
t.Error(errors.New("/stats did not return the proper Content-Type header"))
}
} }