Unit Testing

    This is the test file. It should be placed in the same directory as
    your application and name main_test.go.

    1. package main
    2. import (
    3. "net/http"
    4. "net/http/httptest"
    5. "testing"
    6. func Test_HelloWorld(t *testing.T) {
    7. req, err := http.NewRequest("GET", "http://example.com/foo", nil)
    8. if err != nil {
    9. t.Fatal(err)
    10. }
    11. HelloWorld(res, req)
    12. exp := "Hello World"
    13. act := res.Body.String()
    14. if exp != act {
    15. t.Fatalf("Expected %s gog %s", exp, act)
    16. }
    1. Change the output of HelloWorld to print a parameter and then test that the parameter is rendered.
    2. Create a POST request and test that the request is properly handled.