neilotoole.io

Web presence of Neil O'Toole.

techo testing lib

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 I sometimes found stdlib http.httptest to be tedious and verbose. The value of this library is that writing tests is cleaner and expressive with techo.

Here’s how you use it (some error checks omitted for brevity)

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()

    body, _ := ioutil.ReadAll(resp.Body)
    assert.Equal(t, "Hello World", string(body))
}

Check out the GitHub project page for more.