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

@ -5,35 +5,36 @@
package server package server
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/pushrax/chihaya/config" "github.com/pushrax/chihaya/config"
) )
type stats struct { type stats struct {
Uptime config.Duration `json:"uptime"` Uptime config.Duration `json:"uptime"`
RPM int64 `json:"req_per_min"` RPM int64 `json:"req_per_min"`
} }
func (s *Server) serveStats(w http.ResponseWriter, r *http.Request) { func (s *Server) serveStats(w http.ResponseWriter, r *http.Request) {
stats, _ := json.Marshal(&stats{ w.Header().Set("Content-Type", "application/json")
config.Duration{time.Now().Sub(s.startTime)}, w.Header().Set("Connection", "close")
s.rpm,
})
length, _ := w.Write(stats) stats, _ := json.Marshal(&stats{
w.Header().Set("Content-Type", "application/json") config.Duration{time.Now().Sub(s.startTime)},
w.Header().Set("Content-Length", string(length)) s.rpm,
w.Header().Set("Connection", "close") })
w.(http.Flusher).Flush()
length, _ := w.Write(stats)
w.Header().Set("Content-Length", string(length))
w.(http.Flusher).Flush()
} }
func (s *Server) updateStats() { func (s *Server) updateStats() {
for _ = range time.NewTicker(time.Minute).C { for _ = range time.NewTicker(time.Minute).C {
s.rpm = s.deltaRequests s.rpm = s.deltaRequests
atomic.StoreInt64(&s.deltaRequests, 0) atomic.StoreInt64(&s.deltaRequests, 0)
} }
} }

View file

@ -5,44 +5,51 @@
package server package server
import ( import (
"errors" "errors"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"testing" "testing"
"github.com/pushrax/chihaya/config" "github.com/pushrax/chihaya/config"
_ "github.com/pushrax/chihaya/storage/backend/batter" _ "github.com/pushrax/chihaya/storage/backend/batter"
_ "github.com/pushrax/chihaya/storage/tracker/redis" _ "github.com/pushrax/chihaya/storage/tracker/redis"
) )
func newTestServer() (*Server, error) { func newTestServer() (*Server, error) {
testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH")) testConfig, err := config.Open(os.Getenv("TESTCONFIGPATH"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
s, err := New(testConfig) s, err := New(testConfig)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return s, nil return s, nil
} }
func TestStats(t *testing.T) { func TestStats(t *testing.T) {
s, err := newTestServer() s, err := newTestServer()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
r, err := http.NewRequest("GET", "127.0.0.1:80/stats", nil)
if err != nil { r, err := http.NewRequest("GET", "127.0.0.1:80/stats", nil)
t.Error(err) if err != nil {
} t.Error(err)
w := httptest.NewRecorder() }
s.serveStats(w, r)
if w.Code != 200 { w := httptest.NewRecorder()
t.Error(errors.New("/stats did not return HTTP 200")) 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"))
}
} }