|
@@ -2,6 +2,10 @@ package utils
|
|
|
|
|
|
import (
|
|
import (
|
|
"errors"
|
|
"errors"
|
|
|
|
+ "fmt"
|
|
|
|
+ "net/http"
|
|
|
|
+ "net/http/httptest"
|
|
|
|
+ "strings"
|
|
"testing"
|
|
"testing"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
@@ -10,8 +14,8 @@ import (
|
|
|
|
|
|
func Expect(t *testing.T, actual, expect interface{}) {
|
|
func Expect(t *testing.T, actual, expect interface{}) {
|
|
if actual != expect {
|
|
if actual != expect {
|
|
- t.Fatalf("\033[0;33mExpected \033[0;32m`%v`\033[0;33m but got \033[0;31m`%v`\033[0m",
|
|
|
|
- expect, actual)
|
|
|
|
|
|
+ t.Fatalf("\033[0;33mExpected \033[0;32m`(%T) %v`\033[0;33m but got \033[0;31m`(%T) %v`\033[0m",
|
|
|
|
+ expect, expect, actual, actual)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -128,19 +132,57 @@ func TestGetCurrentUnixTimestamp(t *testing.T) {
|
|
}
|
|
}
|
|
|
|
|
|
func TestSystemRenderTemplate(t *testing.T) {
|
|
func TestSystemRenderTemplate(t *testing.T) {
|
|
- //
|
|
|
|
|
|
+ request, err := http.NewRequest("GET", "/", nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+ recorder := httptest.NewRecorder()
|
|
|
|
+ http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ SystemRenderTemplate(w, []byte(`ok`), nil)
|
|
|
|
+ }).ServeHTTP(recorder, request)
|
|
|
|
+ Expect(t, recorder.Code, 200)
|
|
|
|
+ Expect(t, recorder.Body.String(), `ok`)
|
|
}
|
|
}
|
|
|
|
|
|
func TestSystemErrorPageEngine(t *testing.T) {
|
|
func TestSystemErrorPageEngine(t *testing.T) {
|
|
- //
|
|
|
|
|
|
+ request, err := http.NewRequest("GET", "/", nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+ recorder := httptest.NewRecorder()
|
|
|
|
+ http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ SystemErrorPageEngine(w, errors.New("Test error"))
|
|
|
|
+ }).ServeHTTP(recorder, request)
|
|
|
|
+ Expect(t, recorder.Code, http.StatusInternalServerError)
|
|
|
|
+ Expect(t, strings.Contains(recorder.Body.String(), "Engine Error"), true)
|
|
|
|
+ Expect(t, strings.Contains(recorder.Body.String(), "Test error"), true)
|
|
}
|
|
}
|
|
|
|
|
|
func TestSystemErrorPageTemplate(t *testing.T) {
|
|
func TestSystemErrorPageTemplate(t *testing.T) {
|
|
- //
|
|
|
|
|
|
+ request, err := http.NewRequest("GET", "/", nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+ recorder := httptest.NewRecorder()
|
|
|
|
+ http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ SystemErrorPageTemplate(w, errors.New("Test error"))
|
|
|
|
+ }).ServeHTTP(recorder, request)
|
|
|
|
+ Expect(t, recorder.Code, http.StatusInternalServerError)
|
|
|
|
+ Expect(t, strings.Contains(recorder.Body.String(), "Template Error"), true)
|
|
|
|
+ Expect(t, strings.Contains(recorder.Body.String(), "Test error"), true)
|
|
}
|
|
}
|
|
|
|
|
|
func TestSystemErrorPage404(t *testing.T) {
|
|
func TestSystemErrorPage404(t *testing.T) {
|
|
- //
|
|
|
|
|
|
+ request, err := http.NewRequest("GET", "/", nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+ recorder := httptest.NewRecorder()
|
|
|
|
+ http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ SystemErrorPage404(w)
|
|
|
|
+ }).ServeHTTP(recorder, request)
|
|
|
|
+ Expect(t, recorder.Code, http.StatusNotFound)
|
|
|
|
+ Expect(t, strings.Contains(recorder.Body.String(), "404 Not Found"), true)
|
|
}
|
|
}
|
|
|
|
|
|
func TestUrlToArray(t *testing.T) {
|
|
func TestUrlToArray(t *testing.T) {
|
|
@@ -169,6 +211,10 @@ func TestIntToStr(t *testing.T) {
|
|
Expect(t, IntToStr(2000), "2000")
|
|
Expect(t, IntToStr(2000), "2000")
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func TestInt64ToStr(t *testing.T) {
|
|
|
|
+ Expect(t, Int64ToStr(2000), "2000")
|
|
|
|
+}
|
|
|
|
+
|
|
func TestStrToInt(t *testing.T) {
|
|
func TestStrToInt(t *testing.T) {
|
|
Expect(t, StrToInt("2000"), 2000)
|
|
Expect(t, StrToInt("2000"), 2000)
|
|
Expect(t, StrToInt("string"), 0)
|
|
Expect(t, StrToInt("string"), 0)
|
|
@@ -217,3 +263,49 @@ func TestInArrayInt(t *testing.T) {
|
|
Expect(t, InArrayInt(slice, 2), false)
|
|
Expect(t, InArrayInt(slice, 2), false)
|
|
Expect(t, InArrayInt(slice, 8), false)
|
|
Expect(t, InArrayInt(slice, 8), false)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func TestInArrayString(t *testing.T) {
|
|
|
|
+ slice := []string{"1", "3", "5", "9", "0"}
|
|
|
|
+ Expect(t, InArrayString(slice, "1"), true)
|
|
|
|
+ Expect(t, InArrayString(slice, "9"), true)
|
|
|
|
+ Expect(t, InArrayString(slice, "2"), false)
|
|
|
|
+ Expect(t, InArrayString(slice, "8"), false)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestGetPostArrayInt(t *testing.T) {
|
|
|
|
+ request, err := http.NewRequest("POST", "/", strings.NewReader("cats[]=1&cats[]=3&cats[]=5"))
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+ request.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
|
|
|
|
+ request.ParseForm()
|
|
|
|
+ arr := GetPostArrayInt("cats[]", request)
|
|
|
|
+ Expect(t, fmt.Sprintf("%T%v", arr, arr), "[]int[1 3 5]")
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestGetPostArrayString(t *testing.T) {
|
|
|
|
+ request, err := http.NewRequest("POST", "/", strings.NewReader("cats[]=1&cats[]=3&cats[]=5"))
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+ request.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
|
|
|
|
+ request.ParseForm()
|
|
|
|
+ arr := GetPostArrayString("cats[]", request)
|
|
|
|
+ Expect(t, fmt.Sprintf("%T%v", arr, arr), "[]string[1 3 5]")
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestArrayOfIntToArrayOfString(t *testing.T) {
|
|
|
|
+ res := ArrayOfIntToArrayOfString([]int{1, 3, 5})
|
|
|
|
+ Expect(t, len(res), 3)
|
|
|
|
+ Expect(t, res[0], "1")
|
|
|
|
+ Expect(t, res[1], "3")
|
|
|
|
+ Expect(t, res[2], "5")
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestArrayOfStringToArrayOfInt(t *testing.T) {
|
|
|
|
+ res := ArrayOfStringToArrayOfInt([]string{"1", "3", "5", "abc"})
|
|
|
|
+ Expect(t, len(res), 3)
|
|
|
|
+ Expect(t, res[0], 1)
|
|
|
|
+ Expect(t, res[1], 3)
|
|
|
|
+ Expect(t, res[2], 5)
|
|
|
|
+}
|