This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplain_http_producer_test.go
281 lines (271 loc) · 9.82 KB
/
plain_http_producer_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/Financial-Times/go-logger"
queueProducer "github.com/Financial-Times/message-queue-go-producer/producer"
"github.com/stretchr/testify/assert"
)
func TestSendMessage(t *testing.T) {
logger.InitDefaultLogger("kafka-bridge")
var tests = []struct {
config queueProducer.MessageProducerConfig
uuid string
message queueProducer.Message
expectedHeaders map[string]string
}{
{ //happy flow
queueProducer.MessageProducerConfig{
Addr: "address",
Authorization: "authorizationkey",
},
"",
queueProducer.Message{
Headers: map[string]string{
"Message-Id": "fc429b46-2500-4fe7-88bb-fd507fbaf00c",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"Message-Type": "cms-content-published",
"Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"Content-Type": "application/json",
"X-Request-Id": "t9happe59y",
},
Body: `{"uuid":"7543220a-2389-11e5-bd83-71cb60e8f08c","type":"EOM::CompoundStory","value":"test"}`},
map[string]string{
"X-Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"X-Request-Id": "t9happe59y",
"Authorization": "authorizationkey",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"Content-Type": "application/json",
},
},
{ //missing content-type
queueProducer.MessageProducerConfig{
Addr: "address",
Authorization: "authorizationkey",
},
"",
queueProducer.Message{
Headers: map[string]string{
"Message-Id": "fc429b46-2500-4fe7-88bb-fd507fbaf00c",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"Message-Type": "cms-content-published",
"Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"X-Request-Id": "t9happe59y",
},
Body: `{"uuid":"7543220a-2389-11e5-bd83-71cb60e8f08c","type":"EOM::CompoundStory","value":"test"}`},
map[string]string{
"X-Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"X-Request-Id": "t9happe59y",
"Authorization": "authorizationkey",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
},
},
{ //authorization missing
queueProducer.MessageProducerConfig{
Addr: "address",
},
"",
queueProducer.Message{
Headers: map[string]string{
"Message-Id": "fc429b46-2500-4fe7-88bb-fd507fbaf00c",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"Message-Type": "cms-content-published",
"Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"Content-Type": "application/json",
"X-Request-Id": "t9happe59y",
},
Body: `{"uuid":"7543220a-2389-11e5-bd83-71cb60e8f08c","type":"EOM::CompoundStory","value":"test"}`},
map[string]string{
"X-Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"X-Request-Id": "t9happe59y",
"Authorization": "",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"Content-Type": "application/json",
},
},
{ //host header (queue) is missing
queueProducer.MessageProducerConfig{
Addr: "address",
},
"",
queueProducer.Message{
Headers: map[string]string{
"Message-Id": "fc429b46-2500-4fe7-88bb-fd507fbaf00c",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"Message-Type": "cms-content-published",
"Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"Content-Type": "application/json",
"X-Request-Id": "t9happe59y",
},
Body: `{"uuid":"7543220a-2389-11e5-bd83-71cb60e8f08c","type":"EOM::CompoundStory","value":"test"}`},
map[string]string{
"X-Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"X-Request-Id": "t9happe59y",
"Authorization": "",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"Content-Type": "application/json",
},
},
{ //origin system id is missing
queueProducer.MessageProducerConfig{
Addr: "address",
},
"",
queueProducer.Message{
Headers: map[string]string{
"Message-Id": "fc429b46-2500-4fe7-88bb-fd507fbaf00c",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"Message-Type": "cms-content-published",
"Content-Type": "application/json",
"X-Request-Id": "t9happe59y",
},
Body: `{"uuid":"7543220a-2389-11e5-bd83-71cb60e8f08c","type":"EOM::CompoundStory","value":"test"}`},
map[string]string{
"X-Origin-System-Id": "",
"X-Request-Id": "t9happe59y",
"Authorization": "",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"Content-Type": "application/json",
},
},
{ // origin system id is invalid (but the bridge shouldn't care)
queueProducer.MessageProducerConfig{
Addr: "address",
Authorization: "authorizationkey",
},
"",
queueProducer.Message{
Headers: map[string]string{
"Message-Id": "fc429b46-2500-4fe7-88bb-fd507fbaf00c",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"Message-Type": "cms-content-published",
"Origin-System-Id": "http://foo.ft.com/systems/bar",
"Content-Type": "application/json",
"X-Request-Id": "t9happe59y",
},
Body: `{"uuid":"7543220a-2389-11e5-bd83-71cb60e8f08c","type":"EOM::CompoundStory","value":"test"}`},
map[string]string{
"X-Origin-System-Id": "http://foo.ft.com/systems/bar",
"X-Request-Id": "t9happe59y",
"Authorization": "authorizationkey",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"Content-Type": "application/json",
},
},
{ //Message-Timestamp is missing
queueProducer.MessageProducerConfig{
Addr: "address",
},
"",
queueProducer.Message{
Headers: map[string]string{
"Message-Id": "fc429b46-2500-4fe7-88bb-fd507fbaf00c",
"Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"Message-Type": "cms-content-published",
"Content-Type": "application/json",
"X-Request-Id": "t9happe59y",
},
Body: `{"uuid":"7543220a-2389-11e5-bd83-71cb60e8f08c","type":"EOM::CompoundStory","value":"test"}`},
map[string]string{
"X-Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"X-Request-Id": "t9happe59y",
"Authorization": "",
"Message-Timestamp": "",
"Content-Type": "application/json",
},
},
{ //native-hash forward
queueProducer.MessageProducerConfig{
Addr: "address",
Authorization: "authorizationkey",
},
"",
queueProducer.Message{
Headers: map[string]string{
"Message-Id": "fc429b46-2500-4fe7-88bb-fd507fbaf00c",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"Message-Type": "cms-content-published",
"Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"Content-Type": "application/json",
"X-Request-Id": "t9happe59y",
"Native-Hash": "27f79e6d884acdd642d1758c4fd30d43074f8384d552d1ebb1959345",
},
Body: `{"uuid":"7543220a-2389-11e5-bd83-71cb60e8f08c","type":"EOM::CompoundStory","value":"test"}`},
map[string]string{
"X-Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"X-Request-Id": "t9happe59y",
"Authorization": "authorizationkey",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"X-Native-Hash": "27f79e6d884acdd642d1758c4fd30d43074f8384d552d1ebb1959345",
"Content-Type": "application/json",
},
},
{ //schema-version forward
queueProducer.MessageProducerConfig{
Addr: "address",
Authorization: "authorizationkey",
},
"",
queueProducer.Message{
Headers: map[string]string{
"Message-Id": "fc429b46-2500-4fe7-88bb-fd507fbaf00c",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"Message-Type": "cms-content-published",
"Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"Content-Type": "application/json",
"X-Request-Id": "t9happe59y",
"Native-Hash": "27f79e6d884acdd642d1758c4fd30d43074f8384d552d1ebb1959345",
"X-Schema-Version": "2",
},
Body: `{"uuid":"7543220a-2389-11e5-bd83-71cb60e8f08c","type":"EOM::CompoundStory","value":"test"}`},
map[string]string{
"X-Origin-System-Id": "http://cmdb.ft.com/systems/methode-web-pub",
"X-Request-Id": "t9happe59y",
"Authorization": "authorizationkey",
"Message-Timestamp": "2015-07-06T07:03:09.362Z",
"X-Native-Hash": "27f79e6d884acdd642d1758c4fd30d43074f8384d552d1ebb1959345",
"Content-Type": "application/json",
"X-Schema-Version": "2",
},
},
}
for _, test := range tests {
cmsNotifierTest := &plainHTTPMessageProducer{
test.config,
&dummyHttpClient{
assert: assert.New(t),
address: test.config.Addr,
headers: test.expectedHeaders,
resp: http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBuffer([]byte{})),
},
},
}
err := cmsNotifierTest.SendMessage(test.uuid, test.message)
if err != nil {
t.Errorf("\nExpected error was nil \nActual: %s", err.Error())
}
}
}
type dummyHttpClient struct {
assert *assert.Assertions
address string
headers map[string]string
resp http.Response
host string
}
func (d *dummyHttpClient) Do(req *http.Request) (resp *http.Response, err error) {
// Check url
d.assert.Contains(req.URL.String(), fmt.Sprintf("%s/notify", d.address), fmt.Sprintf("Expected URL incorrect"))
// Check that the correct headers were set
for key, value := range d.headers {
d.assert.Equal(value, req.Header.Get(key), fmt.Sprintf("%s Header value differs. Expected: %s, Actual: %s", key, value, req.Header.Get(key)))
}
// Check that host is set as expected
d.assert.Equal(d.host, req.Host, "Host header value differs.")
return &d.resp, nil
}