diff --git a/pkg/api/http_test.go b/pkg/api/http_test.go new file mode 100644 index 0000000..df9b000 --- /dev/null +++ b/pkg/api/http_test.go @@ -0,0 +1,25 @@ +package api + +import ( + "encoding/json" + "net/http/httptest" + "testing" +) + +func TestRespond(t *testing.T) { + w := httptest.NewRecorder() + respond(w, 15) + + // assert json header + if w.Header().Get("Content-Type") != "application/json" { + t.Errorf("Invalid Content-Type header") + } + + // assert json response + var d int + err := json.NewDecoder(w.Body).Decode(&d) + if err != nil { + t.Errorf("Invalid response body: %s", err) + } + +} diff --git a/pkg/api/params.go b/pkg/api/params.go index f2236e6..aea9164 100644 --- a/pkg/api/params.go +++ b/pkg/api/params.go @@ -7,12 +7,14 @@ import ( "time" ) +// Params defines the commonly used API parameters type Params struct { Limit int StartDate time.Time EndDate time.Time } +// GetRequestParams parses the query parameters and returns commonly used API parameters, with defaults func GetRequestParams(r *http.Request) *Params { params := &Params{ Limit: 20, diff --git a/pkg/api/params_test.go b/pkg/api/params_test.go index ad79ef1..ee50cd5 100644 --- a/pkg/api/params_test.go +++ b/pkg/api/params_test.go @@ -1,9 +1,8 @@ package api import ( - "encoding/json" + "fmt" "net/http" - "net/http/httptest" "testing" "time" ) @@ -11,22 +10,21 @@ import ( func TestGetRequestParams(t *testing.T) { startDate := time.Now().AddDate(0, 0, -12) endDate := time.Now().AddDate(0, 0, -5) + limit := 50 - r, _ := http.NewRequest("GET", "/", nil) - r.URL.Query().Add("before", string(endDate.Unix())) - r.URL.Query().Add("after", string(startDate.Unix())) - r.URL.Query().Add("limit", string(50)) + url := fmt.Sprintf("/?after=%d&before=%d&limit=%d", startDate.Unix(), endDate.Unix(), limit) + r, _ := http.NewRequest("GET", url, nil) params := GetRequestParams(r) if params.Limit != 50 { t.Errorf("Expected %#v, got %#v", 50, params.Limit) } - if startDate != params.StartDate { + if startDate.Unix() != params.StartDate.Unix() { t.Errorf("Expected %#v, got %#v", startDate.Format("2006-01-02 15:04"), params.StartDate.Format("2006-01-02 15:04")) } - if params.EndDate != endDate { + if params.EndDate.Unix() != endDate.Unix() { t.Errorf("Expected %#v, got %#v", endDate.Format("2006-01-02 15:04"), params.EndDate.Format("2006-01-02 15:04")) } @@ -45,21 +43,3 @@ func TestParseMajorMinor(t *testing.T) { t.Errorf("Return value should be %s is %s instead", expected, actual) } } - -func TestRespond(t *testing.T) { - w := httptest.NewRecorder() - respond(w, 15) - - // assert json header - if w.Header().Get("Content-Type") != "application/json" { - t.Errorf("Invalid Content-Type header") - } - - // assert json response - var d int - err := json.NewDecoder(w.Body).Decode(&d) - if err != nil { - t.Errorf("Invalid response body: %s", err) - } - -} diff --git a/pkg/models/site_stats.go b/pkg/models/site_stats.go index f4ae6b7..4341ca6 100644 --- a/pkg/models/site_stats.go +++ b/pkg/models/site_stats.go @@ -10,5 +10,5 @@ type SiteStats struct { Sessions int64 `db:"sessions"` Bounces int64 `db:"bounces"` AvgDuration int64 `db:"avg_duration"` - Date time.Time `db:"date"` + Date time.Time `db:"date" json:"omitempty"` }