forked from spaceweasel/mango
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.go
More file actions
144 lines (131 loc) · 4.49 KB
/
browser.go
File metadata and controls
144 lines (131 loc) · 4.49 KB
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
package mango
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
)
// Browser is client used to simulate HTTP request to the service.
// This can be useful for verifying routings, testing request headers,
// as well as examining responses: headers, status code, content.
type Browser struct {
handler http.Handler
}
// NewBrowser returns a *Browser which can be used to test server responses.
// This method takes a Router as a parameter to enable full and accurate
// testing/simulation to be performed.
func NewBrowser(h http.Handler) *Browser {
return &Browser{handler: h}
}
// Get simulates an HTTP GET request to the server.
// The response can be examined afterwards to check status, headers
// and content.
func (b *Browser) Get(url string, headers http.Header) (*httptest.ResponseRecorder, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header = headers
w := httptest.NewRecorder()
b.handler.ServeHTTP(w, req)
return w, nil
}
// Post simulates an HTTP POST request to the server.
// The response can be examined afterwards to check status, headers
// and content.
func (b *Browser) Post(url string, body io.Reader, headers http.Header) (*httptest.ResponseRecorder, error) {
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.Header = headers
w := httptest.NewRecorder()
b.handler.ServeHTTP(w, req)
return w, nil
}
// PostS simulates an HTTP POST request to the server, with a string body.
// The response can be examined afterwards to check status, headers
// and content.
func (b *Browser) PostS(url, body string, headers http.Header) (*httptest.ResponseRecorder, error) {
bytes := bytes.NewBufferString(body)
return b.Post(url, bytes, headers)
}
// Put simulates an HTTP PUT request to the server.
// The response can be examined afterwards to check status, headers
// and content.
func (b *Browser) Put(url string, body io.Reader, headers http.Header) (*httptest.ResponseRecorder, error) {
req, err := http.NewRequest("PUT", url, body)
if err != nil {
return nil, err
}
req.Header = headers
w := httptest.NewRecorder()
b.handler.ServeHTTP(w, req)
return w, nil
}
// PutS simulates an HTTP PUT request to the server, with a string body.
// The response can be examined afterwards to check status, headers
// and content.
func (b *Browser) PutS(url, body string, headers http.Header) (*httptest.ResponseRecorder, error) {
bytes := bytes.NewBufferString(body)
return b.Put(url, bytes, headers)
}
// Delete simulates an HTTP DELETE request to the server.
// The response can be examined afterwards to check status, headers
// and content.
func (b *Browser) Delete(url string, headers http.Header) (*httptest.ResponseRecorder, error) {
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return nil, err
}
req.Header = headers
w := httptest.NewRecorder()
b.handler.ServeHTTP(w, req)
return w, nil
}
// Patch simulates an HTTP PATCH request to the server, with a string body..
// The response can be examined afterwards to check status, headers
// and content.
func (b *Browser) Patch(url string, body io.Reader, headers http.Header) (*httptest.ResponseRecorder, error) {
req, err := http.NewRequest("PATCH", url, body)
if err != nil {
return nil, err
}
req.Header = headers
w := httptest.NewRecorder()
b.handler.ServeHTTP(w, req)
return w, nil
}
// PatchS simulates an HTTP PATCH request to the server, with a string body..
// The response can be examined afterwards to check status, headers
// and content.
func (b *Browser) PatchS(url, body string, headers http.Header) (*httptest.ResponseRecorder, error) {
bytes := bytes.NewBufferString(body)
return b.Patch(url, bytes, headers)
}
// Head simulates an HTTP HEAD request to the server.
// The response can be examined afterwards to check status, headers
// and content.
func (b *Browser) Head(url string, headers http.Header) (*httptest.ResponseRecorder, error) {
req, err := http.NewRequest("HEAD", url, nil)
if err != nil {
return nil, err
}
req.Header = headers
w := httptest.NewRecorder()
b.handler.ServeHTTP(w, req)
return w, nil
}
// Options simulates an HTTP OPTIONS request to the server.
// The response can be examined afterwards to check status, headers
// and content.
func (b *Browser) Options(url string, headers http.Header) (*httptest.ResponseRecorder, error) {
req, err := http.NewRequest("OPTIONS", url, nil)
if err != nil {
return nil, err
}
req.Header = headers
w := httptest.NewRecorder()
b.handler.ServeHTTP(w, req)
return w, nil
}