Files
qv/cmd/web/testutils_test.go

46 lines
956 B
Go
Raw Normal View History

2024-12-28 17:58:01 +01:00
package main
import (
"code.dlmw.ch/dlmw/qv/internal/models"
"github.com/go-playground/form/v4"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"testing"
)
func newTestApplication(t *testing.T) *application {
formDecoder := form.NewDecoder()
return &application{
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
elections: &models.ElectionModel{},
formDecoder: formDecoder,
}
}
type testServer struct {
*httptest.Server
}
func newTestServer(t *testing.T, h http.Handler) *testServer {
server := httptest.NewServer(h)
return &testServer{server}
}
func (ts *testServer) post(t *testing.T, urlPath string, body io.Reader) (int, http.Header, string) {
res, err := ts.Client().Post(ts.URL+urlPath, "application/json", body)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
responseBody, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
return res.StatusCode, res.Header, string(responseBody)
}