package chattest import ( "encoding/json" "net/http" "testing" ) // ErrorResponse describes an HTTP error that a test server should return // instead of a normal streaming or JSON response. type ErrorResponse struct { StatusCode int Type string Code string Message string } // writeErrorResponse writes a JSON error response matching the common // provider error format used by both Anthropic and OpenAI. func writeErrorResponse(t testing.TB, w http.ResponseWriter, errResp *ErrorResponse) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(errResp.StatusCode) errorBody := map[string]interface{}{ "type": errResp.Type, "message": errResp.Message, } if errResp.Code != "" { errorBody["code"] = errResp.Code } body := map[string]interface{}{"error": errorBody} if err := json.NewEncoder(w).Encode(body); err != nil { t.Errorf("writeErrorResponse: failed to encode error response: %v", err) } } func responseShapeError(stream, hasStreaming, hasNonStreaming bool) string { switch { case hasStreaming && hasNonStreaming: return "handler returned both streaming and non-streaming responses" case !hasStreaming && !hasNonStreaming: return "handler returned empty response" case stream && !hasStreaming: return "handler returned non-streaming response for streaming request" case !stream && !hasNonStreaming: return "handler returned streaming response for non-streaming request" } return "" } // OpenAIErrorResponse returns an OpenAIResponse that causes the // test server to respond with the given HTTP status code and error. func OpenAIErrorResponse(statusCode int, errorType, message string) OpenAIResponse { return OpenAIResponse{ Error: &ErrorResponse{ StatusCode: statusCode, Type: errorType, Message: message, }, } } // OpenAIRateLimitResponse returns a 429 rate limit error. func OpenAIRateLimitResponse() OpenAIResponse { return OpenAIErrorResponse(http.StatusTooManyRequests, "rate_limit_exceeded", "Rate limit exceeded") }