feat: test utils (#26)
parent
d6168cbad9
commit
51cef0bc53
@ -1,4 +1,4 @@
|
|||||||
.devcontainer
|
.devcontainer
|
||||||
components
|
components
|
||||||
logs
|
logs
|
||||||
|
out-test
|
||||||
|
@ -0,0 +1,67 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
gin.SetMode(gin.TestMode)
|
||||||
|
}
|
||||||
|
|
||||||
|
func performRequest(r http.Handler, method, origin string) *httptest.ResponseRecorder {
|
||||||
|
return performRequestWithHeaders(r, method, origin, http.Header{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func performRequestWithHeaders(r http.Handler, method, origin string, header http.Header) *httptest.ResponseRecorder {
|
||||||
|
req, _ := http.NewRequest(method, "/", nil)
|
||||||
|
// From go/net/http/request.go:
|
||||||
|
// For incoming requests, the Host header is promoted to the
|
||||||
|
// Request.Host field and removed from the Header map.
|
||||||
|
req.Host = header.Get("Host")
|
||||||
|
header.Del("Host")
|
||||||
|
if len(origin) > 0 {
|
||||||
|
header.Set("Origin", origin)
|
||||||
|
}
|
||||||
|
req.Header = header
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r.ServeHTTP(w, req)
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTestRouter() *gin.Engine {
|
||||||
|
router := gin.New()
|
||||||
|
router.Use(CorsHandler())
|
||||||
|
router.GET("/", func(c *gin.Context) {
|
||||||
|
c.String(http.StatusOK, "get")
|
||||||
|
})
|
||||||
|
router.POST("/", func(c *gin.Context) {
|
||||||
|
c.String(http.StatusOK, "post")
|
||||||
|
})
|
||||||
|
router.PATCH("/", func(c *gin.Context) {
|
||||||
|
c.String(http.StatusOK, "patch")
|
||||||
|
})
|
||||||
|
|
||||||
|
return router
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_CorsHandler(t *testing.T) {
|
||||||
|
router := newTestRouter()
|
||||||
|
// no CORS request, origin == ""
|
||||||
|
w := performRequest(router, "GET", "")
|
||||||
|
assert.Equal(t, "get", w.Body.String())
|
||||||
|
assert.Equal(t, w.Header().Get("Access-Control-Allow-Origin"), "*")
|
||||||
|
assert.Equal(t, w.Header().Get("Access-Control-Allow-Methods"), "*")
|
||||||
|
assert.Equal(t, w.Header().Get("Access-Control-Allow-Headers"), "*")
|
||||||
|
assert.Equal(t, w.Header().Get("Access-Control-Expose-Headers"), "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar")
|
||||||
|
assert.Equal(t, w.Header().Get("Access-Control-Max-Age"), "172800")
|
||||||
|
assert.Equal(t, w.Header().Get("Access-Control-Allow-Credentials"), "false")
|
||||||
|
assert.Equal(t, w.Header().Get("content-type"), "application/json")
|
||||||
|
|
||||||
|
w = performRequest(router, "OPTIONS", "")
|
||||||
|
assert.Equal(t, w.Body.String(), "\"Options Request!\"")
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_, b, _, _ = runtime.Caller(0)
|
||||||
|
// Root folder of this project
|
||||||
|
Root = filepath.Join(filepath.Dir(b), "../..")
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_GenSmallImage(t *testing.T) {
|
||||||
|
println(Root)
|
||||||
|
err := GenSmallImage(Root+"/docs/open-im-logo.png", Root+"/out-test/open-im-logo-test.png")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
err = GenSmallImage(Root+"/docs/open-im-logo.png", "out-test/open-im-logo-test.png")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
err = GenSmallImage(Root+"/docs/Architecture.jpg", "out-test/Architecture-test.jpg")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Md5(t *testing.T) {
|
||||||
|
result := Md5("go")
|
||||||
|
assert.Equal(t, result, "34d1f91fb2e514b8576fab1a75a89a6b")
|
||||||
|
|
||||||
|
result2 := Md5("go")
|
||||||
|
assert.Equal(t, result, result2)
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_PlatformIDToName(t *testing.T) {
|
||||||
|
assert.Equal(t, PlatformIDToName(1), "IOS")
|
||||||
|
assert.Equal(t, PlatformIDToName(2), "Android")
|
||||||
|
assert.Equal(t, PlatformIDToName(3), "Windows")
|
||||||
|
assert.Equal(t, PlatformIDToName(4), "OSX")
|
||||||
|
assert.Equal(t, PlatformIDToName(5), "Web")
|
||||||
|
assert.Equal(t, PlatformIDToName(6), "MiniWeb")
|
||||||
|
assert.Equal(t, PlatformIDToName(7), "Linux")
|
||||||
|
|
||||||
|
assert.Equal(t, PlatformIDToName(0), "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_PlatformNameToID(t *testing.T) {
|
||||||
|
assert.Equal(t, PlatformNameToID("IOS"), int32(1))
|
||||||
|
assert.Equal(t, PlatformNameToID("Android"), int32(2))
|
||||||
|
assert.Equal(t, PlatformNameToID("Windows"), int32(3))
|
||||||
|
assert.Equal(t, PlatformNameToID("OSX"), int32(4))
|
||||||
|
assert.Equal(t, PlatformNameToID("Web"), int32(5))
|
||||||
|
assert.Equal(t, PlatformNameToID("MiniWeb"), int32(6))
|
||||||
|
assert.Equal(t, PlatformNameToID("Linux"), int32(7))
|
||||||
|
|
||||||
|
assert.Equal(t, PlatformNameToID("UnknownDevice"), int32(0))
|
||||||
|
assert.Equal(t, PlatformNameToID(""), int32(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_PlatformNameToClass(t *testing.T) {
|
||||||
|
assert.Equal(t, PlatformNameToClass("IOS"), "Mobile")
|
||||||
|
assert.Equal(t, PlatformNameToClass("Android"), "Mobile")
|
||||||
|
assert.Equal(t, PlatformNameToClass("OSX"), "PC")
|
||||||
|
assert.Equal(t, PlatformNameToClass("Windows"), "PC")
|
||||||
|
assert.Equal(t, PlatformNameToClass("Web"), "PC")
|
||||||
|
assert.Equal(t, PlatformNameToClass("MiniWeb"), "Mobile")
|
||||||
|
assert.Equal(t, PlatformNameToClass("Linux"), "PC")
|
||||||
|
|
||||||
|
assert.Equal(t, PlatformNameToClass("UnknownDevice"), "")
|
||||||
|
assert.Equal(t, PlatformNameToClass(""), "")
|
||||||
|
}
|
Loading…
Reference in new issue