2016-12-11 13:43:11 +00:00
|
|
|
package api
|
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
import (
|
2017-01-15 15:12:14 +00:00
|
|
|
"encoding/json"
|
2016-12-11 13:50:01 +00:00
|
|
|
"net/http"
|
2017-01-15 15:12:14 +00:00
|
|
|
"net/http/httptest"
|
2016-12-11 13:50:01 +00:00
|
|
|
"testing"
|
2018-05-08 06:22:21 +00:00
|
|
|
"time"
|
2016-12-11 13:43:11 +00:00
|
|
|
)
|
|
|
|
|
2018-05-07 16:59:52 +00:00
|
|
|
func TestGetRequestParams(t *testing.T) {
|
2018-05-08 06:22:21 +00:00
|
|
|
startDate := time.Now().AddDate(0, 0, -12)
|
|
|
|
endDate := time.Now().AddDate(0, 0, -5)
|
|
|
|
|
|
|
|
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))
|
|
|
|
params := GetRequestParams(r)
|
|
|
|
|
|
|
|
if params.Limit != 50 {
|
|
|
|
t.Errorf("Expected %#v, got %#v", 50, params.Limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
if startDate != params.StartDate {
|
|
|
|
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 {
|
|
|
|
t.Errorf("Expected %#v, got %#v", endDate.Format("2006-01-02 15:04"), params.EndDate.Format("2006-01-02 15:04"))
|
|
|
|
}
|
|
|
|
|
2016-12-11 13:43:11 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 14:33:23 +00:00
|
|
|
func TestParseMajorMinor(t *testing.T) {
|
|
|
|
actual := parseMajorMinor("50.0.0")
|
|
|
|
expected := "50.0"
|
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("Return value should be %s, is %s instead", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = parseMajorMinor("1.1")
|
|
|
|
expected = "1.1"
|
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("Return value should be %s is %s instead", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2017-01-15 15:12:14 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|