-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_test.go
More file actions
640 lines (565 loc) · 20 KB
/
error_test.go
File metadata and controls
640 lines (565 loc) · 20 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package ginx
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
e2 "github.com/shrewx/ginx/internal/errors"
"github.com/shrewx/ginx/pkg/statuserror"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDefaultErrorResponse(t *testing.T) {
err := errors.New("test error")
status := http.StatusBadRequest
body := []byte(`{"error":"test"}`)
headers := http.Header{
"X-Custom": []string{"value"},
}
contentType := "application/json"
errorCode := int64(400000001)
message := "test message"
resp := &defaultErrorResponse{
err: err,
status: status,
body: body,
headers: headers,
contentType: contentType,
errorCode: errorCode,
message: message,
}
assert.Equal(t, err, resp.Error())
assert.Equal(t, status, resp.Status())
assert.Equal(t, body, resp.Body())
assert.Equal(t, headers, resp.Headers())
assert.Equal(t, contentType, resp.ContentType())
assert.Equal(t, errorCode, resp.ErrorCode())
assert.Equal(t, message, resp.Message())
}
func TestDefaultErrorHandlerImpl_Handle_ClientResponseError(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)
ctx.Request = httptest.NewRequest("GET", "/test", nil)
handler := &defaultErrorHandlerImpl{}
// 创建 ClientResponseError
headers := http.Header{
"X-Custom-Header": []string{"custom-value"},
}
body := []byte(`{"error":"downstream error"}`)
clientErr := NewRemoteHTTPError(
http.StatusBadGateway,
headers,
body,
"application/json",
)
handled, resp := handler.Handle(ctx, clientErr)
assert.True(t, handled)
require.NotNil(t, resp)
assert.Equal(t, http.StatusBadGateway, resp.Status())
assert.Equal(t, body, resp.Body())
assert.Equal(t, headers, resp.Headers())
assert.Equal(t, "application/json", resp.ContentType())
}
func TestDefaultErrorHandlerImpl_Handle_CommonError(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)
ctx.Request = httptest.NewRequest("GET", "/test", nil)
handler := &defaultErrorHandlerImpl{}
// 创建 CommonError (StatusErr)
commonErr := statuserror.NewStatusErr("TEST_ERROR", 400000001)
commonErr.Message = "Test error message"
handled, resp := handler.Handle(ctx, commonErr)
assert.True(t, handled)
require.NotNil(t, resp)
assert.Equal(t, http.StatusBadRequest, resp.Status())
assert.Equal(t, int64(400000001), resp.ErrorCode())
assert.Equal(t, "application/json", resp.ContentType())
// 验证响应体是有效的 JSON
var bodyMap map[string]interface{}
err := json.Unmarshal(resp.Body(), &bodyMap)
assert.NoError(t, err)
}
func TestDefaultErrorHandlerImpl_Handle_CommonError_WithLowStatusCode(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)
ctx.Request = httptest.NewRequest("GET", "/test", nil)
handler := &defaultErrorHandlerImpl{}
// 创建一个状态码小于 400 的错误码(例如 200000001)
// 这种情况下应该使用 http.StatusUnprocessableEntity
commonErr := statuserror.NewStatusErr("TEST_ERROR", 200000001)
commonErr.Message = "Test error message"
handled, resp := handler.Handle(ctx, commonErr)
assert.True(t, handled)
require.NotNil(t, resp)
// 状态码应该被修正为 422
assert.Equal(t, http.StatusUnprocessableEntity, resp.Status())
assert.Equal(t, int64(200000001), resp.ErrorCode())
}
func TestDefaultErrorHandlerImpl_Handle_UnknownError(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)
ctx.Request = httptest.NewRequest("GET", "/test", nil)
handler := &defaultErrorHandlerImpl{}
// 创建一个未知类型的错误
unknownErr := errors.New("unknown error")
handled, resp := handler.Handle(ctx, unknownErr)
assert.True(t, handled)
require.NotNil(t, resp)
assert.Equal(t, http.StatusInternalServerError, resp.Status())
assert.Equal(t, int64(500000000), resp.ErrorCode())
assert.Equal(t, "application/json", resp.ContentType())
// 验证响应体是有效的 JSON
var bodyMap map[string]interface{}
err := json.Unmarshal(resp.Body(), &bodyMap)
assert.NoError(t, err)
}
func TestRegisterErrorHandler(t *testing.T) {
// 保存原始状态
originalHandlers := make([]ErrorHandler, len(registeredErrorHandlers))
copy(originalHandlers, registeredErrorHandlers)
// 清理注册的处理器
registeredErrorHandlers = []ErrorHandler{}
// 测试注册 nil 处理器(应该被忽略)
RegisterErrorHandler(nil)
assert.Len(t, registeredErrorHandlers, 0)
// 测试注册有效处理器
customHandler := &testErrorHandler{shouldHandle: true}
RegisterErrorHandler(customHandler)
assert.Len(t, registeredErrorHandlers, 1)
assert.Equal(t, customHandler, registeredErrorHandlers[0])
// 测试注册多个处理器
customHandler2 := &testErrorHandler{shouldHandle: false}
RegisterErrorHandler(customHandler2)
assert.Len(t, registeredErrorHandlers, 2)
// 恢复原始状态
registeredErrorHandlers = originalHandlers
}
// testErrorHandler 用于测试的自定义错误处理器
type testErrorHandler struct {
shouldHandle bool
response ErrorResponse
}
func (h *testErrorHandler) Handle(ctx *gin.Context, err error) (bool, ErrorResponse) {
if h.shouldHandle {
return true, h.response
}
return false, nil
}
func TestDefaultFormatterImpl_Match(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)
ctx.Request = httptest.NewRequest("GET", "/test", nil)
formatter := &defaultFormatterImpl{}
// Match 应该总是返回 true
assert.True(t, formatter.Match(ctx, errors.New("test")))
assert.True(t, formatter.Match(ctx, nil))
}
func TestDefaultFormatterImpl_Format(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)
ctx.Request = httptest.NewRequest("GET", "/test", nil)
formatter := &defaultFormatterImpl{}
err := errors.New("test")
status := http.StatusBadRequest
body := []byte(`{"error":"test"}`)
headers := http.Header{"X-Test": []string{"value"}}
contentType := "application/json"
response := &defaultErrorResponse{
err: err,
status: status,
body: body,
headers: headers,
contentType: contentType,
}
statusCode, ct, b, h := formatter.Format(ctx, response)
assert.Equal(t, status, statusCode)
assert.Equal(t, contentType, ct)
assert.Equal(t, body, b)
assert.Equal(t, headers, h)
}
//func TestExecuteErrorHandlers_WithRegisteredHandler(t *testing.T) {
// gin.SetMode(gin.TestMode)
// w := httptest.NewRecorder()
// ctx, _ := gin.CreateTestContext(w)
// ctx.Request = httptest.NewRequest("GET", "/test", nil)
// ctx.Set(OperationName, "test-operation")
//
// // 保存原始状态
// originalHandlers := make([]ErrorHandler, len(registeredErrorHandlers))
// copy(originalHandlers, registeredErrorHandlers)
// originalFormatters := make([]ResponseFormatter, len(registeredResponseFormatters))
// copy(originalFormatters, registeredResponseFormatters)
//
// // 清理
// registeredErrorHandlers = []ErrorHandler{}
// registeredResponseFormatters = []ResponseFormatter{}
//
// // 注册自定义错误处理器
// customResponse := &defaultErrorResponse{
// err: errors.New("custom"),
// status: http.StatusTeapot,
// body: []byte(`{"custom":"error"}`),
// headers: http.Header{"X-Custom": []string{"value"}},
// contentType: "application/json",
// }
//
// customHandler := &testErrorHandler{
// shouldHandle: true,
// response: customResponse,
// }
// RegisterErrorHandler(customHandler)
//
// // 执行错误处理
// testErr := errors.New("test error")
// executeErrorHandlers(testErr, ctx)
//
// // 验证响应
// assert.Equal(t, http.StatusTeapot, w.Code)
// assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
// assert.Contains(t, w.Body.String(), "custom")
//
// // 恢复原始状态
// registeredErrorHandlers = originalHandlers
// registeredResponseFormatters = originalFormatters
//}
//
//func TestExecuteErrorHandlers_WithDefaultHandler(t *testing.T) {
// gin.SetMode(gin.TestMode)
// w := httptest.NewRecorder()
// ctx, _ := gin.CreateTestContext(w)
// ctx.Request = httptest.NewRequest("GET", "/test", nil)
// ctx.Set(OperationName, "test-operation")
//
// // 保存原始状态
// originalHandlers := make([]ErrorHandler, len(registeredErrorHandlers))
// copy(originalHandlers, registeredErrorHandlers)
// originalFormatters := make([]ResponseFormatter, len(registeredResponseFormatters))
// copy(originalFormatters, registeredResponseFormatters)
//
// // 清理
// registeredErrorHandlers = []ErrorHandler{}
// registeredResponseFormatters = []ResponseFormatter{}
//
// // 使用 CommonError,应该被默认处理器处理
// commonErr := statuserror.NewStatusErr("TEST_ERROR", 400000001)
// commonErr.Message = "Test error"
//
// executeErrorHandlers(commonErr, ctx)
//
// // 验证响应
// assert.Equal(t, http.StatusBadRequest, w.Code)
// assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
//
// // 恢复原始状态
// registeredErrorHandlers = originalHandlers
// registeredResponseFormatters = originalFormatters
//}
//
//func TestExecuteErrorHandlers_WithRegisteredFormatter(t *testing.T) {
// gin.SetMode(gin.TestMode)
// w := httptest.NewRecorder()
// ctx, _ := gin.CreateTestContext(w)
// ctx.Request = httptest.NewRequest("GET", "/test", nil)
// ctx.Set(OperationName, "test-operation")
//
// // 保存原始状态
// originalHandlers := make([]ErrorHandler, len(registeredErrorHandlers))
// copy(originalHandlers, registeredErrorHandlers)
// originalFormatters := make([]ResponseFormatter, len(registeredResponseFormatters))
// copy(originalFormatters, registeredResponseFormatters)
//
// // 清理
// registeredErrorHandlers = []ErrorHandler{}
// registeredResponseFormatters = []ResponseFormatter{}
//
// // 注册自定义格式化器
// customFormatter := &testResponseFormatter{
// shouldMatch: true,
// statusCode: http.StatusAccepted,
// contentType: "text/plain",
// body: []byte("custom formatted"),
// headers: http.Header{"X-Formatted": []string{"true"}},
// }
// registeredResponseFormatters = append(registeredResponseFormatters, customFormatter)
//
// testErr := errors.New("test error")
// executeErrorHandlers(testErr, ctx)
//
// // 验证使用了自定义格式化器
// assert.Equal(t, http.StatusAccepted, w.Code)
// assert.Equal(t, "text/plain", w.Header().Get("Content-Type"))
// assert.Equal(t, "custom formatted", w.Body.String())
// assert.Equal(t, "true", w.Header().Get("X-Formatted"))
//
// // 恢复原始状态
// registeredErrorHandlers = originalHandlers
// registeredResponseFormatters = originalFormatters
//}
//
//func TestExecuteErrorHandlers_HandlerReturnsFalse(t *testing.T) {
// gin.SetMode(gin.TestMode)
// w := httptest.NewRecorder()
// ctx, _ := gin.CreateTestContext(w)
// ctx.Request = httptest.NewRequest("GET", "/test", nil)
// ctx.Set(OperationName, "test-operation")
//
// // 保存原始状态
// originalHandlers := make([]ErrorHandler, len(registeredErrorHandlers))
// copy(originalHandlers, registeredErrorHandlers)
// originalFormatters := make([]ResponseFormatter, len(registeredResponseFormatters))
// copy(originalFormatters, registeredResponseFormatters)
//
// // 清理
// registeredErrorHandlers = []ErrorHandler{}
// registeredResponseFormatters = []ResponseFormatter{}
//
// // 注册一个返回 false 的处理器(不处理)
// nonHandlingHandler := &testErrorHandler{
// shouldHandle: false,
// }
// RegisterErrorHandler(nonHandlingHandler)
//
// // 执行错误处理,应该使用默认处理器
// testErr := errors.New("test error")
// executeErrorHandlers(testErr, ctx)
//
// // 验证使用了默认处理器(返回 500)
// assert.Equal(t, http.StatusInternalServerError, w.Code)
//
// // 恢复原始状态
// registeredErrorHandlers = originalHandlers
// registeredResponseFormatters = originalFormatters
//}
//
//func TestExecuteErrorHandlers_HandlerReturnsNilResponse(t *testing.T) {
// gin.SetMode(gin.TestMode)
// w := httptest.NewRecorder()
// ctx, _ := gin.CreateTestContext(w)
// ctx.Request = httptest.NewRequest("GET", "/test", nil)
// ctx.Set(OperationName, "test-operation")
//
// // 保存原始状态
// originalHandlers := make([]ErrorHandler, len(registeredErrorHandlers))
// copy(originalHandlers, registeredErrorHandlers)
// originalFormatters := make([]ResponseFormatter, len(registeredResponseFormatters))
// copy(originalFormatters, registeredResponseFormatters)
//
// // 清理
// registeredErrorHandlers = []ErrorHandler{}
// registeredResponseFormatters = []ResponseFormatter{}
//
// // 注册一个返回 true 但 response 为 nil 的处理器
// // 这种情况应该继续下一个处理器
// nilResponseHandler := &testErrorHandler{
// shouldHandle: true,
// response: nil, // nil response
// }
// RegisterErrorHandler(nilResponseHandler)
//
// // 执行错误处理,应该继续使用默认处理器
// testErr := errors.New("test error")
// executeErrorHandlers(testErr, ctx)
//
// // 验证使用了默认处理器(返回 500)
// assert.Equal(t, http.StatusInternalServerError, w.Code)
//
// // 恢复原始状态
// registeredErrorHandlers = originalHandlers
// registeredResponseFormatters = originalFormatters
//}
// testResponseFormatter 用于测试的自定义响应格式化器
type testResponseFormatter struct {
shouldMatch bool
statusCode int
contentType string
body []byte
headers http.Header
}
func (f *testResponseFormatter) Match(ctx *gin.Context, err error) bool {
return f.shouldMatch
}
func (f *testResponseFormatter) Format(ctx *gin.Context, response ErrorResponse) (int, string, []byte, http.Header) {
return f.statusCode, f.contentType, f.body, f.headers
}
func TestAbortWithResponse(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)
ctx.Request = httptest.NewRequest("GET", "/test", nil)
statusCode := http.StatusBadRequest
contentType := "application/json"
body := []byte(`{"error":"test"}`)
headers := http.Header{
"X-Custom-Header": []string{"custom-value"},
"X-Multiple": []string{"value1", "value2"},
}
abortWithResponse(ctx, statusCode, contentType, body, headers)
// 验证响应
assert.True(t, ctx.IsAborted())
assert.Equal(t, statusCode, w.Code)
assert.Equal(t, contentType, w.Header().Get("Content-Type"))
assert.Equal(t, body, w.Body.Bytes())
assert.Equal(t, "custom-value", w.Header().Get("X-Custom-Header"))
assert.Equal(t, "value1", w.Header().Get("X-Multiple"))
}
func TestAbortWithResponse_NilHeaders(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)
ctx.Request = httptest.NewRequest("GET", "/test", nil)
statusCode := http.StatusOK
contentType := "text/plain"
body := []byte("test body")
abortWithResponse(ctx, statusCode, contentType, body, nil)
// 验证响应
assert.True(t, ctx.IsAborted())
assert.Equal(t, statusCode, w.Code)
assert.Equal(t, contentType, w.Header().Get("Content-Type"))
assert.Equal(t, body, w.Body.Bytes())
}
func TestAbortWithResponse_EmptyHeaders(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)
ctx.Request = httptest.NewRequest("GET", "/test", nil)
statusCode := http.StatusOK
contentType := "text/plain"
body := []byte("test body")
headers := http.Header{}
abortWithResponse(ctx, statusCode, contentType, body, headers)
// 验证响应
assert.True(t, ctx.IsAborted())
assert.Equal(t, statusCode, w.Code)
assert.Equal(t, contentType, w.Header().Get("Content-Type"))
assert.Equal(t, body, w.Body.Bytes())
}
func TestWithStack(t *testing.T) {
originalErr := errors.New("original error")
wrappedErr := WithStack(originalErr)
assert.NotNil(t, wrappedErr)
// 验证错误被包装了
assert.Error(t, wrappedErr)
// 验证原始错误信息保留
assert.Contains(t, wrappedErr.Error(), "original error")
}
func TestDefaultErrorHandlerImpl_Handle_CommonError_WithI18n(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)
ctx.Request = httptest.NewRequest("GET", "/test", nil)
ctx.Request.Header.Set(CurrentLangHeader(), "en")
handler := &defaultErrorHandlerImpl{}
// 使用内部错误(有 i18n 支持)
internalErr := e2.BadRequest
handled, resp := handler.Handle(ctx, internalErr)
assert.True(t, handled)
require.NotNil(t, resp)
assert.Equal(t, http.StatusBadRequest, resp.Status())
assert.Equal(t, "application/json", resp.ContentType())
// 验证响应体是有效的 JSON
var bodyMap map[string]interface{}
err := json.Unmarshal(resp.Body(), &bodyMap)
assert.NoError(t, err)
}
//func TestExecuteErrorHandlers_WithClientResponseError(t *testing.T) {
// gin.SetMode(gin.TestMode)
// w := httptest.NewRecorder()
// ctx, _ := gin.CreateTestContext(w)
// ctx.Request = httptest.NewRequest("GET", "/test", nil)
// ctx.Set(OperationName, "test-operation")
//
// // 保存原始状态
// originalHandlers := make([]ErrorHandler, len(registeredErrorHandlers))
// copy(originalHandlers, registeredErrorHandlers)
// originalFormatters := make([]ResponseFormatter, len(registeredResponseFormatters))
// copy(originalFormatters, registeredResponseFormatters)
//
// // 清理
// registeredErrorHandlers = []ErrorHandler{}
// registeredResponseFormatters = []ResponseFormatter{}
//
// // 使用 ClientResponseError
// clientErr := NewRemoteHTTPError(
// http.StatusGatewayTimeout,
// http.Header{"X-Downstream": []string{"error"}},
// []byte(`{"downstream":"error"}`),
// "application/json",
// )
//
// executeErrorHandlers(clientErr, ctx)
//
// // 验证响应
// assert.Equal(t, http.StatusGatewayTimeout, w.Code)
// assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
// assert.Equal(t, "error", w.Header().Get("X-Downstream"))
// assert.Contains(t, w.Body.String(), "downstream")
//
// // 恢复原始状态
// registeredErrorHandlers = originalHandlers
// registeredResponseFormatters = originalFormatters
//}
//
//func TestExecuteErrorHandlers_FormatterDoesNotMatch(t *testing.T) {
// gin.SetMode(gin.TestMode)
// w := httptest.NewRecorder()
// ctx, _ := gin.CreateTestContext(w)
// ctx.Request = httptest.NewRequest("GET", "/test", nil)
// ctx.Set(OperationName, "test-operation")
//
// // 保存原始状态
// originalHandlers := make([]ErrorHandler, len(registeredErrorHandlers))
// copy(originalHandlers, registeredErrorHandlers)
// originalFormatters := make([]ResponseFormatter, len(registeredResponseFormatters))
// copy(originalFormatters, registeredResponseFormatters)
//
// // 清理
// registeredErrorHandlers = []ErrorHandler{}
// registeredResponseFormatters = []ResponseFormatter{}
//
// // 注册一个不匹配的格式化器
// nonMatchingFormatter := &testResponseFormatter{
// shouldMatch: false, // 不匹配
// }
// registeredResponseFormatters = append(registeredResponseFormatters, nonMatchingFormatter)
//
// testErr := errors.New("test error")
// executeErrorHandlers(testErr, ctx)
//
// // 验证使用了默认格式化器(返回 500)
// assert.Equal(t, http.StatusInternalServerError, w.Code)
// assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
//
// // 恢复原始状态
// registeredErrorHandlers = originalHandlers
// registeredResponseFormatters = originalFormatters
//}
func TestRemoteHTTPError_Methods(t *testing.T) {
body := []byte("oops")
headers := http.Header{}
headers.Set("X-Test", "1")
headers.Set("Content-Type", "application/problem+json")
err := NewRemoteHTTPError(502, headers, body, "")
assert.Equal(t, 502, err.Status())
assert.Equal(t, body, err.Body())
assert.Equal(t, headers, err.Headers())
assert.Equal(t, "application/problem+json", err.ContentType())
assert.Contains(t, err.Error(), "oops")
}
func TestRemoteHTTPError_ContentTypeFallback(t *testing.T) {
// 没有 contentType,也没有 headers 中的 Content-Type,应回退为 application/json
body := []byte("err")
headers := http.Header{}
err := NewRemoteHTTPError(400, headers, body, "")
assert.Equal(t, "application/json", err.ContentType())
// 显式传入 contentType 优先生效
err2 := NewRemoteHTTPError(400, nil, body, "text/plain")
assert.Equal(t, "text/plain", err2.ContentType())
}