|
| 1 | +package fiberoapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/gofiber/fiber/v2" |
| 10 | +) |
| 11 | + |
| 12 | +// UnserializableOutput contains a channel which cannot be serialized to JSON |
| 13 | +type UnserializableOutput struct { |
| 14 | + Name string `json:"name"` |
| 15 | + Channel chan string `json:"channel"` // Channels cannot be JSON serialized |
| 16 | +} |
| 17 | + |
| 18 | +type SerializationTestInput struct{} |
| 19 | + |
| 20 | +type SerializationTestError struct { |
| 21 | + StatusCode int `json:"statusCode"` |
| 22 | + Message string `json:"message"` |
| 23 | +} |
| 24 | + |
| 25 | +func TestResponseSerializationError(t *testing.T) { |
| 26 | + app := fiber.New() |
| 27 | + oapi := New(app) |
| 28 | + |
| 29 | + // Handler that returns an unserializable output (contains a channel) |
| 30 | + Get(oapi, "/unserializable", func(c *fiber.Ctx, input SerializationTestInput) (UnserializableOutput, *SerializationTestError) { |
| 31 | + return UnserializableOutput{ |
| 32 | + Name: "test", |
| 33 | + Channel: make(chan string), // This will fail JSON marshaling |
| 34 | + }, nil |
| 35 | + }, OpenAPIOptions{ |
| 36 | + Summary: "Test endpoint with unserializable response", |
| 37 | + }) |
| 38 | + |
| 39 | + req := httptest.NewRequest("GET", "/unserializable", nil) |
| 40 | + resp, err := app.Test(req) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("Failed to make request: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + // Should return 500 status code |
| 46 | + if resp.StatusCode != 500 { |
| 47 | + t.Errorf("Expected status 500, got %d", resp.StatusCode) |
| 48 | + } |
| 49 | + |
| 50 | + // Should return a proper error response |
| 51 | + body, _ := io.ReadAll(resp.Body) |
| 52 | + var errorResp ErrorResponse |
| 53 | + if err := json.Unmarshal(body, &errorResp); err != nil { |
| 54 | + t.Fatalf("Failed to unmarshal error response: %v. Body: %s", err, string(body)) |
| 55 | + } |
| 56 | + |
| 57 | + if errorResp.Code != 500 { |
| 58 | + t.Errorf("Expected error code 500, got %d", errorResp.Code) |
| 59 | + } |
| 60 | + |
| 61 | + if errorResp.Type != "serialization_error" { |
| 62 | + t.Errorf("Expected error type 'serialization_error', got '%s'", errorResp.Type) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// UnserializableError contains a channel which cannot be serialized |
| 67 | +type UnserializableError struct { |
| 68 | + StatusCode int `json:"statusCode"` |
| 69 | + Channel chan string `json:"channel"` |
| 70 | +} |
| 71 | + |
| 72 | +func TestErrorSerializationError(t *testing.T) { |
| 73 | + app := fiber.New() |
| 74 | + oapi := New(app) |
| 75 | + |
| 76 | + // Handler that returns an unserializable error |
| 77 | + Get(oapi, "/unserializable-error", func(c *fiber.Ctx, input SerializationTestInput) (map[string]string, *UnserializableError) { |
| 78 | + return nil, &UnserializableError{ |
| 79 | + StatusCode: 400, |
| 80 | + Channel: make(chan string), // This will fail JSON marshaling |
| 81 | + } |
| 82 | + }, OpenAPIOptions{ |
| 83 | + Summary: "Test endpoint with unserializable error", |
| 84 | + }) |
| 85 | + |
| 86 | + req := httptest.NewRequest("GET", "/unserializable-error", nil) |
| 87 | + resp, err := app.Test(req) |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("Failed to make request: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + // Should return 500 status code (fallback error) |
| 93 | + if resp.StatusCode != 500 { |
| 94 | + t.Errorf("Expected status 500, got %d", resp.StatusCode) |
| 95 | + } |
| 96 | + |
| 97 | + // Should return a proper error response |
| 98 | + body, _ := io.ReadAll(resp.Body) |
| 99 | + var errorResp map[string]string |
| 100 | + if err := json.Unmarshal(body, &errorResp); err != nil { |
| 101 | + t.Fatalf("Failed to unmarshal error response: %v. Body: %s", err, string(body)) |
| 102 | + } |
| 103 | + |
| 104 | + if errorResp["error"] != "Failed to serialize error response" { |
| 105 | + t.Errorf("Expected error message 'Failed to serialize error response', got '%s'", errorResp["error"]) |
| 106 | + } |
| 107 | +} |
0 commit comments