-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_handler_test.go
57 lines (47 loc) · 1.65 KB
/
request_handler_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
package gohttpclient
import (
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func TestChainRequestHandlers(t *testing.T) {
var result []string
handler1 := func(req *http.Request, handlerFunc RequestHandlerFunc) (*http.Response, error) {
result = append(result, "handler1")
return handlerFunc(req)
}
handler2 := func(req *http.Request, handlerFunc RequestHandlerFunc) (*http.Response, error) {
result = append(result, "handler2")
return handlerFunc(req)
}
handler3 := func(req *http.Request, handlerFunc RequestHandlerFunc) (*http.Response, error) {
result = append(result, "handler3")
return handlerFunc(req)
}
handlerFunc := func(req *http.Request) (resp *http.Response, err error) {
result = append(result, "handlerFunc")
return &http.Response{}, nil
}
handler := ChainRequestHandlers(handler1, handler2, handler3)
req, _ := http.NewRequest(http.MethodGet, "https://example.com", nil)
resp, err := handler(req, handlerFunc)
require.Nil(t, err)
require.NotNil(t, resp)
require.Equal(t, []string{"handler1", "handler2", "handler3", "handlerFunc"}, result)
}
func TestChainRequestHandlers_NoHandler(t *testing.T) {
handler := ChainRequestHandlers()
require.NotNil(t, handler)
}
func TestChainRequestHandlers_OneHandler(t *testing.T) {
handler1 := func(req *http.Request, handlerFunc RequestHandlerFunc) (*http.Response, error) {
return handlerFunc(req)
}
handler := ChainRequestHandlers(handler1)
require.NotNil(t, handler)
}
func TestGetRequestContext(t *testing.T) {
require.NotNil(t, getRequestContext(nil))
req, _ := http.NewRequest(http.MethodGet, "https://example.com", nil)
require.NotNil(t, getRequestContext(req))
}