-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
141 lines (123 loc) · 3.07 KB
/
server_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"testing"
"github.com/gorilla/websocket"
)
type JsonRequest struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []interface{} `json:"params"`
Id int `json:"id"`
Desc string
}
func TestJsonRpcRequest(t *testing.T) {
url := HTTPRPCLocalhost
JsonRequestArr := make([]JsonRequest, 0, 0)
// Echo
// Test_Echo
JsonRequestArr = append(JsonRequestArr, JsonRequest{
Jsonrpc: "2.0",
Method: "test_Echo",
Params: []interface{}{
"f",
3,
map[string]string{"S": "v"},
},
Id: 1,
Desc: "Test_echo",
})
fmt.Println("JSON START -----------------------")
jsonBytes, _ := json.Marshal(JsonRequestArr)
fmt.Println(string(jsonBytes))
fmt.Println("JSON END -----------------------")
//JsonRequestArr = append(JsonRequestArr, JsonRequest{
// Jsonrpc: "2.0",
// Method: "test_peerInfo",
// Id: 1,
// Desc: "PeerInfo",
//})
//JsonRequestArr = append(JsonRequestArr, JsonRequest{
// Jsonrpc: "2.0",
// Method: "test_sleep",
// Params: []inter{}{3},
// Id: 1,
// Desc: "sleep",
//})
//he will block the goroutine
//JsonRequestArr = append(JsonRequestArr, JsonRequest{
// Jsonrpc: "2.0",
// Method: "test_block",
// Id: 1,
// Desc: "Block",
//})
//JsonRequestArr = append(JsonRequestArr, JsonRequest{
// Jsonrpc: "2.0",
// Method: "test_repeat",
// Params: []inter{}{1, "x"},
// Id: 1,
// Desc: "Repeat",
//})
for _, request := range JsonRequestArr {
jsonData, err := json.Marshal(request)
if err != nil {
t.Fatalf("Failed to marshal JSON: %v", err)
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
t.Fatalf("Failed to send POST request: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected status code 200, got %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
t.Fatalf("request.Desc + \":\"+Failed to read response body: %v", err)
} else {
log.Println(request.Desc + ":" + string(body))
}
}
}
func TestSomeSubscription(t *testing.T) {
u := url.URL{Scheme: "ws", Host: RPCLoclahost, Path: "/ws"}
conn, resp, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
t.Fatalf("dial: %v", err)
}
defer conn.Close()
// Check response headers from the initial handshake
fmt.Println("Response headers:")
for key, values := range resp.Header {
for _, value := range values {
fmt.Printf("%s: %s\n", key, value)
}
}
request := map[string]interface{}{
"jsonrpc": "2.0",
"id": 1,
"method": "nftest_subscribe",
"params": []interface{}{"someSubscription", 1, 2},
}
err = conn.WriteJSON(request)
if err != nil {
t.Fatalf("write: %v", err)
}
var response map[string]interface{}
err = conn.ReadJSON(&response)
if err != nil {
t.Fatalf("read: %v", err)
}
subscriptionID, ok := response["result"].(string)
if !ok {
t.Fatalf("unexpected result: %v", response)
}
log.Println(subscriptionID)
t.Skip()
}