Skip to content

Commit 3a2bf31

Browse files
committed
test(gom-25): add coverage
1 parent 5469119 commit 3a2bf31

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

internal/server/http_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
"gomodel/internal/admin"
1010
"gomodel/internal/admin/dashboard"
11+
12+
_ "gomodel/cmd/gomodel/docs"
1113
)
1214

1315
func TestMetricsEndpoint(t *testing.T) {
@@ -399,3 +401,76 @@ func TestHealthEndpointAlwaysAvailable(t *testing.T) {
399401
})
400402
}
401403
}
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

Comments
 (0)