-
Notifications
You must be signed in to change notification settings - Fork 49
Open
Description
Hey @ant0ine
How do you usually test your routes (status code, body etc)? A go-json-rest route expects w *rest.ResponseWriter and r *rest.Request, but using NewResquest and NewRecorder we get httptest.ResponseRecorder and http.Request.
// message.go
/// ...
func main() {
api := Api{}
api.initDB()
defer api.closeDB()
handler := rest.ResourceHandler{
EnableRelaxedContentType: true,
}
handler.SetRoutes(
rest.RouteObjectMethod("POST", "/api/messages", &api, "CreateMessage"),
)
log.Println("Starting in development on http://0.0.0.0:8080")
http.ListenAndServe(":8080", &handler)
}
func (api *Api) CreateMessage(w *rest.ResponseWriter, r *rest.Request) {
w.WriteJson("ok")
}func TestStatusCode(t *testing.T) {
request, _ := http.NewRequest("POST", "http://localhost:8080/api/messages", nil)
response := httptest.NewRecorder()
api := main.Api{}
// api.CreateMessage(response, request)
if response.Code != http.StatusOK {
t.Fatalf("Non-expected status code%v:\n\tbody: %v", "200", response.Code)
}
}Metadata
Metadata
Assignees
Labels
No labels