From 86f8199bef2a34c729d95bd3d9601b9024886f31 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Sat, 23 Nov 2013 18:04:23 -0500 Subject: [PATCH] Fix Content-Type Header not improperly being set for stats --- server/stats.go | 41 ++++++++++++++------------- server/stats_test.go | 67 ++++++++++++++++++++++++-------------------- 2 files changed, 58 insertions(+), 50 deletions(-) diff --git a/server/stats.go b/server/stats.go index b448368..19d577d 100644 --- a/server/stats.go +++ b/server/stats.go @@ -5,35 +5,36 @@ package server import ( - "encoding/json" - "net/http" - "sync/atomic" - "time" + "encoding/json" + "net/http" + "sync/atomic" + "time" - "github.com/pushrax/chihaya/config" + "github.com/pushrax/chihaya/config" ) type stats struct { - Uptime config.Duration `json:"uptime"` - RPM int64 `json:"req_per_min"` + Uptime config.Duration `json:"uptime"` + RPM int64 `json:"req_per_min"` } func (s *Server) serveStats(w http.ResponseWriter, r *http.Request) { - stats, _ := json.Marshal(&stats{ - config.Duration{time.Now().Sub(s.startTime)}, - s.rpm, - }) + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Connection", "close") - length, _ := w.Write(stats) - w.Header().Set("Content-Type", "application/json") - w.Header().Set("Content-Length", string(length)) - w.Header().Set("Connection", "close") - w.(http.Flusher).Flush() + stats, _ := json.Marshal(&stats{ + config.Duration{time.Now().Sub(s.startTime)}, + s.rpm, + }) + + length, _ := w.Write(stats) + w.Header().Set("Content-Length", string(length)) + w.(http.Flusher).Flush() } func (s *Server) updateStats() { - for _ = range time.NewTicker(time.Minute).C { - s.rpm = s.deltaRequests - atomic.StoreInt64(&s.deltaRequests, 0) - } + for _ = range time.NewTicker(time.Minute).C { + s.rpm = s.deltaRequests + atomic.StoreInt64(&s.deltaRequests, 0) + } } diff --git a/server/stats_test.go b/server/stats_test.go index 29b44e7..d0fb0eb 100644 --- a/server/stats_test.go +++ b/server/stats_test.go @@ -5,44 +5,51 @@ package server import ( - "errors" - "net/http" - "net/http/httptest" - "os" - "testing" + "errors" + "net/http" + "net/http/httptest" + "os" + "testing" - "github.com/pushrax/chihaya/config" + "github.com/pushrax/chihaya/config" - _ "github.com/pushrax/chihaya/storage/backend/batter" - _ "github.com/pushrax/chihaya/storage/tracker/redis" + _ "github.com/pushrax/chihaya/storage/backend/batter" + _ "github.com/pushrax/chihaya/storage/tracker/redis" ) func newTestServer() (*Server, error) { - testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH")) - if err != nil { - return nil, err - } + testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH")) + if err != nil { + return nil, err + } - s, err := New(testConfig) - if err != nil { - return nil, err - } + s, err := New(testConfig) + if err != nil { + return nil, err + } - return s, nil + return s, nil } func TestStats(t *testing.T) { - s, err := newTestServer() - if err != nil { - t.Error(err) - } - r, err := http.NewRequest("GET", "127.0.0.1:80/stats", nil) - if err != nil { - t.Error(err) - } - w := httptest.NewRecorder() - s.serveStats(w, r) - if w.Code != 200 { - t.Error(errors.New("/stats did not return HTTP 200")) - } + s, err := newTestServer() + if err != nil { + t.Error(err) + } + + r, err := http.NewRequest("GET", "127.0.0.1:80/stats", nil) + if err != nil { + t.Error(err) + } + + w := httptest.NewRecorder() + s.serveStats(w, r) + + if w.Code != 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")) + } }