-
Notifications
You must be signed in to change notification settings - Fork 26
/
context_test.go
128 lines (116 loc) · 2.93 KB
/
context_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package yee
import (
"github.com/stretchr/testify/assert"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
var testData = `{"id":1,"name":"Jon Snow"}`
type res struct {
ID int `json:"id"`
Name string `json:"name"`
}
func TestContextJSON(t *testing.T) {
y := New()
y.POST("/", func(c Context) (err error) {
t := new(res)
if err = c.Bind(&t); err != nil {
return err
}
return c.JSON(http.StatusOK, t)
})
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(testData))
req.Header.Set("Content-Type", MIMEApplicationJSON)
rec := httptest.NewRecorder()
y.ServeHTTP(rec, req)
assert.Equal(t, testData+"\n", rec.Body.String())
}
func TestContextForward(t *testing.T) {
y := New()
y.POST("/", func(c Context) (err error) {
return c.JSON(http.StatusOK, c.RemoteIP())
})
req := httptest.NewRequest(http.MethodPost, "/", nil)
req.Header.Set(HeaderXForwardedFor, "<img> ")
rec := httptest.NewRecorder()
y.ServeHTTP(rec, req)
}
func TestContextString(t *testing.T) {
y := New()
y.POST("/", func(c Context) (err error) {
return c.String(http.StatusOK, "hello")
})
req := httptest.NewRequest(http.MethodPost, "/", nil)
rec := httptest.NewRecorder()
y.ServeHTTP(rec, req)
assert.Equal(t, "hello", rec.Body.String())
}
func crashMiddleware() HandlerFunc {
return func(c Context) (err error) {
c.CrashWithStatus(http.StatusUnauthorized)
return
}
}
func sayMiddleware() HandlerFunc {
return func(c Context) (err error) {
log.Println("say")
return
}
}
func TestCrash(t *testing.T) {
y := New()
y.Use(crashMiddleware())
y.Use(sayMiddleware())
y.GET("/", func(c Context) (err error) {
return c.String(http.StatusOK, "hello")
})
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
y.ServeHTTP(rec, req)
assert.Equal(t, http.StatusUnauthorized, rec.Body)
}
func TestRedirect(t *testing.T) {
y := New()
y.GET("/", func(c Context) (err error) {
return c.Redirect(http.StatusMovedPermanently, "/get")
})
y.GET("/get", func(c Context) (err error) {
return c.String(http.StatusOK, "hello")
})
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
y.ServeHTTP(rec, req)
}
func BenchmarkAllocJSON(b *testing.B) {
y := New()
y.POST("/", func(c Context) (err error) {
tl := new(res)
if err = c.Bind(&tl); err != nil {
return err
}
return c.JSON(http.StatusOK, tl)
})
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(testData))
req.Header.Set("Content-Type", MIMEApplicationJSON)
rec := httptest.NewRecorder()
y.ServeHTTP(rec, req)
}
}
func BenchmarkAllocString(b *testing.B) {
y := New()
y.POST("/", func(c Context) (err error) {
return c.String(http.StatusOK, "ok")
})
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
req := httptest.NewRequest(http.MethodPost, "/", nil)
rec := httptest.NewRecorder()
y.ServeHTTP(rec, req)
}
}