-
Notifications
You must be signed in to change notification settings - Fork 1
/
forward_request_test.go
92 lines (79 loc) · 1.79 KB
/
forward_request_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package traefik_plugin_forward_request
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestNew(t *testing.T) {
tests := []struct {
desc string
cfg *Config
}{
{
desc: "should return no error",
cfg: func() *Config {
c := CreateConfig()
c.URL = "https://example.com/" // nolint:goconst
return c
}(),
},
}
for _, test := range tests {
test := test // pin
t.Run(test.desc, func(t *testing.T) {
h, err := New(context.Background(), nil, test.cfg, "forwardrequest")
if err != nil {
t.Errorf("Received unexpected error:\n%+v", err)
}
if h == nil {
t.Errorf("Expected value not to be nil.")
}
})
}
}
func TestServeHTTP(t *testing.T) {
tests := []struct {
desc string
cfg *Config
expNextCall bool
expStatusCode int
}{
{
desc: "should return ok status",
cfg: func() *Config {
c := CreateConfig()
c.URL = "https://example.com/"
return c
}(),
expNextCall: true,
expStatusCode: http.StatusOK,
},
}
for _, test := range tests {
test := test // pin
t.Run(test.desc, func(t *testing.T) {
nextCall := false
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
nextCall = true
})
h, err := New(context.Background(), next, test.cfg, "forwardrequest")
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
url := "https://example.com/"
req := httptest.NewRequest(http.MethodPost, url, strings.NewReader("example"))
h.ServeHTTP(rec, req)
res := rec.Result()
defer res.Body.Close()
if nextCall != test.expNextCall {
t.Errorf("next handler should not be called")
}
if res.StatusCode != test.expStatusCode {
t.Errorf("got status code %d, want %d", rec.Code, test.expStatusCode)
}
})
}
}