-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathafter_test.go
41 lines (34 loc) · 929 Bytes
/
after_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
package httpwrap
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestAfter(t *testing.T) {
t.Run("simple", func(t *testing.T) {
req := httptest.NewRequest("GET", "/test", nil)
rw := httptest.NewRecorder()
ctx := newRunCtx(rw, req, nopConstructor)
after, err := newAfter(func(w http.ResponseWriter, res any) {
require.NotNil(t, w)
w.WriteHeader(http.StatusOK)
})
require.NoError(t, err)
after.run(ctx)
require.Equal(t, http.StatusOK, rw.Result().StatusCode)
})
t.Run("with response", func(t *testing.T) {
req := httptest.NewRequest("GET", "/test", nil)
rw := httptest.NewRecorder()
ctx := newRunCtx(rw, req, nopConstructor)
ctx.response = reflect.ValueOf(1)
after, err := newAfter(func(w http.ResponseWriter, res any) {
require.NotNil(t, w)
require.Equal(t, res, 1)
})
require.NoError(t, err)
after.run(ctx)
})
}