Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
35
pkg/httphandlers/method_checker_test.go
Normal file
35
pkg/httphandlers/method_checker_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user