@@ -30,15 +30,17 @@ func TestSubscribe(t *testing.T) {
30
30
testMsgData := "this is a test string"
31
31
defer srv .Shutdown ()
32
32
33
- mgr .NewStream ("ORDERS" , jsm .Subjects ("ORDERS.*" ))
33
+ _ , err := mgr .NewStream ("ORDERS" , jsm .Subjects ("ORDERS.*" ))
34
+ if err != nil {
35
+ t .Fatalf ("unable to create stream: %s" , err )
36
+ }
34
37
msg := nats.Msg {
35
38
Subject : "ORDERS.1" ,
36
39
Data : []byte (testMsgData ),
37
40
}
38
41
39
- nc .PublishMsg (& msg )
40
-
41
42
t .Run ("--dump=file" , func (t * testing.T ) {
43
+ nc .PublishMsg (& msg )
42
44
runNatsCli (t , fmt .Sprintf ("--server='%s' sub --stream ORDERS --last --count=1 --dump=/tmp/test1" , srv .ClientURL ()))
43
45
defer os .RemoveAll ("/tmp/test1/" )
44
46
@@ -58,6 +60,7 @@ func TestSubscribe(t *testing.T) {
58
60
})
59
61
60
62
t .Run ("--dump=-" , func (t * testing.T ) {
63
+ nc .PublishMsg (& msg )
61
64
output := runNatsCli (t , fmt .Sprintf ("--server='%s' sub --stream ORDERS --last --count=1 --dump=-" , srv .ClientURL ()))
62
65
// We trimspace here because some shells can pre- and append whitespaces to the output
63
66
resp := strings .TrimSpace (strings .Split (string (output ), "\n " )[1 ])
@@ -75,6 +78,7 @@ func TestSubscribe(t *testing.T) {
75
78
})
76
79
77
80
t .Run ("--translate" , func (t * testing.T ) {
81
+ nc .PublishMsg (& msg )
78
82
output := runNatsCli (t , fmt .Sprintf ("--server='%s' sub --stream ORDERS --last --count=1 --raw --translate=\" wc -c\" " , srv .ClientURL ()))
79
83
// We trimspace here because some shells can pre- and append whitespaces to the output
80
84
resp := strings .TrimSpace (strings .Split (string (output ), "\n " )[1 ])
@@ -83,11 +87,26 @@ func TestSubscribe(t *testing.T) {
83
87
}
84
88
})
85
89
90
+ t .Run ("--translate empty message" , func (t * testing.T ) {
91
+ empty := nats.Msg {
92
+ Subject : "ORDERS.1" ,
93
+ Data : []byte ("" ),
94
+ }
95
+ nc .PublishMsg (& empty )
96
+ output := runNatsCli (t , fmt .Sprintf ("--server='%s' sub --stream ORDERS --last --count=1 --translate=\" wc -c\" " , srv .ClientURL ()))
97
+ // We trimspace here because some shells can pre- and append whitespaces to the output
98
+ resp := strings .TrimSpace (strings .Split (string (output ), "\n " )[2 ])
99
+ if resp != "0" {
100
+ t .Errorf ("unexpected response. Got \" %s\" expected \" %s\" " , resp , "21" )
101
+ }
102
+ })
103
+
86
104
t .Run ("--dump and --translate" , func (t * testing.T ) {
105
+ nc .PublishMsg (& msg )
87
106
runNatsCli (t , fmt .Sprintf ("--server='%s' sub --stream ORDERS --last --count=1 --dump=/tmp/test2 --translate=\" wc -c\" " , srv .ClientURL ()))
88
107
defer os .RemoveAll ("/tmp/test2" )
89
108
90
- resp , err := os .ReadFile ("/tmp/test2/1 .json" )
109
+ resp , err := os .ReadFile ("/tmp/test2/5 .json" )
91
110
if err != nil {
92
111
t .Fatal (err )
93
112
}
@@ -104,6 +123,7 @@ func TestSubscribe(t *testing.T) {
104
123
})
105
124
106
125
t .Run ("--raw" , func (t * testing.T ) {
126
+ nc .PublishMsg (& msg )
107
127
output := runNatsCli (t , fmt .Sprintf ("--server='%s' sub --stream ORDERS --last --count=1 --raw" , srv .ClientURL ()))
108
128
resp := strings .TrimSpace (strings .Split (string (output ), "\n " )[1 ])
109
129
if resp != testMsgData {
@@ -112,6 +132,7 @@ func TestSubscribe(t *testing.T) {
112
132
})
113
133
114
134
t .Run ("--pretty" , func (t * testing.T ) {
135
+ nc .PublishMsg (& msg )
115
136
output := runNatsCli (t , fmt .Sprintf ("--server='%s' sub --stream ORDERS --last --count=1" , srv .ClientURL ()))
116
137
pattern := `\[#\d\] Received JetStream message: stream: ORDERS seq (\d+) / subject: ORDERS.1 / time: \d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ`
117
138
matcher := regexp .MustCompile (pattern )
0 commit comments