package httphandlers

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/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
}
