-
Notifications
You must be signed in to change notification settings - Fork 12
/
gogo_test.go
153 lines (121 loc) · 3.13 KB
/
gogo_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package gogo
import (
"net/http"
"net/http/httptest"
"os"
"path"
"testing"
"github.com/dolab/httptesting"
"github.com/golib/assert"
)
var (
fakeApp = func(mode string) *AppServer {
root, _ := os.Getwd()
return New(mode, path.Join(root, "testdata"))
}
)
func Test_New(t *testing.T) {
app := fakeApp("test")
app.GET("/gogo", func(ctx *Context) {
ctx.Text("Hello, gogo!")
})
app.PUT("/ping", func(ctx *Context) {
ctx.Text("pong")
})
ts := httptesting.NewServer(app, false)
defer ts.Close()
// GET
request := ts.New(t)
request.Get("/gogo")
request.AssertOK()
request.AssertContains("Hello, gogo!")
// PUT
request = ts.New(t)
request.PutJSON("/ping", nil)
request.AssertOK()
request.AssertContains("pong")
}
func Test_NewWithModeConfig(t *testing.T) {
it := assert.New(t)
app := fakeApp("development")
it.Equal("gogo for development", app.config.RunName())
}
func Test_NewWithFilename(t *testing.T) {
it := assert.New(t)
root, _ := os.Getwd()
filename := path.Join(root, "testdata", "config", "application.yml")
app := New("development", filename)
it.Equal("gogo", app.config.RunName())
}
func Test_Gogo_Response(t *testing.T) {
app := fakeApp("product")
app.GET("/ping", func(ctx *Context) {
ctx.SetStatus(http.StatusConflict)
ctx.Return()
})
app.GET("/ghost", func(ctx *Context) {
ctx.SetStatus(http.StatusConflict)
})
ts := httptesting.NewServer(app, false)
defer ts.Close()
// it should work for ping-pong
request := ts.New(t)
request.Get("/ping", nil)
request.AssertStatus(http.StatusConflict)
request.AssertEmpty()
// it should work for ghost
request = ts.New(t)
request.Get("/ghost", nil)
request.AssertStatus(http.StatusConflict)
request.AssertEmpty()
}
func Benchmark_Gogo(b *testing.B) {
it := assert.New(b)
recorder := httptest.NewRecorder()
reader := []byte("Hello,world!")
app := fakeApp("product")
app.GET("/bench", func(ctx *Context) {
recorder.WriteHeader(http.StatusOK)
recorder.Write(reader)
})
ts := httptest.NewServer(app)
defer ts.Close()
request, _ := http.NewRequest(http.MethodGet, ts.URL+"/bench", nil)
_, err := http.DefaultClient.Do(request)
if it.Nil(err) {
it.Equal(http.StatusOK, recorder.Code)
it.Equal(reader, recorder.Body.Bytes())
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
recorder.Body.Reset()
http.DefaultClient.Do(request)
it.Equal(http.StatusOK, recorder.Code)
it.Equal(reader, recorder.Body.Bytes())
}
}
func Benchmark_Go(b *testing.B) {
it := assert.New(b)
recorder := httptest.NewRecorder()
reader := []byte("Hello,world!")
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
recorder.WriteHeader(http.StatusOK)
recorder.Write(reader)
}))
defer ts.Close()
request, _ := http.NewRequest(http.MethodGet, ts.URL+"/bench", nil)
_, err := http.DefaultClient.Do(request)
if it.Nil(err) {
it.Equal(http.StatusOK, recorder.Code)
it.Equal(reader, recorder.Body.Bytes())
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
recorder.Body.Reset()
http.DefaultClient.Do(request)
it.Equal(http.StatusOK, recorder.Code)
it.Equal(reader, recorder.Body.Bytes())
}
}