forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
128 lines (105 loc) · 2.6 KB
/
utils_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 dotweb
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"testing"
"net/url"
)
//common init context
func initContext(param *InitContextParam) *HttpContext {
httpRequest := &http.Request{}
context := &HttpContext{
request: &Request{
Request: httpRequest,
},
}
header := make(map[string][]string)
header["Accept-Encoding"] = []string{"gzip, deflate"}
header["Accept-Language"] = []string{"en-us"}
header["Foo"] = []string{"Bar", "two"}
//specify json
header["Content-Type"] = []string{param.contentType}
context.request.Header = header
jsonStr := param.convertHandler(param.t, param.v)
body := format(jsonStr)
context.request.Request.Body = body
return context
}
//init response context
func initResponseContext(param *InitContextParam) *HttpContext {
context := &HttpContext{
response: &Response{},
}
var buf1 bytes.Buffer
w := io.MultiWriter(&buf1)
writer := &gzipResponseWriter{
ResponseWriter: &httpWriter{},
Writer: w,
}
context.response = NewResponse(writer)
return context
}
//init request and response context
func initAllContext(param *InitContextParam) *HttpContext {
context := &HttpContext{
response: &Response{},
request: &Request{
Request: &http.Request{},
},
}
header := make(map[string][]string)
header["Accept-Encoding"] = []string{"gzip, deflate"}
header["Accept-Language"] = []string{"en-us"}
header["Foo"] = []string{"Bar", "two"}
//specify json
header["Content-Type"] = []string{param.contentType}
context.request.Header = header
u:=&url.URL{
Path:"/index",
}
context.request.URL=u
context.request.Method="POST"
jsonStr := param.convertHandler(param.t, param.v)
body := format(jsonStr)
context.request.Request.Body = body
//var buf1 bytes.Buffer
//w := io.MultiWriter(&buf1)
w := &httpWriter{}
//gzip 开关
/*
gw, _ := gzip.NewWriterLevel(w, DefaultGzipLevel)
writer := &gzipResponseWriter{
ResponseWriter: w,
Writer: &gzipResponseWriter{Writer: gw, ResponseWriter: w},
}
*/
context.response = NewResponse(w)
return context
}
type httpWriter http.Header
func (ho httpWriter) Header() http.Header {
return http.Header(ho)
}
func (ho httpWriter) Write(byte []byte) (int, error) {
fmt.Println("string:", string(byte))
return 0, nil
}
func (ho httpWriter) WriteHeader(code int) {
fmt.Println("code:", code)
}
func format(b string) io.ReadCloser {
s := strings.NewReader(b)
r := ioutil.NopCloser(s)
r.Close()
return r
}
type InitContextParam struct {
t *testing.T
v interface{}
contentType string
convertHandler func(t *testing.T, v interface{}) string
}