-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
70 lines (56 loc) · 1.35 KB
/
response_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
package rpc
import (
"errors"
"testing"
)
func TestNewResponse(t *testing.T) {
id := "123"
result := "success"
err := errors.New("some error")
response, err := NewResponse(id, result, err)
if err == nil {
t.Errorf("expected error, got nil")
}
if response != nil {
t.Errorf("expected nil response, got %+v", response)
}
err = nil
response, err = NewResponse(id, result, err)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if response == nil {
t.Fatalf("expected non-nil response, got nil")
}
if response.ID != id {
t.Errorf("expected ID %q, got %q", id, response.ID)
}
if response.Error != "" {
t.Errorf("expected empty error, got %q", response.Error)
}
if string(response.Result) != "\"success\"" {
t.Errorf("expected result %q, got %q", "\"success\"", string(response.Result))
}
}
func TestParseResult(t *testing.T) {
response := &Response{
ID: "123",
Result: []byte(`{"name":"John","age":30}`),
}
var data struct {
Name string `json:"name"`
Age int `json:"age"`
}
err := response.ParseResut(&data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expectedName := "John"
if data.Name != expectedName {
t.Errorf("expected name %q, got %q", expectedName, data.Name)
}
expectedAge := 30
if data.Age != expectedAge {
t.Errorf("expected age %d, got %d", expectedAge, data.Age)
}
}