End to End Testing

最后更新于:2022-04-01 03:04:11

# 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 endtests will run the middleware, router, and other that a request my passthrough. ~~~ package main import ( "fmt" "net/http" "github.com/codegangsta/negroni" "github.com/julienschmidt/httprouter" ) func HelloWorld(res http.ResponseWriter, req *http.Request, p httprouter.Params) { fmt.Fprint(res, "Hello World") } 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 main() { http.ListenAndServe(":3000", App()) } ~~~ This is the test file. It should be placed in the same directory asyour 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 1. Create another piece of middleware that mutates the status of the request. 1. Create a POST request and test that the request is properly handled.
';