43 lines
964 B
Go
43 lines
964 B
Go
package httphandlers_test
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"fiskerinc.com/modules/common"
|
|
"fiskerinc.com/modules/httphandlers"
|
|
"fiskerinc.com/modules/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)
|
|
}
|
|
}
|