-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve_test.go
105 lines (90 loc) · 2.75 KB
/
serve_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
package caddyslack
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
"github.com/stretchr/testify/assert"
)
func newTestHandler(t *testing.T, caddyFile string) *handler {
c := caddy.NewTestController("http", caddyFile)
slackConfig, err := parse(c)
assert.NoError(t, err)
h := newHandler(slackConfig)
h.Next = httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
return http.StatusTeapot, nil
})
return h
}
//newTestTarget listens until timeout and returns on the first incoming request
func newTestTarget(addr string, timeoutSec time.Duration, readyChan chan struct{}) (*http.Request, error) {
reqCh := make(chan *http.Request)
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusOK)
reqCh <- req
})
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
go func() {
close(readyChan)
http.Serve(l, nil)
}()
select {
case req := <-reqCh:
return req, nil
case <-time.After(time.Second * timeoutSec):
return nil, fmt.Errorf("Timeout after %d seconds", timeoutSec)
}
}
func TestServeHTTP_ShouldForwardEmptyRequest(t *testing.T) {
h := newTestHandler(t, `slack {
url http://localhost:9997
}`)
req, err := http.NewRequest("POST", "/slack", nil)
assert.NoError(t, err)
readyChan := make(chan struct{})
go func() {
requestToSlack, targetErr := newTestTarget("localhost:9997", 1, readyChan)
assert.NoError(t, targetErr)
bodyToSlack, targetErr := ioutil.ReadAll(requestToSlack.Body)
assert.NoError(t, targetErr)
assert.Empty(t, bodyToSlack)
}()
w := httptest.NewRecorder()
<-readyChan
statusForCaddy, err := h.ServeHTTP(w, req)
assert.NoError(t, err)
assert.Exactly(t, statusForCaddy, StatusEmpty)
bytes, err := ioutil.ReadAll(w.Result().Body)
assert.NoError(t, err)
assert.Exactly(t, w.Code, http.StatusOK, string(bytes))
}
/*func TestServeHTTP_ShouldForwardRequestUnmodified(t *testing.T) {
h := newTestHandler(t, `slack {
url http://localhost:9998
}`)
jsonStr := []byte(`{"text":"hello"}`)
req, err := http.NewRequest("POST", "/slack", bytes.NewBuffer(jsonStr))
assert.NoError(t, err)
go func() {
requestToSlack, targetErr := newTestTarget("localhost:9998", 1)
assert.NoError(t, targetErr)
bodyToSlack, targetErr := ioutil.ReadAll(requestToSlack.Body)
assert.NoError(t, targetErr)
assert.Equal(t, bodyToSlack, jsonStr)
}()
w := httptest.NewRecorder()
statusForCaddy, err := h.ServeHTTP(w, req)
assert.NoError(t, err)
assert.Exactly(t, statusForCaddy, StatusEmpty)
bytes, err := ioutil.ReadAll(w.Result().Body)
assert.NoError(t, err)
assert.Exactly(t, w.Code, http.StatusOK, string(bytes))
}*/