package test import ( "bytes" "context" "magmise/models" "net/http" "net/http/httptest" "testing" "github.com/go-chi/chi/v5" ) func Setup(t *testing.T, name string) func() { t.Log("Setup " + name) models.Init() //models.DB().Exec("DROP TABLE ticketapplication_answers") models.DB().Migrator().DropTable(models.User{}) models.DB().AutoMigrate(models.User{}) models.User{Username: "fred", Password: "a"}.Create() return func() { t.Log("Cleanup " + name) models.DB().Migrator().DropTable(models.User{}) } } func Request(t *testing.T, model http.HandlerFunc, method, path, _json string, urlparams map[string]string) *httptest.ResponseRecorder { handler := http.HandlerFunc(model) r, err := http.NewRequest("GET", "/user/{username}", bytes.NewBufferString(_json)) if err != nil { t.Fatal(err) } rctx := chi.NewRouteContext() for key, value := range urlparams { rctx.URLParams.Add(key, value) } r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx)) w := httptest.NewRecorder() handler(w, r) if w.Code != 200 { t.Fail() } return w }