-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialization_error_test.go
More file actions
107 lines (88 loc) · 3.07 KB
/
serialization_error_test.go
File metadata and controls
107 lines (88 loc) · 3.07 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
package fiberoapi
import (
"encoding/json"
"io"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
)
// UnserializableOutput contains a channel which cannot be serialized to JSON
type UnserializableOutput struct {
Name string `json:"name"`
Channel chan string `json:"channel"` // Channels cannot be JSON serialized
}
type SerializationTestInput struct{}
type SerializationTestError struct {
StatusCode int `json:"statusCode"`
Message string `json:"message"`
}
func TestResponseSerializationError(t *testing.T) {
app := fiber.New()
oapi := New(app)
// Handler that returns an unserializable output (contains a channel)
Get(oapi, "/unserializable", func(c *fiber.Ctx, input SerializationTestInput) (UnserializableOutput, *SerializationTestError) {
return UnserializableOutput{
Name: "test",
Channel: make(chan string), // This will fail JSON marshaling
}, nil
}, OpenAPIOptions{
Summary: "Test endpoint with unserializable response",
})
req := httptest.NewRequest("GET", "/unserializable", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to make request: %v", err)
}
// Should return 500 status code
if resp.StatusCode != 500 {
t.Errorf("Expected status 500, got %d", resp.StatusCode)
}
// Should return a proper error response
body, _ := io.ReadAll(resp.Body)
var errorResp ErrorResponse
if err := json.Unmarshal(body, &errorResp); err != nil {
t.Fatalf("Failed to unmarshal error response: %v. Body: %s", err, string(body))
}
if errorResp.Code != 500 {
t.Errorf("Expected error code 500, got %d", errorResp.Code)
}
if errorResp.Type != "serialization_error" {
t.Errorf("Expected error type 'serialization_error', got '%s'", errorResp.Type)
}
}
// UnserializableError contains a channel which cannot be serialized
type UnserializableError struct {
StatusCode int `json:"statusCode"`
Channel chan string `json:"channel"`
}
func TestErrorSerializationError(t *testing.T) {
app := fiber.New()
oapi := New(app)
// Handler that returns an unserializable error
Get(oapi, "/unserializable-error", func(c *fiber.Ctx, input SerializationTestInput) (map[string]string, *UnserializableError) {
return nil, &UnserializableError{
StatusCode: 400,
Channel: make(chan string), // This will fail JSON marshaling
}
}, OpenAPIOptions{
Summary: "Test endpoint with unserializable error",
})
req := httptest.NewRequest("GET", "/unserializable-error", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to make request: %v", err)
}
// Should return 500 status code (fallback error)
if resp.StatusCode != 500 {
t.Errorf("Expected status 500, got %d", resp.StatusCode)
}
// Should return a proper error response
body, _ := io.ReadAll(resp.Body)
var errorResp map[string]string
if err := json.Unmarshal(body, &errorResp); err != nil {
t.Fatalf("Failed to unmarshal error response: %v. Body: %s", err, string(body))
}
if errorResp["error"] != "Failed to serialize error response" {
t.Errorf("Expected error message 'Failed to serialize error response', got '%s'", errorResp["error"])
}
}