saasitone/pkg/handlers/router_test.go

138 lines
2.7 KiB
Go
Raw Permalink Normal View History

package handlers
2021-12-05 17:22:45 -08:00
import (
"net/http"
"net/http/cookiejar"
2021-12-05 17:22:45 -08:00
"net/http/httptest"
"net/url"
2021-12-05 17:22:45 -08:00
"os"
"testing"
"github.com/PuerkitoBio/goquery"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2024-07-09 17:57:05 -07:00
"git.grosinger.net/tgrosinger/saasitone/config"
"git.grosinger.net/tgrosinger/saasitone/pkg/services"
2021-12-05 17:22:45 -08:00
)
var (
srv *httptest.Server
2021-12-18 07:07:12 -08:00
c *services.Container
2021-12-05 17:22:45 -08:00
)
func TestMain(m *testing.M) {
2021-12-13 09:51:00 -08:00
// Set the environment to test
2021-12-14 17:17:09 -08:00
config.SwitchEnvironment(config.EnvTest)
2021-12-13 09:51:00 -08:00
2021-12-19 11:56:00 -08:00
// Start a new container
2021-12-18 07:07:12 -08:00
c = services.NewContainer()
2021-12-19 11:56:00 -08:00
// Start a test HTTP server
if err := BuildRouter(c); err != nil {
panic(err)
}
2021-12-05 17:22:45 -08:00
srv = httptest.NewServer(c.Web)
2021-12-14 17:14:11 -08:00
// Run tests
2021-12-05 17:22:45 -08:00
exitVal := m.Run()
// Shutdown the container and test server
if err := c.Shutdown(); err != nil {
panic(err)
}
2021-12-05 17:22:45 -08:00
srv.Close()
2021-12-05 17:22:45 -08:00
os.Exit(exitVal)
}
2021-12-06 07:38:55 -08:00
type httpRequest struct {
route string
client http.Client
body url.Values
t *testing.T
}
func request(t *testing.T) *httpRequest {
jar, err := cookiejar.New(nil)
require.NoError(t, err)
2021-12-06 07:38:55 -08:00
r := httpRequest{
t: t,
body: url.Values{},
client: http.Client{
Jar: jar,
},
2021-12-06 07:38:55 -08:00
}
return &r
}
func (h *httpRequest) setClient(client http.Client) *httpRequest {
h.client = client
return h
}
func (h *httpRequest) setRoute(route string, params ...any) *httpRequest {
2021-12-06 07:38:55 -08:00
h.route = srv.URL + c.Web.Reverse(route, params)
return h
}
func (h *httpRequest) setBody(body url.Values) *httpRequest {
h.body = body
return h
}
func (h *httpRequest) get() *httpResponse {
resp, err := h.client.Get(h.route)
require.NoError(h.t, err)
r := httpResponse{
t: h.t,
Response: resp,
}
return &r
}
func (h *httpRequest) post() *httpResponse {
// Make a get request to get the CSRF token
doc := h.get().
assertStatusCode(http.StatusOK).
toDoc()
// Extract the CSRF and include it in the POST request body
csrf := doc.Find(`input[name="csrf"]`).First()
token, exists := csrf.Attr("value")
assert.True(h.t, exists)
h.body["csrf"] = []string{token}
// Make the POST requests
2021-12-06 07:38:55 -08:00
resp, err := h.client.PostForm(h.route, h.body)
require.NoError(h.t, err)
r := httpResponse{
t: h.t,
Response: resp,
}
return &r
}
2021-12-06 04:42:20 -08:00
type httpResponse struct {
*http.Response
t *testing.T
2021-12-05 17:22:45 -08:00
}
2021-12-06 04:42:20 -08:00
func (h *httpResponse) assertStatusCode(code int) *httpResponse {
assert.Equal(h.t, code, h.Response.StatusCode)
return h
}
func (h *httpResponse) assertRedirect(t *testing.T, route string, params ...any) *httpResponse {
assert.Equal(t, c.Web.Reverse(route, params), h.Header.Get("Location"))
2021-12-06 04:42:20 -08:00
return h
}
func (h *httpResponse) toDoc() *goquery.Document {
doc, err := goquery.NewDocumentFromReader(h.Body)
require.NoError(h.t, err)
err = h.Body.Close()
assert.NoError(h.t, err)
2021-12-05 17:22:45 -08:00
return doc
}