-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresponse_test.go
67 lines (59 loc) · 1.69 KB
/
response_test.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package inertia
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo/v4"
)
func TestNewResponseWriterWrapper(t *testing.T) {
t.Run("should buffer status code", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
e := echo.New()
c := e.NewContext(req, rec)
var w *ResponseWriterWrapper
err := func(c echo.Context) error {
res := c.Response()
w = NewResponseWriterWrapper(res.Writer)
res.Writer = w
return c.Redirect(http.StatusFound, "/example")
}(c)
if err != nil {
t.Fatal(err)
}
if rec.Code != http.StatusOK {
// status code 200, because the actual status code is buffered and not sent.
t.Errorf("expected status code to be 200, got %d", rec.Code)
}
w.FlushHeader()
if rec.Code != http.StatusFound {
// you will get buffered status code after FlushHeader.
t.Errorf("expected status code to be 302, got %d", rec.Code)
}
})
t.Run("should NOT buffer status code", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
e := echo.New()
c := e.NewContext(req, rec)
var w *ResponseWriterWrapper
err := func(c echo.Context) error {
res := c.Response()
w = NewResponseWriterWrapper(res.Writer)
res.Writer = w
return c.String(http.StatusNotFound, "/example")
}(c)
if err != nil {
t.Fatal(err)
}
if rec.Code != http.StatusNotFound {
// status code 404, 404 is not buffered.
t.Errorf("expected status code to be 404, got %d", rec.Code)
}
w.FlushHeader()
if rec.Code != http.StatusNotFound {
// no effect by FlushHeader
t.Errorf("expected status code to be 404, got %d", rec.Code)
}
})
}