github.com/cortesi/devd@v0.0.0-20200427000907-c1a3bfba27d8/common_test.go (about) 1 package devd 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "net/url" 7 "strings" 8 "testing" 9 ) 10 11 type handlerTester struct { 12 t *testing.T 13 h http.Handler 14 } 15 16 // Request makes a test request 17 func (ht *handlerTester) Request(method string, url string, params url.Values) *httptest.ResponseRecorder { 18 req, err := http.NewRequest(method, url, strings.NewReader(params.Encode())) 19 if err != nil { 20 ht.t.Errorf("%v", err) 21 } 22 if params != nil { 23 req.Header.Set( 24 "Content-Type", 25 "application/x-www-form-urlencoded; param=value", 26 ) 27 } 28 w := httptest.NewRecorder() 29 ht.h.ServeHTTP(w, req) 30 return w 31 } 32 33 // AssertCode asserts that the HTTP return code matches an expected value 34 func AssertCode(t *testing.T, resp *httptest.ResponseRecorder, code int) { 35 if resp.Code != code { 36 t.Errorf("Expected code %d, got %d", code, resp.Code) 37 } 38 }