...

Source file src/github.com/go-openapi/runtime/middleware/rapidoc_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 TestRapiDocMiddleware(t *testing.T) {
    15  	t.Run("with defaults", func(t *testing.T) {
    16  		rapidoc := RapiDoc(RapiDocOpts{}, nil)
    17  
    18  		req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/docs", nil)
    19  		require.NoError(t, err)
    20  		recorder := httptest.NewRecorder()
    21  		rapidoc.ServeHTTP(recorder, req)
    22  		assert.Equal(t, http.StatusOK, recorder.Code)
    23  		assert.Equal(t, "text/html; charset=utf-8", recorder.Header().Get(contentTypeHeader))
    24  		var o RapiDocOpts
    25  		o.EnsureDefaults()
    26  		assert.Contains(t, recorder.Body.String(), fmt.Sprintf("<title>%s</title>", o.Title))
    27  		assert.Contains(t, recorder.Body.String(), fmt.Sprintf("<rapi-doc spec-url=%q></rapi-doc>", o.SpecURL))
    28  		assert.Contains(t, recorder.Body.String(), rapidocLatest)
    29  	})
    30  
    31  	t.Run("edge cases", func(t *testing.T) {
    32  		t.Run("with custom template that fails to execute", func(t *testing.T) {
    33  			assert.Panics(t, func() {
    34  				RapiDoc(RapiDocOpts{
    35  					Template: `<!DOCTYPE html>
    36  <html>
    37  	spec-url='{{ .Unknown }}'
    38  </html>
    39  `,
    40  				}, nil)
    41  			})
    42  		})
    43  	})
    44  }
    45  

View as plain text