-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
115 lines (93 loc) · 2.19 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
package rider
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"testing"
"github.com/hypwxm/rider/logger"
)
func newTestContext() (Context, http.ResponseWriter, *http.Request) {
var context Context
var hw *riderWriter
var req *http.Request
hw = &riderWriter{}
url, _ := url.Parse("localhost:5000/a/x")
req = &http.Request{
Method: http.MethodGet,
URL: url,
}
server := &HttpServer{}
server.logger = logger.NewLogger()
context = newContext(hw, req, server)
return context, hw, req
}
func TestNewContext(t *testing.T) {
context, hw, req := newTestContext()
if context.Response().writer != hw {
t.Error("设置response错误")
}
if context.Request().request != req {
t.Error("设置request错误")
}
}
func TestCSend(t *testing.T) {
context, _, _ := newTestContext()
context.Send(200, []byte("test"))
//context.Send([]byte("test"))
body := fmt.Sprintf("%s", wf.body)
if body != "200test" {
t.Error("context.response.send error")
}
wf.body = []byte{}
}
func TestCSendJson(t *testing.T) {
context, _, _ := newTestContext()
context.SendJson(200, map[string]string{
"a": "a",
})
mapdata := make(map[string]string)
t.Logf("%s", wf.body[3:]) //200{"a":"a"}
err := json.Unmarshal(wf.body[3:], &mapdata)
if err != nil {
t.Error(err)
}
if mapdata["a"] != "a" {
t.Error("context.response.send error")
}
header := context.HeaderValue("Content-Type")
t.Logf("%s", header)
wf.body = []byte{}
}
func TestHijackSend(t *testing.T) {
context, _, _ := newTestContext()
context.Hijack()
context.Send(200, []byte("test"))
t.Logf("%s", wf.body)
body := fmt.Sprintf("%s", wf.body)
if body != "200test" {
t.Error("context.response.send error")
}
wf.body = []byte{}
}
func TestHijackSendJson(t *testing.T) {
context, _, _ := newTestContext()
context.Hijack()
context.SendJson(200, map[string]string{
"a": "a",
})
mapdata := make(map[string]string)
t.Logf("%s", wf.body[3:]) //200{"a":"a"}
err := json.Unmarshal(wf.body[3:], &mapdata)
if err != nil {
t.Error(err)
}
if mapdata["a"] != "a" {
t.Error("context.response.send error")
}
header := context.HeaderValue("Content-Type")
t.Logf("%s", header)
wf.body = []byte{}
}
func BenchmarkSend(b *testing.B) {
}