File size: 1,840 Bytes
6bc074c | 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 | package apierrors
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func setupTestContext() (*gin.Context, *httptest.ResponseRecorder) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request, _ = http.NewRequest("POST", "/test", nil)
return c, w
}
func TestInvalidRequest(t *testing.T) {
c, w := setupTestContext()
InvalidRequest(c, "bad input", "test_error")
if w.Code != http.StatusBadRequest {
t.Errorf("expected status %d, got %d", http.StatusBadRequest, w.Code)
}
body := w.Body.String()
if body == "" {
t.Error("expected non-empty response body")
}
}
func TestMissingParam(t *testing.T) {
c, w := setupTestContext()
MissingParam(c, "prompt", "missing_required_parameter")
if w.Code != http.StatusBadRequest {
t.Errorf("expected status %d, got %d", http.StatusBadRequest, w.Code)
}
}
func TestAuthError(t *testing.T) {
c, w := setupTestContext()
AuthError(c, http.StatusUnauthorized, "invalid token")
if w.Code != http.StatusUnauthorized {
t.Errorf("expected status %d, got %d", http.StatusUnauthorized, w.Code)
}
}
func TestInternalError(t *testing.T) {
c, w := setupTestContext()
InternalError(c, "server_error", "something broke", 500)
if w.Code != http.StatusInternalServerError {
t.Errorf("expected status %d, got %d", http.StatusInternalServerError, w.Code)
}
}
func TestBadRequest(t *testing.T) {
c, w := setupTestContext()
BadRequest(c, "invalid_type", "bad request", "test_code")
if w.Code != http.StatusBadRequest {
t.Errorf("expected status %d, got %d", http.StatusBadRequest, w.Code)
}
}
func TestNotFoundAccount(t *testing.T) {
c, w := setupTestContext()
NotFoundAccount(c)
if w.Code != http.StatusBadRequest {
t.Errorf("expected status %d, got %d", http.StatusBadRequest, w.Code)
}
}
|