|
8 | 8 |
|
9 | 9 | "gomodel/internal/admin" |
10 | 10 | "gomodel/internal/admin/dashboard" |
| 11 | + |
| 12 | + _ "gomodel/cmd/gomodel/docs" |
11 | 13 | ) |
12 | 14 |
|
13 | 15 | func TestMetricsEndpoint(t *testing.T) { |
@@ -399,3 +401,76 @@ func TestHealthEndpointAlwaysAvailable(t *testing.T) { |
399 | 401 | }) |
400 | 402 | } |
401 | 403 | } |
| 404 | + |
| 405 | +func TestSwaggerEndpoint_Enabled(t *testing.T) { |
| 406 | + mock := &mockProvider{} |
| 407 | + srv := New(mock, &Config{SwaggerEnabled: true}) |
| 408 | + |
| 409 | + req := httptest.NewRequest(http.MethodGet, "/swagger/index.html", nil) |
| 410 | + rec := httptest.NewRecorder() |
| 411 | + |
| 412 | + srv.ServeHTTP(rec, req) |
| 413 | + |
| 414 | + if rec.Code != http.StatusOK { |
| 415 | + t.Errorf("expected status 200, got %d", rec.Code) |
| 416 | + } |
| 417 | + |
| 418 | + contentType := rec.Header().Get("Content-Type") |
| 419 | + if !strings.Contains(contentType, "text/html") { |
| 420 | + t.Errorf("expected text/html Content-Type, got %s", contentType) |
| 421 | + } |
| 422 | + |
| 423 | + if !strings.Contains(rec.Body.String(), "swagger") { |
| 424 | + t.Errorf("expected body to contain swagger UI content, got: %s", rec.Body.String()[:min(200, len(rec.Body.String()))]) |
| 425 | + } |
| 426 | +} |
| 427 | + |
| 428 | +func TestSwaggerEndpoint_Disabled(t *testing.T) { |
| 429 | + mock := &mockProvider{} |
| 430 | + srv := New(mock, &Config{SwaggerEnabled: false}) |
| 431 | + |
| 432 | + req := httptest.NewRequest(http.MethodGet, "/swagger/index.html", nil) |
| 433 | + rec := httptest.NewRecorder() |
| 434 | + |
| 435 | + srv.ServeHTTP(rec, req) |
| 436 | + |
| 437 | + if rec.Code != http.StatusNotFound { |
| 438 | + t.Errorf("expected status 404, got %d", rec.Code) |
| 439 | + } |
| 440 | +} |
| 441 | + |
| 442 | +func TestSwaggerEndpoint_NilConfig(t *testing.T) { |
| 443 | + mock := &mockProvider{} |
| 444 | + srv := New(mock, nil) |
| 445 | + |
| 446 | + req := httptest.NewRequest(http.MethodGet, "/swagger/index.html", nil) |
| 447 | + rec := httptest.NewRecorder() |
| 448 | + |
| 449 | + srv.ServeHTTP(rec, req) |
| 450 | + |
| 451 | + if rec.Code != http.StatusNotFound { |
| 452 | + t.Errorf("expected status 404, got %d", rec.Code) |
| 453 | + } |
| 454 | +} |
| 455 | + |
| 456 | +func TestSwaggerDocJson_ReturnsExpectedContent(t *testing.T) { |
| 457 | + mock := &mockProvider{} |
| 458 | + srv := New(mock, &Config{SwaggerEnabled: true}) |
| 459 | + |
| 460 | + req := httptest.NewRequest(http.MethodGet, "/swagger/doc.json", nil) |
| 461 | + rec := httptest.NewRecorder() |
| 462 | + |
| 463 | + srv.ServeHTTP(rec, req) |
| 464 | + |
| 465 | + if rec.Code != http.StatusOK { |
| 466 | + t.Errorf("expected status 200, got %d", rec.Code) |
| 467 | + } |
| 468 | + |
| 469 | + body := rec.Body.String() |
| 470 | + if !strings.Contains(body, "GOModel") { |
| 471 | + t.Errorf("expected doc.json to contain GOModel API title, got: %s", body[:min(300, len(body))]) |
| 472 | + } |
| 473 | + if !strings.Contains(body, "swagger") { |
| 474 | + t.Errorf("expected doc.json to contain swagger spec, got: %s", body[:min(300, len(body))]) |
| 475 | + } |
| 476 | +} |
0 commit comments