...

Source file src/github.com/go-openapi/runtime/middleware/swaggerui_oauth2_test.go

Documentation: github.com/go-openapi/runtime/middleware

     1  package middleware
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestSwaggerUIOAuth2CallbackMiddleware(t *testing.T) {
    15  	t.Run("with defaults", func(t *testing.T) {
    16  		doc := SwaggerUIOAuth2Callback(SwaggerUIOpts{}, nil)
    17  
    18  		req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/docs/oauth2-callback", nil)
    19  		require.NoError(t, err)
    20  		recorder := httptest.NewRecorder()
    21  
    22  		doc.ServeHTTP(recorder, req)
    23  		require.Equal(t, http.StatusOK, recorder.Code)
    24  		assert.Equal(t, "text/html; charset=utf-8", recorder.Header().Get(contentTypeHeader))
    25  
    26  		var o SwaggerUIOpts
    27  		o.EnsureDefaultsOauth2()
    28  		htmlResponse := recorder.Body.String()
    29  		assert.Contains(t, htmlResponse, fmt.Sprintf("<title>%s</title>", o.Title))
    30  		assert.Contains(t, htmlResponse, `oauth2.auth.schema.get("flow") === "accessCode"`)
    31  	})
    32  
    33  	t.Run("edge cases", func(t *testing.T) {
    34  		t.Run("with custom template that fails to execute", func(t *testing.T) {
    35  			assert.Panics(t, func() {
    36  				SwaggerUIOAuth2Callback(SwaggerUIOpts{
    37  					Template: `<!DOCTYPE html>
    38  <html>
    39  	spec-url='{{ .Unknown }}'
    40  </html>
    41  `,
    42  				}, nil)
    43  			})
    44  		})
    45  	})
    46  }
    47  

View as plain text