forked from appleboy/go-hello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello-world_test.go
42 lines (33 loc) · 891 Bytes
/
hello-world_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"net/http"
"testing"
"time"
"github.com/appleboy/gofight"
"github.com/buger/jsonparser"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestGinHelloWorld(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gofight.New()
r.GET("/").
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
data := []byte(r.Body.String())
value, _ := jsonparser.GetString(data, "text")
assert.Equal(t, "Hello World", value)
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestRunNormalServer(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
go func() {
main()
}()
// have to wait for the goroutine to start and run the server
// otherwise the main thread will complete
time.Sleep(5 * time.Millisecond)
assert.Error(t, router.Run(":8000"))
gofight.TestRequest(t, "http://localhost:8000/")
}