forked from sohlich/nats-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
94 lines (77 loc) · 2.1 KB
/
client_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
package natsproxy
import (
"fmt"
"os"
"testing"
"time"
"github.com/gogo/protobuf/proto"
"github.com/nats-io/nats"
)
var nats_url = getTestNatsUrl()
func getTestNatsUrl() string {
natsURL := os.Getenv("NATS_URL")
if natsURL == "" {
natsURL = "127.0.0.1:4222"
}
return fmt.Sprintf("nats://%s", natsURL)
}
func TestGetSubscribe(t *testing.T) {
clientConn, _ := nats.Connect(nats_url)
natsClient, _ := NewNatsClient(clientConn)
defer clientConn.Close()
natsClient.GET("/test", func(c *Context) {
c.JSON(200, "OK")
})
testClient, _ := nats.Connect(nats_url)
defer testClient.Close()
r := &Request{}
data, _ := proto.Marshal(r)
if _, err := testClient.Request("GET:.test", data, 10*time.Second); err != nil {
t.Error("Did not get response")
}
}
func TestPostSubscribe(t *testing.T) {
clientConn, _ := nats.Connect(nats_url)
natsClient, _ := NewNatsClient(clientConn)
defer clientConn.Close()
natsClient.POST("/test", func(c *Context) {
c.JSON(200, "OK")
})
testClient, _ := nats.Connect(nats_url)
defer testClient.Close()
r := &Request{}
data, _ := proto.Marshal(r)
if _, err := testClient.Request("POST:.test", data, 10*time.Second); err != nil {
t.Error("Did not get response")
}
}
func TestPutSubscribe(t *testing.T) {
clientConn, _ := nats.Connect(nats_url)
natsClient, _ := NewNatsClient(clientConn)
defer clientConn.Close()
natsClient.PUT("/test", func(c *Context) {
c.JSON(200, "OK")
})
testClient, _ := nats.Connect(nats_url)
defer testClient.Close()
r := &Request{}
data, _ := proto.Marshal(r)
if _, err := testClient.Request("PUT:.test", data, 10*time.Second); err != nil {
t.Error("Did not get response")
}
}
func TestDeleteSubscribe(t *testing.T) {
clientConn, _ := nats.Connect(nats_url)
natsClient, _ := NewNatsClient(clientConn)
defer clientConn.Close()
natsClient.DELETE("/test", func(c *Context) {
c.JSON(200, "OK")
})
testClient, _ := nats.Connect(nats_url)
defer testClient.Close()
r := &Request{}
data, _ := proto.Marshal(r)
if _, err := testClient.Request("DELETE:.test", data, 10*time.Second); err != nil {
t.Error("Did not get response")
}
}