-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregression_test.go
76 lines (64 loc) · 1.63 KB
/
regression_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
package nape_test
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/muir/nape"
"github.com/stretchr/testify/assert"
)
type RequestBody []byte
func LogError(inner func() error) {
// actually, ignore error
_ = inner()
}
func SaveRequest(inner func(RequestBody) error, r *http.Request) error {
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(buf))
return inner(buf)
}
func TestSaveRequest(t *testing.T) {
t.Parallel()
calledPost := false
calledGet := false
s := nape.PreregisterServiceWithMux("TestSaveRequest", LogError, SaveRequest)
s.RegisterEndpoint("/ept", func(body RequestBody, w http.ResponseWriter) error {
w.WriteHeader(204)
assert.Equal(t, "some stuff", string(body))
calledPost = true
return nil
}).Methods("POST")
s.RegisterEndpoint("/ept", func(w http.ResponseWriter) error {
w.WriteHeader(204)
calledGet = true
return nil
}).Methods("GET")
muxRouter := mux.NewRouter()
assert.False(t, calledPost)
assert.False(t, calledGet)
s.Start(muxRouter)
assert.False(t, calledPost)
assert.False(t, calledGet)
localServer := httptest.NewServer(muxRouter)
defer localServer.Close()
// nolint:noctx
resp, err := http.Post(localServer.URL+"/ept", "text/plain", ioutil.NopCloser(bytes.NewBuffer([]byte("some stuff"))))
assert.NoError(t, err)
assert.True(t, calledPost)
assert.False(t, calledGet)
if resp != nil {
resp.Body.Close()
}
// nolint:noctx
resp, err = http.Get(localServer.URL + "/ept")
assert.NoError(t, err)
assert.True(t, calledGet)
if resp != nil {
resp.Body.Close()
}
}