forked from cortesi/devd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
38 lines (34 loc) · 833 Bytes
/
common_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
package devd
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
type handlerTester struct {
t *testing.T
h http.Handler
}
// Request makes a test request
func (ht *handlerTester) Request(method string, url string, params url.Values) *httptest.ResponseRecorder {
req, err := http.NewRequest(method, url, strings.NewReader(params.Encode()))
if err != nil {
ht.t.Errorf("%v", err)
}
if params != nil {
req.Header.Set(
"Content-Type",
"application/x-www-form-urlencoded; param=value",
)
}
w := httptest.NewRecorder()
ht.h.ServeHTTP(w, req)
return w
}
// AssertCode asserts that the HTTP return code matches an expected value
func AssertCode(t *testing.T, resp *httptest.ResponseRecorder, code int) {
if resp.Code != code {
t.Errorf("Expected code %d, got %d", code, resp.Code)
}
}