forked from gobuffalo/httptest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httptest.go
79 lines (69 loc) · 1.63 KB
/
httptest.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
/*
Package httptest was formerly known as [https://github.com/markbates/willie](https://github.com/markbates/willie). It is used to test HTTP applications easily.
*/
package httptest
import (
"fmt"
"net/http"
"net/http/httptest"
)
// map the std httptest package for ease
const DefaultRemoteAddr = httptest.DefaultRemoteAddr
var NewServer = httptest.NewServer
var NewRequest = httptest.NewRequest
var NewRecorder = httptest.NewRecorder
var NewTLSServer = httptest.NewTLSServer
var NewUnstartedServer = httptest.NewUnstartedServer
type ResponseRecorder = httptest.ResponseRecorder
type Server = httptest.Server
type encodable interface {
Encode() string
}
type Handler struct {
http.Handler
Cookies string
Headers map[string]string
HmaxSecret string
}
func (w *Handler) HTML(u string, args ...interface{}) *Request {
hs := map[string]string{}
for key, val := range w.Headers {
hs[key] = val
}
hs["Accept"] = "application/html"
return &Request{
URL: fmt.Sprintf(u, args...),
handler: w,
Headers: hs,
}
}
func (w *Handler) JSON(u string, args ...interface{}) *JSON {
hs := map[string]string{}
for key, val := range w.Headers {
hs[key] = val
}
hs["Accept"] = "application/json"
return &JSON{
URL: fmt.Sprintf(u, args...),
handler: w,
Headers: hs,
}
}
func (w *Handler) XML(u string, args ...interface{}) *XML {
hs := map[string]string{}
for key, val := range w.Headers {
hs[key] = val
}
hs["Accept"] = "application/xml"
return &XML{
URL: fmt.Sprintf(u, args...),
handler: w,
Headers: hs,
}
}
func New(h http.Handler) *Handler {
return &Handler{
Handler: h,
Headers: map[string]string{},
}
}