2016-12-11 14:43:11 +01:00
|
|
|
package api
|
|
|
|
|
2016-12-11 14:50:01 +01:00
|
|
|
import (
|
2017-01-15 16:12:14 +01:00
|
|
|
"encoding/json"
|
2016-12-11 14:50:01 +01:00
|
|
|
"net/http"
|
2017-01-15 16:12:14 +01:00
|
|
|
"net/http/httptest"
|
2016-12-11 14:50:01 +01:00
|
|
|
"testing"
|
2016-12-11 14:43:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetRequestedLimit(t *testing.T) {
|
2016-12-11 14:50:01 +01:00
|
|
|
r, _ := http.NewRequest("GET", "", nil)
|
|
|
|
limit := getRequestedLimit(r)
|
|
|
|
|
|
|
|
if limit != defaultLimit {
|
|
|
|
t.Errorf("Expected limit of %d does not match %d", defaultLimit, limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, _ = http.NewRequest("GET", "?limit=50", nil)
|
|
|
|
limit = getRequestedLimit(r)
|
|
|
|
if limit != 50 {
|
|
|
|
t.Errorf("Expected limit of %d does not match %d", defaultLimit, limit)
|
|
|
|
}
|
2016-12-11 14:43:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRequestedPeriods(t *testing.T) {
|
2016-12-11 14:50:01 +01:00
|
|
|
r, _ := http.NewRequest("GET", "?before=500&after=100", nil)
|
|
|
|
before, after := getRequestedPeriods(r)
|
2016-12-11 14:43:11 +01:00
|
|
|
|
2016-12-11 14:50:01 +01:00
|
|
|
if before != 500 || after != 100 {
|
|
|
|
t.Error("Expected URl argument for `before` or `after` does not match")
|
|
|
|
}
|
2016-12-11 14:43:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRequestIp(t *testing.T) {
|
2016-12-11 14:50:01 +01:00
|
|
|
// test X-Forwarded-For header
|
|
|
|
ipAddress := "192.168.1.2"
|
|
|
|
r, _ := http.NewRequest("GET", "", nil)
|
|
|
|
r.Header.Set("X-Forwarded-For", ipAddress)
|
|
|
|
result := getRequestIp(r)
|
|
|
|
|
|
|
|
if result != ipAddress {
|
|
|
|
t.Errorf("Expected IP address of %s does not match %s", ipAddress, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test RemoteAddr prop
|
|
|
|
r, _ = http.NewRequest("GET", "", nil)
|
|
|
|
r.RemoteAddr = ipAddress
|
|
|
|
result = getRequestIp(r)
|
|
|
|
if result != ipAddress {
|
|
|
|
t.Errorf("Expected IP address of %s does not match %s", ipAddress, result)
|
|
|
|
}
|
2016-12-11 14:43:11 +01:00
|
|
|
}
|
2016-12-11 15:33:23 +01: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 16:12:14 +01: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|