|
| 1 | +package anthropic |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | +) |
| 12 | + |
| 13 | +func TestWriteAnthropicError(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + statusCode int |
| 19 | + errorType string |
| 20 | + message string |
| 21 | + wantBody string |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "invalid request error", |
| 25 | + statusCode: http.StatusBadRequest, |
| 26 | + errorType: "invalid_request_error", |
| 27 | + message: "Missing required field: model", |
| 28 | + wantBody: `{"type":"error","error":{"type":"invalid_request_error","message":"Missing required field: model"}}`, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "not found error", |
| 32 | + statusCode: http.StatusNotFound, |
| 33 | + errorType: "not_found_error", |
| 34 | + message: "Model not found: test-model", |
| 35 | + wantBody: `{"type":"error","error":{"type":"not_found_error","message":"Model not found: test-model"}}`, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "internal error", |
| 39 | + statusCode: http.StatusInternalServerError, |
| 40 | + errorType: "internal_error", |
| 41 | + message: "An internal error occurred", |
| 42 | + wantBody: `{"type":"error","error":{"type":"internal_error","message":"An internal error occurred"}}`, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + for _, tt := range tests { |
| 47 | + t.Run(tt.name, func(t *testing.T) { |
| 48 | + t.Parallel() |
| 49 | + |
| 50 | + rec := httptest.NewRecorder() |
| 51 | + discard := logrus.New() |
| 52 | + discard.SetOutput(io.Discard) |
| 53 | + h := &Handler{log: logrus.NewEntry(discard)} |
| 54 | + h.writeAnthropicError(rec, tt.statusCode, tt.errorType, tt.message) |
| 55 | + |
| 56 | + if rec.Code != tt.statusCode { |
| 57 | + t.Errorf("expected status %d, got %d", tt.statusCode, rec.Code) |
| 58 | + } |
| 59 | + |
| 60 | + if contentType := rec.Header().Get("Content-Type"); contentType != "application/json" { |
| 61 | + t.Errorf("expected Content-Type application/json, got %s", contentType) |
| 62 | + } |
| 63 | + |
| 64 | + body := strings.TrimSpace(rec.Body.String()) |
| 65 | + if body != tt.wantBody { |
| 66 | + t.Errorf("expected body %s, got %s", tt.wantBody, body) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestRouteHandlers(t *testing.T) { |
| 73 | + t.Parallel() |
| 74 | + |
| 75 | + h := &Handler{ |
| 76 | + router: http.NewServeMux(), |
| 77 | + } |
| 78 | + |
| 79 | + routes := h.routeHandlers() |
| 80 | + |
| 81 | + expectedRoutes := []string{ |
| 82 | + "POST " + APIPrefix + "/v1/messages", |
| 83 | + "POST " + APIPrefix + "/v1/messages/count_tokens", |
| 84 | + } |
| 85 | + |
| 86 | + for _, route := range expectedRoutes { |
| 87 | + if _, exists := routes[route]; !exists { |
| 88 | + t.Errorf("expected route %s to be registered", route) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if len(routes) != len(expectedRoutes) { |
| 93 | + t.Errorf("expected %d routes, got %d", len(expectedRoutes), len(routes)) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestAPIPrefix(t *testing.T) { |
| 98 | + t.Parallel() |
| 99 | + |
| 100 | + if APIPrefix != "/anthropic" { |
| 101 | + t.Errorf("expected APIPrefix to be /anthropic, got %s", APIPrefix) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestProxyToBackend_InvalidJSON(t *testing.T) { |
| 106 | + t.Parallel() |
| 107 | + |
| 108 | + discard := logrus.New() |
| 109 | + discard.SetOutput(io.Discard) |
| 110 | + h := &Handler{log: logrus.NewEntry(discard)} |
| 111 | + |
| 112 | + rec := httptest.NewRecorder() |
| 113 | + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", strings.NewReader(`{invalid json`)) |
| 114 | + |
| 115 | + h.proxyToBackend(rec, req, "/v1/messages") |
| 116 | + |
| 117 | + if rec.Code != http.StatusBadRequest { |
| 118 | + t.Errorf("expected status %d, got %d", http.StatusBadRequest, rec.Code) |
| 119 | + } |
| 120 | + |
| 121 | + body := rec.Body.String() |
| 122 | + if !strings.Contains(body, "invalid_request_error") { |
| 123 | + t.Errorf("expected body to contain 'invalid_request_error', got %s", body) |
| 124 | + } |
| 125 | + if !strings.Contains(body, "Invalid JSON") { |
| 126 | + t.Errorf("expected body to contain 'Invalid JSON', got %s", body) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func TestProxyToBackend_MissingModel(t *testing.T) { |
| 131 | + t.Parallel() |
| 132 | + |
| 133 | + discard := logrus.New() |
| 134 | + discard.SetOutput(io.Discard) |
| 135 | + h := &Handler{log: logrus.NewEntry(discard)} |
| 136 | + |
| 137 | + rec := httptest.NewRecorder() |
| 138 | + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", strings.NewReader(`{"messages": []}`)) |
| 139 | + |
| 140 | + h.proxyToBackend(rec, req, "/v1/messages") |
| 141 | + |
| 142 | + if rec.Code != http.StatusBadRequest { |
| 143 | + t.Errorf("expected status %d, got %d", http.StatusBadRequest, rec.Code) |
| 144 | + } |
| 145 | + |
| 146 | + body := rec.Body.String() |
| 147 | + if !strings.Contains(body, "invalid_request_error") { |
| 148 | + t.Errorf("expected body to contain 'invalid_request_error', got %s", body) |
| 149 | + } |
| 150 | + if !strings.Contains(body, "Missing required field: model") { |
| 151 | + t.Errorf("expected body to contain 'Missing required field: model', got %s", body) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func TestProxyToBackend_EmptyModel(t *testing.T) { |
| 156 | + t.Parallel() |
| 157 | + |
| 158 | + discard := logrus.New() |
| 159 | + discard.SetOutput(io.Discard) |
| 160 | + h := &Handler{log: logrus.NewEntry(discard)} |
| 161 | + |
| 162 | + rec := httptest.NewRecorder() |
| 163 | + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", strings.NewReader(`{"model": ""}`)) |
| 164 | + |
| 165 | + h.proxyToBackend(rec, req, "/v1/messages") |
| 166 | + |
| 167 | + if rec.Code != http.StatusBadRequest { |
| 168 | + t.Errorf("expected status %d, got %d", http.StatusBadRequest, rec.Code) |
| 169 | + } |
| 170 | + |
| 171 | + body := rec.Body.String() |
| 172 | + if !strings.Contains(body, "invalid_request_error") { |
| 173 | + t.Errorf("expected body to contain 'invalid_request_error', got %s", body) |
| 174 | + } |
| 175 | + if !strings.Contains(body, "Missing required field: model") { |
| 176 | + t.Errorf("expected body to contain 'Missing required field: model', got %s", body) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +func TestProxyToBackend_RequestTooLarge(t *testing.T) { |
| 181 | + t.Parallel() |
| 182 | + |
| 183 | + discard := logrus.New() |
| 184 | + discard.SetOutput(io.Discard) |
| 185 | + h := &Handler{log: logrus.NewEntry(discard)} |
| 186 | + |
| 187 | + // Create a request body that exceeds the maxRequestBodySize (10MB) |
| 188 | + // We'll use a reader that simulates a large body without actually allocating it |
| 189 | + largeBody := strings.NewReader(`{"model": "test-model", "data": "` + strings.Repeat("x", maxRequestBodySize+1) + `"}`) |
| 190 | + |
| 191 | + rec := httptest.NewRecorder() |
| 192 | + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", largeBody) |
| 193 | + |
| 194 | + h.proxyToBackend(rec, req, "/v1/messages") |
| 195 | + |
| 196 | + if rec.Code != http.StatusRequestEntityTooLarge { |
| 197 | + t.Errorf("expected status %d, got %d", http.StatusRequestEntityTooLarge, rec.Code) |
| 198 | + } |
| 199 | + |
| 200 | + body := rec.Body.String() |
| 201 | + if !strings.Contains(body, "request_too_large") { |
| 202 | + t.Errorf("expected body to contain 'request_too_large', got %s", body) |
| 203 | + } |
| 204 | +} |
0 commit comments