#5 Fix SetUI not work bug & CDN address

This commit is contained in:
ElvinChan 2018-09-15 10:10:37 +08:00
parent 245aaa7753
commit 8ed0f9a124
3 changed files with 12 additions and 13 deletions

View File

@ -1,7 +1,7 @@
package echoswagger package echoswagger
// CDN refer to https://www.jsdelivr.com/package/npm/swagger-ui-dist // CDN refer to https://www.jsdelivr.com/package/npm/swagger-ui-dist
const DefaultCDN = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@3.18.3" const DefaultCDN = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@3.18.3-republish2"
const SwaggerUIContent = `{{define "swagger"}} const SwaggerUIContent = `{{define "swagger"}}
<!DOCTYPE html> <!DOCTYPE html>

View File

@ -36,13 +36,11 @@ func (r *Root) docHandler(swaggerPath string) echo.HandlerFunc {
if err != nil { if err != nil {
panic(err) panic(err)
} }
return func(c echo.Context) error {
cdn := DefaultCDN cdn := DefaultCDN
if r.ui.CDN != "" { if r.ui.CDN != "" {
cdn = r.ui.CDN cdn = r.ui.CDN
} }
return func(c echo.Context) error {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
t.Execute(buf, map[string]interface{}{ t.Execute(buf, map[string]interface{}{
"title": r.spec.Info.Title, "title": r.spec.Info.Title,

View File

@ -293,17 +293,18 @@ func TestAddResponse(t *testing.T) {
func TestUI(t *testing.T) { func TestUI(t *testing.T) {
r := New(echo.New(), "/", "doc/", nil) r := New(echo.New(), "/", "doc/", nil)
cdn := "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.18.1"
r.SetUI(UISetting{
HideTop: true,
CDN: cdn,
})
se := r.(*Root) se := r.(*Root)
req := httptest.NewRequest(echo.GET, "/doc/", nil) req := httptest.NewRequest(echo.GET, "/doc/", nil)
rec := httptest.NewRecorder() rec := httptest.NewRecorder()
c := se.echo.NewContext(req, rec) c := se.echo.NewContext(req, rec)
h := se.docHandler("/doc/swagger.json") h := se.docHandler("/doc/swagger.json")
cdn := "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.18.0"
r.SetUI(UISetting{
HideTop: true,
CDN: cdn,
})
if assert.NoError(t, h(c)) { if assert.NoError(t, h(c)) {
assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(t, http.StatusOK, rec.Code)
assert.Contains(t, rec.Body.String(), cdn) assert.Contains(t, rec.Body.String(), cdn)