-
Thanks for a great project. gofiber have some examples around testing using the standard library and expect here I would rather use your library for this as it provides a much easier way for me to write my tests. gofiber is using fasthttp and so I have tried to use the fasthttp example you have as a start. Below is the example code and unit test , I am trying to implement the same example test but with httptest. Clearly I am not getting it right . Example fiber app. package main
import (
"log"
"github.com/gofiber/fiber/v2"
)
func main() {
// Use an external setup function in order
// to configure the app in tests as well
app := Setup()
// start the application on http://localhost:3000
log.Fatal(app.Listen(":3000"))
}
// Setup Setup a fiber app with all of its routes
func Setup() *fiber.App {
// Initialize a new app
app := fiber.New()
// Register the index route with a simple
// "OK" response. It should return status
// code 200
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("OK")
})
// Return the configured app
return app
} Example Tests package main
import (
"io/ioutil"
"net/http"
"testing"
"github.com/gavv/httpexpect/v2"
"github.com/stretchr/testify/assert"
)
func TestIndexRoute(t *testing.T) {
// Define a structure for specifying input and output
// data of a single test case. This structure is then used
// to create a so called test map, which contains all test
// cases, that should be run for testing this function
tests := []struct {
description string
// Test input
route string
// Expected output
expectedError bool
expectedCode int
expectedBody string
}{
{
description: "index route",
route: "/",
expectedError: false,
expectedCode: 200,
expectedBody: "OK",
},
{
description: "non existing route",
route: "/i-dont-exist",
expectedError: false,
expectedCode: 404,
expectedBody: "Cannot GET /i-dont-exist",
},
}
// Setup the app as it is done in the main function
app := Setup()
// Iterate through test single test cases
for _, test := range tests {
// Create a new http request with the route
// from the test case
req, _ := http.NewRequest(
"GET",
test.route,
nil,
)
// Perform the request plain with the app.
// The -1 disables request latency.
res, err := app.Test(req, -1)
// verify that no error occured, that is not expected
assert.Equalf(t, test.expectedError, err != nil, test.description)
// As expected errors lead to broken responses, the next
// test case needs to be processed
if test.expectedError {
continue
}
// Verify if the status code is as expected
assert.Equalf(t, test.expectedCode, res.StatusCode, test.description)
// Read the response body
body, err := ioutil.ReadAll(res.Body)
// Reading the response body should work everytime, such that
// the err variable should be nil
assert.Nilf(t, err, test.description)
// Verify, that the reponse body equals the expected body
assert.Equalf(t, test.expectedBody, string(body), test.description)
}
}
//fastHTTPTester returns a new Expect instance to test FastHTTPHandler().
func fastHTTPTester(t *testing.T) *httpexpect.Expect {
app := Setup()
return httpexpect.WithConfig(httpexpect.Config{
// Pass requests directly to FastHTTPHandler.
Client: &http.Client{
Transport: httpexpect.NewFastBinder(app),
Jar: httpexpect.NewJar(),
},
// Report errors using testify.
Reporter: httpexpect.NewAssertReporter(t),
})
}
func TestFastHTTP(t *testing.T) {
e := fastHTTPTester(t)
e.GET("/").Expect().
Status(http.StatusOK).
Body().Equal("OK")
e.GET("/i-dont-exist").Expect().
Status(http.StatusNotFound).
Body().Equal("Cannot GET /i-dont-exist")
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I found the issue. Transport: httpexpect.NewFastBinder(app), Should be Transport: httpexpect.NewFastBinder(app.Handler()), |
Beta Was this translation helpful? Give feedback.
-
Thanks for sharing! |
Beta Was this translation helpful? Give feedback.
I found the issue.
Should be