-
Notifications
You must be signed in to change notification settings - Fork 4
/
integration_test.go
75 lines (54 loc) · 1.75 KB
/
integration_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
package porthos
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func getAmqpUrl() string {
return os.Getenv("AMQP_URL")
}
type testPorthosResponse struct {
Original int `json:"original_value"`
Sum int `json:"value_plus_one"`
}
type testPorthosRequest struct {
Value int `json:"value"`
}
func TestClientServerSync(t *testing.T) {
serverName := "TestClientServer"
methodName := "method"
b, err := NewBroker(getAmqpUrl())
if assert.Nil(t, err) {
defer b.Close()
server, err := NewServer(b, serverName, Options{AutoAck: false})
if assert.Nil(t, err, "Failed to create server.") {
defer server.Close()
requestPayload := testPorthosRequest{Value: 1}
server.Register(methodName, func(req Request, res Response) {
payload := &testPorthosRequest{}
assert.Nil(t, req.Bind(payload))
res.JSON(StatusOK, testPorthosResponse{payload.Value, payload.Value + 1})
})
server.Register("dummyMethod", func(req Request, res Response) {
payload := &testPorthosRequest{}
assert.Nil(t, req.Bind(payload))
res.JSON(StatusOK, testPorthosResponse{payload.Value, payload.Value + 2})
})
go server.ListenAndServe()
client, err := NewClient(b, serverName, 1*time.Second)
if assert.Nil(t, err, "Failed to create client.") {
defer client.Close()
ret, err := client.Call(methodName).WithStruct(requestPayload).Sync()
if assert.Nil(t, err, "Failed to call remote method.") {
responsePayload := &testPorthosResponse{}
assert.Equal(t, StatusOK, ret.StatusCode)
if assert.Nil(t, ret.UnmarshalJSONTo(responsePayload)) {
assert.Equal(t, requestPayload.Value, responsePayload.Original)
assert.Equal(t, requestPayload.Value+1, responsePayload.Sum)
}
}
}
}
}
}