package middleware import ( "context" "fmt" "net/http" "net/http/httptest" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestRedocMiddleware(t *testing.T) { t.Run("with defaults", func(t *testing.T) { redoc := Redoc(RedocOpts{}, nil) req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/docs", nil) require.NoError(t, err) recorder := httptest.NewRecorder() redoc.ServeHTTP(recorder, req) assert.Equal(t, http.StatusOK, recorder.Code) assert.Equal(t, "text/html; charset=utf-8", recorder.Header().Get(contentTypeHeader)) var o RedocOpts o.EnsureDefaults() assert.Contains(t, recorder.Body.String(), fmt.Sprintf("%s", o.Title)) assert.Contains(t, recorder.Body.String(), fmt.Sprintf("", o.SpecURL)) assert.Contains(t, recorder.Body.String(), redocLatest) }) t.Run("with alternate path and spec URL", func(t *testing.T) { redoc := Redoc(RedocOpts{ BasePath: "/base", Path: "ui", SpecURL: "/ui/swagger.json", }, nil) req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/base/ui", nil) require.NoError(t, err) recorder := httptest.NewRecorder() redoc.ServeHTTP(recorder, req) assert.Equal(t, http.StatusOK, recorder.Code) assert.Contains(t, recorder.Body.String(), "") }) t.Run("with custom template", func(t *testing.T) { redoc := Redoc(RedocOpts{ Template: ` {{ .Title }}