I’ve just released techo, an Echo-based alternative to Golang’s http.httptest. The genesis was trying to write automated tests for generated (swagger-codegen) code, and how tedious and unnecessarily verbose the standard http.httptest library was. If you see the same code repeated three or more times, write a library. I will stand by that maxim through the current Go fad of “always write your own” nonsense. The value of this library is that writing tests is much cleaner and expressive with techo. Here’s how you use it:

func TestHello(t *testing.T) {

te := techo.New() // start the web server - it's running on some random port now
defer te.Stop() // stop the server at the end of this function

// serve up some content (using echo, cuz it's so hot right now)
te.GET("/hello", func(c echo.Context) error {
param := c.QueryParam("name")
assert.Equal(t, param, "World") // assert some stuff
return c.String(http.StatusOK, fmt.Sprintf("Hello %v", param))
})

// Let's make a request to the web server
resp, _ := http.Get(te.AbsURL("/hello?name=World"))
// Note that te.AbsURL() turns "/hello" into "http://127.0.0.1:[PORT]/hello"
defer resp.Body.Close()