forked from plimble/ace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
86 lines (72 loc) · 1.71 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
package ace
import (
"bytes"
"encoding/json"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestJSONResp(t *testing.T) {
assert := assert.New(t)
data := map[string]interface{}{
"s": "test",
"n": 123,
"b": true,
}
a := New()
a.GET("/", func(c *C) {
c.JSON(200, data)
})
buf := &bytes.Buffer{}
json.NewEncoder(buf).Encode(data)
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
assert.Equal(200, w.Code)
assert.Equal(buf.String(), w.Body.String())
assert.Equal("application/json; charset=UTF-8", w.Header().Get("Content-Type"))
}
func TestStringResp(t *testing.T) {
assert := assert.New(t)
a := New()
a.GET("/", func(c *C) {
c.String(200, "123")
})
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
assert.Equal(200, w.Code)
assert.Equal("123", w.Body.String())
assert.Equal("text/html; charset=UTF-8", w.Header().Get("Content-Type"))
}
func TestDownloadResp(t *testing.T) {
assert := assert.New(t)
a := New()
a.GET("/", func(c *C) {
c.Download(200, []byte("123"))
})
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
assert.Equal(200, w.Code)
assert.Equal("123", w.Body.String())
assert.Equal("application/octet-stream; charset=UTF-8", w.Header().Get("Content-Type"))
}
func TestCData(t *testing.T) {
assert := assert.New(t)
a := New()
a.Use(func(c *C) {
c.Set("test", "123")
c.Next()
})
a.GET("/", func(c *C) {
c.GetAll()
c.String(200, c.Get("test").(string))
})
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, r)
assert.Equal(200, w.Code)
assert.Equal("123", w.Body.String())
}