Skip to content

Commit 4765870

Browse files
committed
Added test cases for tcp server and response.
1 parent 54765ba commit 4765870

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

response/json_rpc_response_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package response
2+
3+
import (
4+
"encoding/json"
5+
c "github.com/smartystreets/goconvey/convey"
6+
"testing"
7+
)
8+
9+
func Test_Json_Rpc_Response(t *testing.T) {
10+
c.Convey("Json encode and decode must support T.", t, func() {
11+
data := &JsonRPCResponse[string, string]{
12+
Id: "123",
13+
Path: "/json_rpc/index",
14+
Data: "Hello World",
15+
Context: "",
16+
}
17+
18+
ret, _ := json.Marshal(data)
19+
jsonData := "{\"id\":\"123\",\"path\":\"/json_rpc/index\",\"data\":\"Hello World\",\"context\":\"\"}"
20+
c.So(string(ret), c.ShouldEqual, jsonData)
21+
22+
data2 := &JsonRPCResponse[string, string]{}
23+
bt := []byte(jsonData)
24+
json.Unmarshal(bt, data2)
25+
26+
c.So(data2.Id, c.ShouldEqual, data.Id)
27+
c.So(data2.Path, c.ShouldEqual, data.Path)
28+
c.So(data2.Data, c.ShouldEqual, data.Data)
29+
c.So(data2.Context, c.ShouldEqual, data.Context)
30+
})
31+
}

server/tcp_server_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package server
2+
3+
import (
4+
"github.com/hyperf/roc"
5+
c "github.com/smartystreets/goconvey/convey"
6+
"net"
7+
"testing"
8+
)
9+
10+
func Test_New_Tcp_Server(t *testing.T) {
11+
c.Convey("NewTcpServer must return TcpServer.", t, func() {
12+
serv := NewTcpServer("127.0.0.1:9501", func(conn net.Conn, packet *roc.Packet, server *TcpServer) {
13+
14+
})
15+
16+
c.So(serv.Address, c.ShouldEqual, "127.0.0.1:9501")
17+
})
18+
}

0 commit comments

Comments
 (0)