mirror of https://github.com/dtm-labs/dtm.git
csharpjavadistributed-transactionsdtmgogolangmicroservicenodejsphpdatabasesagaseatatcctransactiontransactionsxapythondistributed
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.3 KiB
56 lines
1.3 KiB
/*
|
|
* Copyright (c) 2021 yedf. All rights reserved.
|
|
* Use of this source code is governed by a BSD-style
|
|
* license that can be found in the LICENSE file.
|
|
*/
|
|
|
|
package dtmutil
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGin(t *testing.T) {
|
|
app := GetGinApp()
|
|
app.GET("/api/sample", WrapHandler2(func(c *gin.Context) interface{} {
|
|
return 1
|
|
}))
|
|
app.GET("/api/error", WrapHandler2(func(c *gin.Context) interface{} {
|
|
return errors.New("err1")
|
|
}))
|
|
getResultString := func(api string, body io.Reader) string {
|
|
req, _ := http.NewRequest("GET", api, body)
|
|
w := httptest.NewRecorder()
|
|
app.ServeHTTP(w, req)
|
|
return w.Body.String()
|
|
}
|
|
assert.Equal(t, "{\"msg\":\"pong\"}", getResultString("/api/ping", nil))
|
|
assert.Equal(t, "1", getResultString("/api/sample", nil))
|
|
assert.Equal(t, "{\"message\":\"err1\"}", getResultString("/api/error", strings.NewReader("{}")))
|
|
}
|
|
|
|
func TestFuncs(t *testing.T) {
|
|
wd := MustGetwd()
|
|
assert.NotEqual(t, "", wd)
|
|
|
|
dir1 := GetSQLDir()
|
|
assert.Equal(t, true, strings.HasSuffix(dir1, "/sqls"))
|
|
|
|
}
|
|
|
|
func TestRecoverPanic(t *testing.T) {
|
|
err := func() (rerr error) {
|
|
defer RecoverPanic(&rerr)
|
|
panic(fmt.Errorf("an error"))
|
|
}()
|
|
assert.Equal(t, "an error", err.Error())
|
|
}
|
|
|