Handlers
How to test
Handlers can be tested with gos
sub-level package onyx
. onyx
provides functions to create new HTTP requests as well as execute a handler and determine test result.
Avoid direct handler tests
gos
generates two versions of your HTTP handler functions, one for development and one for production. To ensure your tests never break, use your handlers’ package function MakeHandler
to keep your tests from breaking.
Sample test
Below is source code using onyx to test an HTTP handler. Notice how MakeHandler
is the handler, while the test name is different.
package handlers
import "testing"
import "github.com/cheikhshift/gos/onyx"
// Camel case version of method + request path,
// without slashes,
// prefixed with `Test`
func TestGETIndex(t *testing.T) {
// Load all handlers.
h := MakeHandler(Handler)
// New request to GET /index
req,err := onyx.NewRequest("GET", "/index")
if err != nil {
panic(err)
}
// execute, fail test if code
// is not 200.
onyx.Handle(req, h, t)
}