mirror of https://github.com/status-im/fathom.git
fix params test
This commit is contained in:
parent
8e99bc5896
commit
ca3d258174
|
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,12 +7,14 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Params defines the commonly used API parameters
|
||||||
type Params struct {
|
type Params struct {
|
||||||
Limit int
|
Limit int
|
||||||
StartDate time.Time
|
StartDate time.Time
|
||||||
EndDate time.Time
|
EndDate time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetRequestParams parses the query parameters and returns commonly used API parameters, with defaults
|
||||||
func GetRequestParams(r *http.Request) *Params {
|
func GetRequestParams(r *http.Request) *Params {
|
||||||
params := &Params{
|
params := &Params{
|
||||||
Limit: 20,
|
Limit: 20,
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -11,22 +10,21 @@ import (
|
||||||
func TestGetRequestParams(t *testing.T) {
|
func TestGetRequestParams(t *testing.T) {
|
||||||
startDate := time.Now().AddDate(0, 0, -12)
|
startDate := time.Now().AddDate(0, 0, -12)
|
||||||
endDate := time.Now().AddDate(0, 0, -5)
|
endDate := time.Now().AddDate(0, 0, -5)
|
||||||
|
limit := 50
|
||||||
|
|
||||||
r, _ := http.NewRequest("GET", "/", nil)
|
url := fmt.Sprintf("/?after=%d&before=%d&limit=%d", startDate.Unix(), endDate.Unix(), limit)
|
||||||
r.URL.Query().Add("before", string(endDate.Unix()))
|
r, _ := http.NewRequest("GET", url, nil)
|
||||||
r.URL.Query().Add("after", string(startDate.Unix()))
|
|
||||||
r.URL.Query().Add("limit", string(50))
|
|
||||||
params := GetRequestParams(r)
|
params := GetRequestParams(r)
|
||||||
|
|
||||||
if params.Limit != 50 {
|
if params.Limit != 50 {
|
||||||
t.Errorf("Expected %#v, got %#v", 50, params.Limit)
|
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"))
|
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"))
|
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)
|
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -10,5 +10,5 @@ type SiteStats struct {
|
||||||
Sessions int64 `db:"sessions"`
|
Sessions int64 `db:"sessions"`
|
||||||
Bounces int64 `db:"bounces"`
|
Bounces int64 `db:"bounces"`
|
||||||
AvgDuration int64 `db:"avg_duration"`
|
AvgDuration int64 `db:"avg_duration"`
|
||||||
Date time.Time `db:"date"`
|
Date time.Time `db:"date" json:"omitempty"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue