-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware_test.go
61 lines (54 loc) · 1.1 KB
/
middleware_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package ginception
import (
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"testing"
"time"
)
func TestMain(m *testing.M) {
go setupServer()
// Replace with something better
time.Sleep(1 * time.Second)
m.Run()
}
func setupServer() {
r := gin.Default()
r.Use(Middleware())
r.GET("/", func(context *gin.Context) {
panic("test panic")
})
r.GET("/ok", func(context *gin.Context) {
context.String(200, "OK")
})
err := r.Run(":7885")
if err != nil {
panic(err)
}
}
func TestMiddleware(t *testing.T) {
req, err := http.Get("http://localhost:7885")
if err != nil {
t.Fatal(err)
}
if req.StatusCode != 500 {
t.Fatal("server send responded with code other then 500:", req.StatusCode)
}
}
func TestMiddlewareOk(t *testing.T) {
req, err := http.Get("http://localhost:7885/ok")
if err != nil {
t.Fatal(err)
}
if req.StatusCode != 200 {
t.Fatal("server send responded with code other then 200:", req.StatusCode)
}
content, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
if string(content) != "OK" {
t.Log(content)
t.Fatal("Server returned wrong response")
}
}