diff --git a/internal/api/handler_test.go b/internal/api/handler_test.go index 4769929..69bf845 100644 --- a/internal/api/handler_test.go +++ b/internal/api/handler_test.go @@ -15,76 +15,87 @@ import ( "github.com/jackc/pgx/v5" ) -func withServer(f func(handler oas.Handler)) { +func prepare(t *testing.T) oas.Handler { name := fmt.Sprintf("test%d", time.Now().Unix()) - ctx := context.TODO() - root, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL")) + connCfg, err := pgx.ParseConfig(os.Getenv("DATABASE_URL")) if err != nil { - log.Fatal(err) + t.Fatal(err) } + ctx := context.TODO() + root, err := pgx.ConnectConfig(ctx, connCfg) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + root.Close(ctx) + }) + if _, err = root.Exec(ctx, "CREATE DATABASE "+name+";"); err != nil { - log.Fatal("failed to create database", name) + t.Fatal("failed to create database", name) } - defer func() { + t.Cleanup(func() { root.Exec(ctx, "DROP DATABASE "+name+";") - }() + }) - // TODO: substitute from DATABASE_URL instead - conn, err := pgx.Connect(ctx, "postgres://short:short@localhost:5432/"+name) + connCfg.Database = name + conn, err := pgx.ConnectConfig(ctx, connCfg) if err != nil { log.Fatal(err) } + t.Cleanup(func() { + conn.Close(ctx) + }) schema, err := os.ReadFile("../../sql/schema.sql") if err != nil { - log.Fatal(err) + t.Fatal(err) } conn.Exec(ctx, string(schema)) - f(api.NewHandler("http://test", db.New(conn))) - conn.Close(ctx) + return api.NewHandler("http://test", db.New(conn)) } func TestCreateURL(t *testing.T) { + h := prepare(t) + cases := []string{ "http://test.com", "http://github.com", "abcdef", // FIXME: should not pass but it works... } - withServer(func(h oas.Handler) { - for _, url := range cases { - shortenRes, err := h.CreateShortURL(context.TODO(), &oas.CreateShortURLReq{ - URL: url, - }) - if err != nil { - t.Errorf("failed to create url %q: %v", url, err) - continue - } - shorten, ok := shortenRes.(*oas.CreateShortURLCreated) - if !ok { - t.Errorf("expected 201 status code, got 400 for url %q", url) - continue - } - - res, err := h.RedirectLongURL(context.TODO(), oas.RedirectLongURLParams{ - Hash: strings.TrimPrefix(shorten.Shorten, "http://test/"), - }) - if err != nil { - t.Errorf("failed to get redirect url for %q: %v", url, err) - continue - } - - redirect, ok := res.(*oas.RedirectLongURLTemporaryRedirect) - if !ok { - t.Errorf("expected 307 status code, got 404 for url %q with shorten %q", url, shorten.Shorten) - } - - if redirect.Location.Value != url { - t.Errorf("supplied %q but got %q during redirect", url, redirect.Location.Value) - } + for _, url := range cases { + shortenRes, err := h.CreateShortURL(context.TODO(), &oas.CreateShortURLReq{ + URL: url, + }) + if err != nil { + t.Errorf("failed to create url %q: %v", url, err) + continue } - }) + + shorten, ok := shortenRes.(*oas.CreateShortURLCreated) + if !ok { + t.Errorf("expected 201 status code, got 400 for url %q", url) + continue + } + + res, err := h.RedirectLongURL(context.TODO(), oas.RedirectLongURLParams{ + Hash: strings.TrimPrefix(shorten.Shorten, "http://test/"), + }) + if err != nil { + t.Errorf("failed to get redirect url for %q: %v", url, err) + continue + } + + redirect, ok := res.(*oas.RedirectLongURLTemporaryRedirect) + if !ok { + t.Errorf("expected 307 status code, got 404 for url %q with shorten %q", url, shorten.Shorten) + } + + if redirect.Location.Value != url { + t.Errorf("supplied %q but got %q during redirect", url, redirect.Location.Value) + } + } }