HTTPTest in Go - no body returning with Echo

Viewed 1026

I'm trying to test a handler in Go using Echo. Here is the handler:

func submitted(c echo.Context) error {
    // do stuff with JSON here
    return c.Render(http.StatusOK, "submitted", "true")
}

and the test:

import (
    "testing"
    "net/http/httptest"
    "net/http"
    "github.com/labstack/echo"
    "strings"
    "fmt"
    "html/template"
)

var userJSON = `{<values go here>}`

func TestFunction(t *testing.T) {
    te := &Template{
        templates: template.Must(template.ParseGlob("public/views/*.html")),
    }
    e := echo.New()
    e.Renderer = te
    req := httptest.NewRequest(http.MethodPost, "/submitted", strings.NewReader(userJSON))
    req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
    rec := httptest.NewRecorder()
    c := e.NewContext(req, rec)
    fmt.Println(rec.Body.String())
}

However, no body is returned it seems - even though the handler returns submitted.html file when running regularly. Any ideas?

1 Answers

There is one element which seems missing from your test function: a handler.

Compare your approach to "Build a web application in Go (golang) / Testing" from Soham Kamani:

//main_test.go

package main

import (
    "net/http"
    "net/http/httptest"
    "testing"
)

func TestHandler(t *testing.T) {
    //Here, we form a new HTTP request. This is the request that's going to be
    // passed to our handler.
    // The first argument is the method, the second argument is the route (which
    //we leave blank for now, and will get back to soon), and the third is the
    //request body, which we don't have in this case.
    req, err := http.NewRequest("GET", "", nil)

    // In case there is an error in forming the request, we fail and stop the test
    if err != nil {
        t.Fatal(err)
    }

    // We use Go's httptest library to create an http recorder. This recorder
    // will act as the target of our http request
    // (you can think of it as a mini-browser, which will accept the result of
    // the http request that we make)
    recorder := httptest.NewRecorder()

    // Create an HTTP handler from our handler function. "handler" is the handler
    // function defined in our main.go file that we want to test
    hf := http.HandlerFunc(handler)

    // Serve the HTTP request to our recorder. This is the line that actually
    // executes our the handler that we want to test
    hf.ServeHTTP(recorder, req)

    // Check the status code is what we expect.
    if status := recorder.Code; status != http.StatusOK {
        t.Errorf("handler returned wrong status code: got %v want %v",
            status, http.StatusOK)
    }

    // Check the response body is what we expect.
    expected := `Hello World!`
    actual := recorder.Body.String()
    if actual != expected {
        t.Errorf("handler returned unexpected body: got %v want %v", actual, expected)
    }
}
Related