test improvements and some refactor

This commit is contained in:
2024-11-20 16:01:55 +01:00
parent 774d1d28a0
commit eb9c8d955c
3 changed files with 142 additions and 72 deletions
+31
View File
@@ -1,5 +1,10 @@
package testhelpers
import (
"net/http/httptest"
"testing"
)
func CheckSlicesEquality(a []any, b []any) bool {
if len(a) != len(b) {
return false
@@ -31,3 +36,29 @@ func StringSliceToAnySlice(s []string) []any {
}
return result
}
type ExpectedResponse struct {
Code int
Header string
Body string
}
func VerifyResponse(t *testing.T, rr *httptest.ResponseRecorder, expectedResponse ExpectedResponse) {
t.Helper()
expectedCode := expectedResponse.Code
expectedHeader := expectedResponse.Header
expectedBody := expectedResponse.Body
if status := rr.Code; status != expectedCode {
t.Errorf("Expected status code: %d, Actual: %d", expectedCode, status)
}
if header := rr.Header().Get("Content-Type"); header != expectedHeader {
t.Errorf("Expected Content-Type: %s, Actual: %s", expectedHeader, header)
}
if body := rr.Body.String(); body != expectedBody {
t.Errorf("Expected body: %q, Actual: %q", expectedBody, body)
}
}