forked from RedisLabs/xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse_test.go
More file actions
166 lines (154 loc) · 3.35 KB
/
response_test.go
File metadata and controls
166 lines (154 loc) · 3.35 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package xmlrpc
import (
"testing"
)
func TestResponseErr(t *testing.T) {
t.Parallel()
tests := []struct {
name string
xml string
wantErr bool
faultCode int
}{
{
name: "fault_response",
xml: `<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultString</name>
<value><string>You must log in before using this part of Bugzilla.</string></value>
</member>
<member>
<name>faultCode</name>
<value><int>410</int></value>
</member>
</struct>
</value>
</fault>
</methodResponse>`,
wantErr: true,
faultCode: 410,
},
{
name: "success_response",
xml: `<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value><string>success</string></value>
</param>
</params>
</methodResponse>`,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
resp := Response([]byte(tt.xml))
err := resp.Err()
if tt.wantErr {
if err == nil {
t.Fatal("expected error, got nil")
}
fault, ok := err.(FaultError)
if !ok {
t.Fatalf("expected FaultError, got %T", err)
}
if fault.Code != tt.faultCode {
t.Errorf("expected fault code %d, got %d", tt.faultCode, fault.Code)
}
} else {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
})
}
}
func TestResponseUnmarshal(t *testing.T) {
t.Parallel()
tests := []struct {
name string
xml string
target any
validate func(t *testing.T, v any)
}{
{
name: "string",
xml: `<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value><string>hello world</string></value>
</param>
</params>
</methodResponse>`,
target: new(string),
validate: func(t *testing.T, v any) {
if got := *v.(*string); got != "hello world" {
t.Errorf("expected 'hello world', got %q", got)
}
},
},
{
name: "struct_with_empty_value",
xml: `<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>user</name>
<value><string>Joe Smith</string></value>
</member>
<member>
<name>token</name>
<value/>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>`,
target: &struct {
User string `xmlrpc:"user"`
Token string `xmlrpc:"token"`
}{},
validate: func(t *testing.T, v any) {
result := v.(*struct {
User string `xmlrpc:"user"`
Token string `xmlrpc:"token"`
})
if result.User != "Joe Smith" {
t.Errorf("expected User 'Joe Smith', got %q", result.User)
}
if result.Token != "" {
t.Errorf("expected Token '', got %q", result.Token)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
resp := Response([]byte(tt.xml))
if err := resp.Unmarshal(tt.target); err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
tt.validate(t, tt.target)
})
}
}
func TestFaultErrorString(t *testing.T) {
t.Parallel()
fault := FaultError{Code: 123, String: "test error"}
expected := "Fault(123): test error"
if got := fault.Error(); got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
}