Write test skeleton
This commit is contained in:
45
cmd/web/testutils_test.go
Normal file
45
cmd/web/testutils_test.go
Normal file
@ -0,0 +1,45 @@
|
||||
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)
|
||||
}
|
Reference in New Issue
Block a user