Files
cloud-services/pkg/httphandlers/method_checker_test.go

36 lines
902 B
Go

package httphandlers
import (
"net/http"
"net/http/httptest"
"testing"
"fiskerinc.com/modules/testhelper"
)
func TestCheckMethod(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
testCheckMethod(t, CheckMethod(http.MethodGet, handler), http.StatusOK)
testCheckMethod(t, CheckMethod(MethodAll, handler), http.StatusOK)
testCheckMethod(t, CheckMethod(http.MethodPost, handler), http.StatusBadRequest)
}
func testCheckMethod(t *testing.T, fn http.HandlerFunc, expectedStatusCode int) {
req := setupAuthorizeRequest()
recorder := httptest.NewRecorder()
fn(recorder, req)
if recorder.Code != expectedStatusCode {
t.Errorf(testhelper.TestErrorTemplate, "Status code", expectedStatusCode, recorder.Code)
}
}
func setupAuthorizeRequest() *http.Request {
req, _ := http.NewRequest("GET", "http://example.com", nil)
return req
}