45 lines
723 B
Go
45 lines
723 B
Go
|
|
package responsex_test
|
||
|
|
|
||
|
|
import (
|
||
|
|
"io"
|
||
|
|
"net/http"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"code.yun.ink/pkg/responsex"
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestResponse(t *testing.T) {
|
||
|
|
|
||
|
|
go func() {
|
||
|
|
g := gin.Default()
|
||
|
|
g.GET("/", func(ctx *gin.Context) {
|
||
|
|
responsex.GinSuccess(ctx, "hello world")
|
||
|
|
})
|
||
|
|
// 启动服务
|
||
|
|
// http://localhost:8080
|
||
|
|
g.Run(":8080")
|
||
|
|
}()
|
||
|
|
|
||
|
|
time.Sleep(1 * time.Second)
|
||
|
|
|
||
|
|
// 测试响应
|
||
|
|
// 发送请求
|
||
|
|
// curl http://localhost:8080
|
||
|
|
// 期望响应: {"code":200,"msg":"请求成功","data":"hello world"}
|
||
|
|
|
||
|
|
resp, err := http.Get("http://localhost:8080")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
defer resp.Body.Close()
|
||
|
|
|
||
|
|
b, err := io.ReadAll(resp.Body)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
t.Log(string(b))
|
||
|
|
|
||
|
|
}
|