-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
42 lines (34 loc) · 826 Bytes
/
example_test.go
File metadata and controls
42 lines (34 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package example
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/goflash/flash/v2"
)
func TestExampleMiddleware(t *testing.T) {
app := flash.New()
called := 0
// Register middleware under test
app.Use(ExampleMiddleware)
// Register a simple handler to verify next() is called
app.GET("/", func(c flash.Ctx) error {
called++
return c.String(http.StatusOK, "ok")
})
// Perform request
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
app.ServeHTTP(rec, req)
// Assertions
if rec.Code != http.StatusOK {
t.Fatalf("unexpected status: %d", rec.Code)
}
body, _ := io.ReadAll(rec.Body)
if string(body) != "ok" {
t.Fatalf("unexpected body: %q", string(body))
}
if called != 1 {
t.Fatalf("handler should be called once, got %d", called)
}
}