-
Notifications
You must be signed in to change notification settings - Fork 0
/
destination_http_test.go
144 lines (133 loc) · 4.39 KB
/
destination_http_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
package opinionatedevents
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestHTTPDestination(t *testing.T) {
t.Run("fails if no HTTP handlers added", func(t *testing.T) {
ctx := context.Background()
destination := NewHTTPDestination("https://api.example.com/events")
client := &testHTTPClient{}
destination.setClient(client)
msg, err := NewMessage("test.test", nil)
assert.NoError(t, err)
err = destination.Deliver(ctx, []*Message{msg})
assert.Error(t, err)
})
t.Run("successfully delivers a message if endpoint responds with 200", func(t *testing.T) {
ctx := context.Background()
destination := NewHTTPDestination("https://api.example.com/events")
client := &testHTTPClient{}
destination.setClient(client)
// configure the handler
i := 0
client.pushHandler(func(req *http.Request) (*http.Response, error) {
assert.Equal(t, "POST", req.Method)
assert.Equal(t, "/events", req.URL.Path)
i += 1
return &http.Response{StatusCode: 200}, nil
})
// publish the message
msg, err := NewMessage("test.test", nil)
assert.NoError(t, err)
err = destination.Deliver(ctx, []*Message{msg})
assert.NoError(t, err)
assert.Equal(t, 1, i)
})
t.Run("fails delivering a message if endpoint responds with non-200", func(t *testing.T) {
ctx := context.Background()
destination := NewHTTPDestination("https://api.example.com/events")
client := &testHTTPClient{}
destination.setClient(client)
// configure the handler
i := 0
client.pushHandler(func(req *http.Request) (*http.Response, error) {
assert.Equal(t, "POST", req.Method)
assert.Equal(t, "/events", req.URL.Path)
i += 1
return &http.Response{StatusCode: 404}, nil
})
// publish the message
msg, err := NewMessage("test.test", nil)
assert.NoError(t, err)
err = destination.Deliver(ctx, []*Message{msg})
assert.Error(t, err)
assert.Equal(t, 1, i)
})
t.Run("sends the message as JSON in the POST body", func(t *testing.T) {
ctx := context.Background()
destination := NewHTTPDestination("https://api.example.com/events")
client := &testHTTPClient{}
destination.setClient(client)
// create the message
msg, err := NewMessage("test.test", &testHTTPClientPayload{})
assert.NoError(t, err)
// configure the handler
i := 0
client.pushHandler(func(req *http.Request) (*http.Response, error) {
assert.Equal(t, "POST", req.Method)
assert.Equal(t, "/events", req.URL.Path)
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
i += 1
// parse the request payload
body, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
var payload []map[string]interface{}
if err := json.Unmarshal(body, &payload); err != nil {
return nil, err
}
assert.Len(t, payload, 1)
for _, i := range payload {
meta, ok := i["meta"].(map[string]interface{})
assert.True(t, ok)
// assert the data types
assert.IsType(t, "", i["name"])
assert.IsType(t, "", i["payload"])
assert.IsType(t, "", meta["published_at"])
// assert some fields
assert.Equal(t, "test.test", i["name"])
assert.Equal(t, msg.GetPublishedAt().UTC().Format(time.RFC3339Nano), meta["published_at"])
// parse the message payload
payloadAsJson, err := base64.StdEncoding.DecodeString(i["payload"].(string))
assert.NoError(t, err)
var data map[string]interface{}
assert.NoError(t, json.Unmarshal(payloadAsJson, &data))
assert.Equal(t, "world", data["hello"])
assert.Equal(t, true, data["ok"])
assert.Equal(t, 4.0, data["age"])
}
return &http.Response{StatusCode: 200}, nil
})
// publish the message
deliveryErr := destination.Deliver(ctx, []*Message{msg})
assert.NoError(t, deliveryErr)
})
}
type testHTTPClientPayload struct{}
func (p *testHTTPClientPayload) MarshalJSON() ([]byte, error) {
payload := map[string]interface{}{"hello": "world", "ok": true, "age": 4}
return json.Marshal(payload)
}
type testHTTPClient struct {
handlers []func(req *http.Request) (*http.Response, error)
}
func (c *testHTTPClient) Do(req *http.Request) (*http.Response, error) {
if len(c.handlers) == 0 {
return nil, fmt.Errorf("no handlers left")
}
handler := c.handlers[0]
c.handlers = c.handlers[1:]
return handler(req)
}
func (c *testHTTPClient) pushHandler(handler func(req *http.Request) (*http.Response, error)) {
c.handlers = append(c.handlers, handler)
}