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
|
package middleware
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSwaggerUIOAuth2CallbackMiddleware(t *testing.T) {
t.Run("with defaults", func(t *testing.T) {
doc := SwaggerUIOAuth2Callback(SwaggerUIOpts{}, nil)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/docs/oauth2-callback", nil)
require.NoError(t, err)
recorder := httptest.NewRecorder()
doc.ServeHTTP(recorder, req)
require.Equal(t, http.StatusOK, recorder.Code)
assert.Equal(t, "text/html; charset=utf-8", recorder.Header().Get(contentTypeHeader))
var o SwaggerUIOpts
o.EnsureDefaultsOauth2()
htmlResponse := recorder.Body.String()
assert.Contains(t, htmlResponse, fmt.Sprintf("<title>%s</title>", o.Title))
assert.Contains(t, htmlResponse, `oauth2.auth.schema.get("flow") === "accessCode"`)
})
t.Run("edge cases", func(t *testing.T) {
t.Run("with custom template that fails to execute", func(t *testing.T) {
assert.Panics(t, func() {
SwaggerUIOAuth2Callback(SwaggerUIOpts{
Template: `<!DOCTYPE html>
<html>
spec-url='{{ .Unknown }}'
</html>
`,
}, nil)
})
})
})
}
|