package httphandlers_test

import (
	"io"
	"net/http"
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/httphandlers"
	"github.com/fiskerinc/cloud-services/pkg/testhelper"
)

func TestLogRequest(t *testing.T) {
	tests := []testhelper.BasicHttpTest{
		{
			Name:             "GET request",
			Request:          testhelper.MakeTestRequest(http.MethodGet, "/test", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `null`,
		},
		{
			Name:             "POST request",
			Request:          testhelper.MakeTestRequest(http.MethodPost, "/test", common.Car{VIN: "TESTVIN"}),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"vin":"TESTVIN"}`,
		},
	}

	echo := func(w http.ResponseWriter, r *http.Request) {
		b, err := io.ReadAll(r.Body)
		if err != nil {
			w.Write([]byte(err.Error()))
		} else {
			w.Write(b)
		}
	}
	handler := httphandlers.LogRequest(echo)

	for _, test := range tests {
		testhelper.RunBasicHttpTest(t, test, handler)
	}
}
