You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I get a 201 with "done". But I want to ensure this endpoint only accepts the content type "application/json", otherwise send back a 415.
Unfortunately the middleware is not working yet. I also tried to test the behaviour with the testrunner
package main
import (
"net/http""net/http/httptest""strconv""testing"
)
funcTestHandleRoute(suite*testing.T) {
server:=httptest.NewServer(HandleRoute())
suite.Run("responds with status code "+strconv.Itoa(http.StatusUnsupportedMediaType)+" if content type is not application/json", func(testing*testing.T) {
response, _:=http.Post(server.URL, "text/xml", nil)
ifresponse.StatusCode!=http.StatusUnsupportedMediaType {
testing.Errorf("Expected statuscode %d but got %d", http.StatusUnsupportedMediaType, response.StatusCode)
}
})
}
Unfortunately the test fails with the message
main_test.go:17: Expected statuscode 415 but got 201
so it seems the middleware didn't run.
Is my setup wrong or is it a bug?
The text was updated successfully, but these errors were encountered:
@jtuchel, your unit test setup is wrong. httptest is intended to test the handler rather than the HTTP server itself. So basically you just test the HandleRoute instead of the whole server logic.
You can use TestMain to serve your server or stub the chi router with the handler.
Given this sample code
When calling the API via
I get a 201 with "done". But I want to ensure this endpoint only accepts the content type "application/json", otherwise send back a 415.
Unfortunately the middleware is not working yet. I also tried to test the behaviour with the testrunner
Unfortunately the test fails with the message
so it seems the middleware didn't run.
Is my setup wrong or is it a bug?
The text was updated successfully, but these errors were encountered: