-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol_test.go
More file actions
131 lines (121 loc) · 3.02 KB
/
Copy pathprotocol_test.go
File metadata and controls
131 lines (121 loc) · 3.02 KB
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
package udsrpc
import (
"encoding/json"
"testing"
)
func TestDecodeMessage_Request(t *testing.T) {
raw := json.RawMessage(`{"id":1,"method":"Ping"}`)
msg, err := DecodeMessage(raw)
if err != nil {
t.Fatal(err)
}
if msg.Request == nil {
t.Fatal("expected Request, got nil")
}
if msg.Request.Method != "Ping" {
t.Errorf("method = %q, want Ping", msg.Request.Method)
}
if msg.Request.ID != 1 {
t.Errorf("id = %d, want 1", msg.Request.ID)
}
if msg.Response != nil || msg.Event != nil {
t.Error("expected only Request to be set")
}
}
func TestDecodeMessage_Response(t *testing.T) {
raw := json.RawMessage(`{"id":1,"result":{"pong":true}}`)
msg, err := DecodeMessage(raw)
if err != nil {
t.Fatal(err)
}
if msg.Response == nil {
t.Fatal("expected Response, got nil")
}
if msg.Response.ID != 1 {
t.Errorf("id = %d, want 1", msg.Response.ID)
}
if msg.Response.Error != nil {
t.Error("expected no error")
}
}
func TestDecodeMessage_ResponseError(t *testing.T) {
raw := json.RawMessage(`{"id":2,"error":{"code":-32601,"message":"not found"}}`)
msg, err := DecodeMessage(raw)
if err != nil {
t.Fatal(err)
}
if msg.Response == nil {
t.Fatal("expected Response")
}
if msg.Response.Error == nil {
t.Fatal("expected error in response")
}
if msg.Response.Error.Code != ErrCodeNotFound {
t.Errorf("code = %d, want %d", msg.Response.Error.Code, ErrCodeNotFound)
}
if msg.Response.Error.Message != "not found" {
t.Errorf("message = %q, want 'not found'", msg.Response.Error.Message)
}
}
func TestDecodeMessage_Event(t *testing.T) {
raw := json.RawMessage(`{"type":"NewItem","data":{"id":"abc"}}`)
msg, err := DecodeMessage(raw)
if err != nil {
t.Fatal(err)
}
if msg.Event == nil {
t.Fatal("expected Event, got nil")
}
if msg.Event.Type != "NewItem" {
t.Errorf("type = %q, want NewItem", msg.Event.Type)
}
var data struct {
ID string `json:"id"`
}
if err := json.Unmarshal(msg.Event.Data, &data); err != nil {
t.Fatal(err)
}
if data.ID != "abc" {
t.Errorf("id = %q, want abc", data.ID)
}
}
func TestDecodeMessage_Invalid(t *testing.T) {
raw := json.RawMessage(`{invalid}`)
if _, err := DecodeMessage(raw); err == nil {
t.Error("expected error for invalid JSON")
}
}
func TestError_ErrorInterface(t *testing.T) {
e := &Error{Code: ErrCodeInternal, Message: "something broke"}
if e.Error() != "something broke" {
t.Errorf("Error() = %q, want 'something broke'", e.Error())
}
}
func TestRequestRoundTrip(t *testing.T) {
type params struct {
Limit int `json:"limit"`
}
raw, _ := json.Marshal(params{Limit: 50})
req := Request{ID: 42, Method: "Fetch", Params: raw}
data, err := json.Marshal(req)
if err != nil {
t.Fatal(err)
}
msg, err := DecodeMessage(data)
if err != nil {
t.Fatal(err)
}
if msg.Request == nil {
t.Fatal("expected Request")
}
if msg.Request.ID != 42 {
t.Errorf("id = %d, want 42", msg.Request.ID)
}
var p params
if err := json.Unmarshal(msg.Request.Params, &p); err != nil {
t.Fatal(err)
}
if p.Limit != 50 {
t.Errorf("limit = %d, want 50", p.Limit)
}
}