Love HTTP Tests with tf

http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, World!")
})
func TestHTTPRouter(t *testing.T) {
run := tf.ServeHTTP(t, http.DefaultServeMux.ServeHTTP)
run(&tf.HTTPTest{
Path: "/hello",
Status: http.StatusOK,
ResponseBody: strings.NewReader("Hello, World!"),
})
run(&tf.HTTPTest{
Path: "/world",
Status: http.StatusNotFound,
})
}
run(&tf.MultiHTTPTest{
Steps: []*tf.HTTPTest{
{
Path: "/save",
Method: http.MethodPut,
RequestBody: strings.NewReader(`{"foo":"bar"}`),
Status: http.StatusCreated,
},
{
Path: "/fetch",
Method: http.MethodGet,
Status: http.StatusOK,
ResponseBody: strings.NewReader(`{"foo":"bar"}`),
},
},
})

--

--

I’m a data nerd and TDD enthusiast originally from Sydney. Currently working for Uber in New York. My thoughts here are my own. 🤓 elliotchance@gmail.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Elliot Chance

Elliot Chance

I’m a data nerd and TDD enthusiast originally from Sydney. Currently working for Uber in New York. My thoughts here are my own. 🤓 elliotchance@gmail.com