40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
package scalar
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestScalarUIHandlesQueryAndMissingAssets(t *testing.T) {
|
|
engine := gin.New()
|
|
engine.GET("/docs/*any", New("Test <API>", "/docs").Handler())
|
|
|
|
t.Run("index with query", func(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodGet, "/docs/index.html?cache=1", nil)
|
|
engine.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusOK)
|
|
}
|
|
if contentType := recorder.Header().Get("Content-Type"); !strings.HasPrefix(contentType, "text/html") {
|
|
t.Fatalf("Content-Type = %q, want HTML", contentType)
|
|
}
|
|
if strings.Contains(recorder.Body.String(), "Test <API>") {
|
|
t.Fatal("document title was not HTML escaped")
|
|
}
|
|
})
|
|
|
|
t.Run("missing asset", func(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodGet, "/docs/missing.js", nil)
|
|
engine.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusNotFound {
|
|
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusNotFound)
|
|
}
|
|
})
|
|
}
|