End To End Testing
End to end allows us to test applications through the whole request cycle. Where unit testing is meant to just test a particular function, end to end tests will run the middleware, router, and other that a request my pass through.
package main
import (
"fmt"
"net/http"
"github.com/codegangsta/negroni"
"github.com/julienschmidt/httprouter"
)
func main() {
http.ListenAndServe(":3000", app())
}
func app() http.Handler {
n := negroni.Classic()
m := func(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
fmt.Fprint(res, "Before...")
next(res, req)
fmt.Fprint(res, "...After")
}
n.Use(negroni.HandlerFunc(m))
r := httprouter.New()
r.GET("/", helloWorld)
n.UseHandler(r)
return n
}
func helloWorld(res http.ResponseWriter, req *http.Request, p httprouter.Params) {
fmt.Fprint(res, "Hello World")
}
This is the test file. It should be placed in the same directory as
your application and name main_test.go
.
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func Test_App(t *testing.T) {
ts := httptest.NewServer(app())
defer ts.Close()
res, err := http.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
exp := "Before...Hello World...After"
if exp != string(body) {
t.Fatalf("Expected %s got %s", exp, body)
}
}
Exercises
- Create another piece of middleware that mutates the status of the request.
- Create a POST request and test that the request is properly handled.