-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
160 lines (145 loc) · 4.05 KB
/
response_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
157
158
159
160
package acomm_test
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
log "github.com/Sirupsen/logrus"
"github.com/mistifyio/acomm"
"github.com/pborman/uuid"
"github.com/stretchr/testify/suite"
)
type ResponseTestSuite struct {
suite.Suite
Responses chan *acomm.Response
}
func (s *ResponseTestSuite) SetupSuite() {
log.SetLevel(log.FatalLevel)
s.Responses = make(chan *acomm.Response, 10)
}
func TestResponseTestSuite(t *testing.T) {
suite.Run(t, new(ResponseTestSuite))
}
func (s *ResponseTestSuite) NextResp() *acomm.Response {
return nextResp(s.Responses)
}
func (s *ResponseTestSuite) TestNewResponse() {
result := map[string]string{
"foo": "bar",
}
request, _ := acomm.NewRequest("foobar", "unix://foo", nil, nil, nil)
respErr := errors.New("foobar")
tests := []struct {
description string
request *acomm.Request
result interface{}
err error
expectedErr bool
}{
{"missing request", nil, result, nil, true},
{"missing result and error", request, nil, nil, false},
{"result and error", request, result, respErr, true},
{"result only", request, result, nil, false},
{"error only", request, nil, respErr, false},
}
for _, test := range tests {
msg := testMsgFunc(test.description)
resp, err := acomm.NewResponse(test.request, test.result, nil, test.err)
if test.expectedErr {
s.Error(err, msg("should have failed"))
s.Nil(resp, msg("should not have returned a response"))
} else {
if !s.NoError(err, msg("should have succeeded")) {
s.T().Log(msg(err.Error()))
continue
}
if !s.NotNil(resp, msg("should have returned a response")) {
continue
}
s.Equal(test.request.ID, resp.ID, msg("should have set an ID"))
var result map[string]string
s.NoError(resp.UnmarshalResult(&result))
if test.result == nil {
s.Nil(result, msg("should have nil result"))
} else {
s.Equal(test.result, result, msg("should have set the result"))
}
s.Equal(test.err, resp.Error, msg("should have set the error"))
}
}
}
func (s *ResponseTestSuite) TestSend() {
// Mock HTTP response server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp := &acomm.Response{}
body, err := ioutil.ReadAll(r.Body)
s.NoError(err, "should not fail reading body")
s.NoError(json.Unmarshal(body, resp), "should not fail unmarshalling response")
ack, _ := json.Marshal(&acomm.Response{})
_, _ = w.Write(ack)
s.Responses <- resp
}))
defer ts.Close()
// Mock Unix response listener
f, err := ioutil.TempFile("", "acommTest-")
if !s.NoError(err, "failed to create test unix socket") {
return
}
_ = f.Close()
_ = os.Remove(f.Name())
socketPath := fmt.Sprintf("%s.sock", f.Name())
listener, err := net.Listen("unix", socketPath)
if !s.NoError(err, "failed to listen on unix socket") {
return
}
defer func() { _ = listener.Close() }()
go func() {
for {
conn, err := listener.Accept()
if err != nil {
return
}
resp := &acomm.Response{}
s.NoError(acomm.UnmarshalConnData(conn, resp), "should not fail unmarshalling conn data")
_ = acomm.SendConnData(conn, &acomm.Response{})
s.Responses <- resp
_ = conn.Close()
}
}()
resultJ, _ := json.Marshal(map[string]string{"foo": "bar"})
response := &acomm.Response{
ID: uuid.New(),
Result: (*json.RawMessage)(&resultJ),
}
tests := []struct {
responseHook string
expectedErr bool
}{
{ts.URL, false},
{"http://badpath", true},
{fmt.Sprintf("unix://%s", socketPath), false},
{fmt.Sprintf("unix://%s", "badpath"), true},
{"foobar://", true},
}
for _, test := range tests {
msg := testMsgFunc(test.responseHook)
u, _ := url.ParseRequestURI(test.responseHook)
err := acomm.Send(u, response)
resp := s.NextResp()
if test.expectedErr {
s.Error(err, msg("send should fail"))
s.Nil(resp, msg("response hook should not receive a response"))
} else {
if !s.NoError(err, msg("send should not fail")) {
continue
}
s.Equal(response.ID, resp.ID, msg("response should be what was sent"))
}
}
}