# 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.
- Introduction
- 1. Go Makes Things Simple
- 2. The net/http package
- 3. Creating a Basic Web App
- 4. Deployment
- 5. URL Routing
- 6. Middleware
- 7. Rendering
- JSON
- HTML Templates
- Using The render package
- 8. Testing
- Unit Testing
- End to End Testing
- 9. Controllers
- 10. Databases
- 11. Tips and Tricks
- 12. Moving Forward