Skip to content

Instantly share code, notes, and snippets.

@mindscratch
Created July 20, 2015 11:36
Show Gist options
  • Save mindscratch/d0196414b684276db046 to your computer and use it in GitHub Desktop.
Save mindscratch/d0196414b684276db046 to your computer and use it in GitHub Desktop.
test code that uses julienschmidt/httprouter
import "github.com/julienschmidt/httprouter"
import "net/http"
func doRequest(method, uri string, body *bytes.Buffer, handle httprouter.Handle) (*httptest.ResponseRecorder, error) {
resp := httptest.NewRecorder()
req, err := http.NewRequest(method, uri, body)
if err != nil {
return nil, err
}
router := httprouter.New()
router.Handle(method, uri, handle)
router.ServeHTTP(resp, req)
return resp, nil
}
func SomeTest(t *testing.T) {
body := bytes.NewBuffferString("hello")
handle := SomeHandle()
resp, err := doRequest("POST", "/v1/test", body, handle)
// check resp.Code, etc
}
@mat
Copy link

mat commented Jul 23, 2018

Thanks!

Typo: bytes.NewBuffferString has three f ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment