|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + _ "modernc.org/sqlite" |
| 12 | +) |
| 13 | + |
| 14 | +func setupTestDB(t *testing.T) *sql.DB { |
| 15 | + t.Helper() |
| 16 | + db, err := sql.Open("sqlite", ":memory:") |
| 17 | + if err != nil { |
| 18 | + t.Fatal(err) |
| 19 | + } |
| 20 | + if err := initDB(db); err != nil { |
| 21 | + t.Fatal(err) |
| 22 | + } |
| 23 | + t.Cleanup(func() { db.Close() }) |
| 24 | + return db |
| 25 | +} |
| 26 | + |
| 27 | +func postSubscribe(handler http.HandlerFunc, body string) *httptest.ResponseRecorder { |
| 28 | + req := httptest.NewRequest("POST", "/api/subscribe", strings.NewReader(body)) |
| 29 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 30 | + w := httptest.NewRecorder() |
| 31 | + handler(w, req) |
| 32 | + return w |
| 33 | +} |
| 34 | + |
| 35 | +type response struct { |
| 36 | + OK bool `json:"ok"` |
| 37 | + Error string `json:"error,omitempty"` |
| 38 | +} |
| 39 | + |
| 40 | +func parseResponse(t *testing.T, w *httptest.ResponseRecorder) response { |
| 41 | + t.Helper() |
| 42 | + var r response |
| 43 | + if err := json.Unmarshal(w.Body.Bytes(), &r); err != nil { |
| 44 | + t.Fatalf("invalid json: %s", w.Body.String()) |
| 45 | + } |
| 46 | + return r |
| 47 | +} |
| 48 | + |
| 49 | +func subscriberCount(t *testing.T, db *sql.DB) int { |
| 50 | + t.Helper() |
| 51 | + var count int |
| 52 | + if err := db.QueryRow("SELECT COUNT(*) FROM subscribers").Scan(&count); err != nil { |
| 53 | + t.Fatal(err) |
| 54 | + } |
| 55 | + return count |
| 56 | +} |
| 57 | + |
| 58 | +func TestSubscribe(t *testing.T) { |
| 59 | + db := setupTestDB(t) |
| 60 | + handler := makeHandler(db) |
| 61 | + |
| 62 | + t.Run("valid email", func(t *testing.T) { |
| 63 | + w := postSubscribe(handler, "email=test@example.com") |
| 64 | + if w.Code != 200 { |
| 65 | + t.Fatalf("expected 200, got %d", w.Code) |
| 66 | + } |
| 67 | + r := parseResponse(t, w) |
| 68 | + if !r.OK { |
| 69 | + t.Fatal("expected ok=true") |
| 70 | + } |
| 71 | + if n := subscriberCount(t, db); n != 1 { |
| 72 | + t.Fatalf("expected 1 subscriber, got %d", n) |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + t.Run("duplicate email", func(t *testing.T) { |
| 77 | + w := postSubscribe(handler, "email=dupe@example.com") |
| 78 | + if w.Code != 200 { |
| 79 | + t.Fatalf("expected 200, got %d", w.Code) |
| 80 | + } |
| 81 | + before := subscriberCount(t, db) |
| 82 | + w = postSubscribe(handler, "email=dupe@example.com") |
| 83 | + if w.Code != 200 { |
| 84 | + t.Fatalf("expected 200, got %d", w.Code) |
| 85 | + } |
| 86 | + r := parseResponse(t, w) |
| 87 | + if !r.OK { |
| 88 | + t.Fatal("expected ok=true for duplicate") |
| 89 | + } |
| 90 | + after := subscriberCount(t, db) |
| 91 | + if after != before { |
| 92 | + t.Fatalf("duplicate inserted: before=%d after=%d", before, after) |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + t.Run("missing email", func(t *testing.T) { |
| 97 | + w := postSubscribe(handler, "") |
| 98 | + if w.Code != 400 { |
| 99 | + t.Fatalf("expected 400, got %d", w.Code) |
| 100 | + } |
| 101 | + r := parseResponse(t, w) |
| 102 | + if r.OK { |
| 103 | + t.Fatal("expected ok=false") |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + t.Run("invalid email", func(t *testing.T) { |
| 108 | + w := postSubscribe(handler, "email=notanemail") |
| 109 | + if w.Code != 400 { |
| 110 | + t.Fatalf("expected 400, got %d", w.Code) |
| 111 | + } |
| 112 | + r := parseResponse(t, w) |
| 113 | + if r.OK { |
| 114 | + t.Fatal("expected ok=false") |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + t.Run("email too long", func(t *testing.T) { |
| 119 | + long := "email=" + strings.Repeat("a", 250) + "@b.com" |
| 120 | + w := postSubscribe(handler, long) |
| 121 | + if w.Code != 400 { |
| 122 | + t.Fatalf("expected 400, got %d", w.Code) |
| 123 | + } |
| 124 | + }) |
| 125 | + |
| 126 | + t.Run("wrong method", func(t *testing.T) { |
| 127 | + req := httptest.NewRequest("GET", "/api/subscribe", nil) |
| 128 | + w := httptest.NewRecorder() |
| 129 | + handler(w, req) |
| 130 | + if w.Code != 405 { |
| 131 | + t.Fatalf("expected 405, got %d", w.Code) |
| 132 | + } |
| 133 | + }) |
| 134 | +} |
0 commit comments