-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherrors_test.go
More file actions
297 lines (246 loc) · 7.93 KB
/
errors_test.go
File metadata and controls
297 lines (246 loc) · 7.93 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package epo_ops
import (
"errors"
"net/http"
"testing"
)
func TestParseErrorXML_DetailedFormat(t *testing.T) {
xml := `<?xml version="1.0" encoding="UTF-8"?>
<error>
<code>CLIENT.InvalidReference</code>
<message>Invalid patent reference format</message>
<moreInfo>https://ops.epo.org/3.2/rest-services/help</moreInfo>
</error>`
opsErr, err := parseErrorXML([]byte(xml), http.StatusBadRequest)
if err != nil {
t.Fatalf("parseErrorXML failed: %v", err)
}
if opsErr.HTTPStatus != http.StatusBadRequest {
t.Errorf("Expected HTTPStatus %d, got %d", http.StatusBadRequest, opsErr.HTTPStatus)
}
if opsErr.Code != "CLIENT.InvalidReference" {
t.Errorf("Expected code 'CLIENT.InvalidReference', got '%s'", opsErr.Code)
}
if opsErr.Message != "Invalid patent reference format" {
t.Errorf("Expected message 'Invalid patent reference format', got '%s'", opsErr.Message)
}
if opsErr.MoreInfo != "https://ops.epo.org/3.2/rest-services/help" {
t.Errorf("Expected moreInfo URL, got '%s'", opsErr.MoreInfo)
}
// Test Error() method includes moreInfo
errStr := opsErr.Error()
if errStr != "[400] CLIENT.InvalidReference: Invalid patent reference format (see https://ops.epo.org/3.2/rest-services/help)" {
t.Errorf("Unexpected Error() output: %s", errStr)
}
}
func TestParseErrorXML_FaultFormat(t *testing.T) {
xml := `<?xml version="1.0" encoding="UTF-8"?>
<fault xmlns="http://ops.epo.org">
<code>404</code>
<message>Document not found</message>
<description>No published document found for the specified input</description>
</fault>`
opsErr, err := parseErrorXML([]byte(xml), http.StatusNotFound)
if err != nil {
t.Fatalf("parseErrorXML failed: %v", err)
}
if opsErr.HTTPStatus != http.StatusNotFound {
t.Errorf("Expected HTTPStatus %d, got %d", http.StatusNotFound, opsErr.HTTPStatus)
}
if opsErr.Code != "HTTP.404" {
t.Errorf("Expected code 'HTTP.404', got '%s'", opsErr.Code)
}
// Should use description as message
if opsErr.Message != "No published document found for the specified input" {
t.Errorf("Expected description as message, got '%s'", opsErr.Message)
}
// Test Error() method without moreInfo
errStr := opsErr.Error()
if errStr != "[404] HTTP.404: No published document found for the specified input" {
t.Errorf("Unexpected Error() output: %s", errStr)
}
}
func TestParseErrorXML_FaultFormat_NoDescription(t *testing.T) {
xml := `<?xml version="1.0" encoding="UTF-8"?>
<fault xmlns="http://ops.epo.org">
<code>429</code>
<message>Fair use limit exceeded</message>
</fault>`
opsErr, err := parseErrorXML([]byte(xml), http.StatusTooManyRequests)
if err != nil {
t.Fatalf("parseErrorXML failed: %v", err)
}
if opsErr.Code != "HTTP.429" {
t.Errorf("Expected code 'HTTP.429', got '%s'", opsErr.Code)
}
// Should use message when no description
if opsErr.Message != "Fair use limit exceeded" {
t.Errorf("Expected message 'Fair use limit exceeded', got '%s'", opsErr.Message)
}
}
func TestParseErrorXML_InvalidXML(t *testing.T) {
xml := `not valid xml`
opsErr, err := parseErrorXML([]byte(xml), http.StatusBadRequest)
if err == nil {
t.Error("Expected error for invalid XML, got nil")
}
if opsErr != nil {
t.Errorf("Expected nil OPSError for invalid XML, got %+v", opsErr)
}
}
func TestParseErrorXML_EmptyXML(t *testing.T) {
xml := `<?xml version="1.0" encoding="UTF-8"?><empty/>`
opsErr, err := parseErrorXML([]byte(xml), http.StatusBadRequest)
if err == nil {
t.Error("Expected error for empty XML, got nil")
}
if opsErr != nil {
t.Errorf("Expected nil OPSError for empty XML, got %+v", opsErr)
}
}
func TestHandleErrorResponse_WithValidErrorXML(t *testing.T) {
client, _ := NewClient(&Config{
ConsumerKey: "test",
ConsumerSecret: "test",
})
xml := `<?xml version="1.0" encoding="UTF-8"?>
<error>
<code>CLIENT.InvalidReference</code>
<message>Invalid patent number</message>
</error>`
err := client.handleErrorResponse(http.StatusBadRequest, []byte(xml))
if err == nil {
t.Fatal("Expected error, got nil")
}
// Should be mapped to NotFoundError based on code
var notFoundErr *NotFoundError
if !errors.As(err, ¬FoundErr) {
t.Errorf("Expected NotFoundError, got %T: %v", err, err)
}
if notFoundErr.Message != "Invalid patent number" {
t.Errorf("Expected message 'Invalid patent number', got '%s'", notFoundErr.Message)
}
}
func TestHandleErrorResponse_WithFaultXML(t *testing.T) {
client, _ := NewClient(&Config{
ConsumerKey: "test",
ConsumerSecret: "test",
})
xml := `<?xml version="1.0" encoding="UTF-8"?>
<fault xmlns="http://ops.epo.org">
<code>404</code>
<message>Document not found</message>
<description>No published document found for the specified input</description>
</fault>`
err := client.handleErrorResponse(http.StatusNotFound, []byte(xml))
if err == nil {
t.Fatal("Expected error, got nil")
}
// Should be mapped to NotFoundError based on HTTP.404 code
var notFoundErr *NotFoundError
if !errors.As(err, ¬FoundErr) {
t.Errorf("Expected NotFoundError, got %T: %v", err, err)
}
if notFoundErr.Message != "No published document found for the specified input" {
t.Errorf("Expected description message, got '%s'", notFoundErr.Message)
}
}
func TestHandleErrorResponse_FallbackToPlainText(t *testing.T) {
client, _ := NewClient(&Config{
ConsumerKey: "test",
ConsumerSecret: "test",
})
plainText := "Document not found"
err := client.handleErrorResponse(http.StatusNotFound, []byte(plainText))
if err == nil {
t.Fatal("Expected error, got nil")
}
// Should fall back to NotFoundError with plain text message
var notFoundErr *NotFoundError
if !errors.As(err, ¬FoundErr) {
t.Errorf("Expected NotFoundError, got %T: %v", err, err)
}
if notFoundErr.Message != plainText {
t.Errorf("Expected message '%s', got '%s'", plainText, notFoundErr.Message)
}
}
func TestHandleErrorResponse_AuthErrorMapping(t *testing.T) {
client, _ := NewClient(&Config{
ConsumerKey: "test",
ConsumerSecret: "test",
})
xml := `<?xml version="1.0" encoding="UTF-8"?>
<error>
<code>CLIENT.InvalidAccessToken</code>
<message>The access token is invalid</message>
</error>`
err := client.handleErrorResponse(http.StatusUnauthorized, []byte(xml))
if err == nil {
t.Fatal("Expected error, got nil")
}
var authErr *AuthError
if !errors.As(err, &authErr) {
t.Errorf("Expected AuthError, got %T: %v", err, err)
}
if authErr.Message != "The access token is invalid" {
t.Errorf("Expected message 'The access token is invalid', got '%s'", authErr.Message)
}
}
func TestHandleErrorResponse_QuotaErrorMapping(t *testing.T) {
client, _ := NewClient(&Config{
ConsumerKey: "test",
ConsumerSecret: "test",
})
xml := `<?xml version="1.0" encoding="UTF-8"?>
<error>
<code>SERVER.RateLimitExceeded</code>
<message>Rate limit exceeded</message>
</error>`
err := client.handleErrorResponse(http.StatusTooManyRequests, []byte(xml))
if err == nil {
t.Fatal("Expected error, got nil")
}
var quotaErr *QuotaExceededError
if !errors.As(err, "aErr) {
t.Errorf("Expected QuotaExceededError, got %T: %v", err, err)
}
if quotaErr.Message != "Rate limit exceeded" {
t.Errorf("Expected message 'Rate limit exceeded', got '%s'", quotaErr.Message)
}
}
func TestOPSError_ErrorMethod(t *testing.T) {
tests := []struct {
name string
opsErr *OPSError
expected string
}{
{
name: "With moreInfo",
opsErr: &OPSError{
HTTPStatus: 400,
Code: "CLIENT.InvalidReference",
Message: "Invalid input",
MoreInfo: "https://example.com/help",
},
expected: "[400] CLIENT.InvalidReference: Invalid input (see https://example.com/help)",
},
{
name: "Without moreInfo",
opsErr: &OPSError{
HTTPStatus: 404,
Code: "HTTP.404",
Message: "Not found",
MoreInfo: "",
},
expected: "[404] HTTP.404: Not found",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.opsErr.Error()
if got != tt.expected {
t.Errorf("Error() = %s, want %s", got, tt.expected)
}
})
}
}