-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware_pre_process_test.go
45 lines (38 loc) · 1.04 KB
/
middleware_pre_process_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
package uhttp_test
import (
"context"
"errors"
"net/http"
"testing"
"github.com/dunv/uhttp"
"github.com/stretchr/testify/require"
)
func TestPreProcess(t *testing.T) {
u := uhttp.NewUHTTP()
originalMessage := "??"
handler := uhttp.NewHandler(
uhttp.WithPreProcess(func(ctx context.Context) error {
originalMessage = "world"
return nil
}),
uhttp.WithGet(func(r *http.Request, ret *int) interface{} {
return map[string]string{"hello": originalMessage}
}),
)
u.Handle("/test", handler)
RequireHTTPBodyJSONEq(t, u.ServeMux().ServeHTTP, http.MethodGet, "/test", nil, `{"hello": "world"}`)
}
func TestPreError(t *testing.T) {
u := uhttp.NewUHTTP()
originalMessage := "??"
handler := uhttp.NewHandler(
uhttp.WithPreProcess(func(ctx context.Context) error {
return errors.New("did not work")
}),
uhttp.WithGet(func(r *http.Request, ret *int) interface{} {
return map[string]string{"hello": originalMessage}
}),
)
u.Handle("/test", handler)
require.HTTPError(t, u.ServeMux().ServeHTTP, http.MethodGet, "/test", nil)
}