-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
config_test.go
156 lines (125 loc) · 3.57 KB
/
config_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package mocha
import (
"log"
"net/http"
"os"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/vitorsalgado/mocha/v3/expect"
"github.com/vitorsalgado/mocha/v3/internal/headers"
"github.com/vitorsalgado/mocha/v3/internal/mimetypes"
"github.com/vitorsalgado/mocha/v3/internal/testutil"
"github.com/vitorsalgado/mocha/v3/reply"
)
type testBodyParser struct{}
func (p testBodyParser) CanParse(content string, r *http.Request) bool {
return content == mimetypes.TextPlain && r.Header.Get("x-test") == "num"
}
func (p testBodyParser) Parse(body []byte, _ *http.Request) (any, error) {
return strconv.Atoi(string(body))
}
type customTestServer struct {
decorated Server
}
func (s *customTestServer) Configure(config Config, handler http.Handler) error {
return s.decorated.Configure(config, handler)
}
func (s *customTestServer) Start() (ServerInfo, error) {
return s.decorated.Start()
}
func (s *customTestServer) StartTLS() (ServerInfo, error) {
return s.decorated.StartTLS()
}
func (s *customTestServer) Close() error {
return s.decorated.Close()
}
func (s *customTestServer) Info() ServerInfo {
return s.decorated.Info()
}
func TestConfig(t *testing.T) {
t.Run("should run server with the custom given address", func(t *testing.T) {
addr := os.Getenv("TEST_CUSTOM_ADDR")
if addr == "" {
addr = "127.0.0.1:3000"
}
m := New(t, Configure().
Addr(addr).
Build()).
CloseOnCleanup(t)
m.Start()
scoped := m.AddMocks(
Get(expect.URLPath("/test")).
Reply(reply.OK()))
req := testutil.Get(m.URL() + "/test")
res, err := req.Do()
if err != nil {
log.Fatal(err)
}
assert.True(t, scoped.Called())
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Contains(t, m.server.Info().URL, addr)
})
t.Run("request body parsers from config should take precedence", func(t *testing.T) {
m := New(t, Configure().
RequestBodyParsers(&testBodyParser{}).
Build())
m.Start()
scoped := m.AddMocks(Post(expect.URLPath("/test")).
Body(expect.ToEqual(10)).
Reply(reply.OK()))
req := testutil.Post(m.URL()+"/test", strings.NewReader("10"))
req.Header(headers.ContentType, mimetypes.TextPlain)
req.Header("x-test", "num")
res, err := req.Do()
if err != nil {
log.Fatal(err)
}
assert.True(t, scoped.Called())
assert.Equal(t, http.StatusOK, res.StatusCode)
})
t.Run("middlewares from config should take precedence", func(t *testing.T) {
msg := "hello world"
middleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("intercepted", "true")
w.Header().Add(headers.ContentType, mimetypes.TextPlain)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(msg))
})
}
m := New(t, Configure().
Middlewares(middleware).
Build())
m.Start()
scoped := m.AddMocks(
Get(expect.URLPath("/test")).
Reply(reply.OK()))
req := testutil.Get(m.URL() + "/test")
res, err := req.Do()
if err != nil {
log.Fatal(err)
}
assert.False(t, scoped.Called())
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
assert.Equal(t, "true", res.Header.Get("intercepted"))
})
t.Run("configure custom server", func(t *testing.T) {
m := New(t, Configure().
Server(&customTestServer{decorated: newServer()}).
Build())
m.Start()
scoped := m.AddMocks(
Get(expect.URLPath("/test")).
Reply(reply.OK()))
req := testutil.Get(m.URL() + "/test")
res, err := req.Do()
if err != nil {
log.Fatal(err)
}
assert.True(t, scoped.Called())
assert.Equal(t, http.StatusOK, res.StatusCode)
})
}