Merge pull request #6 from elvinchan/5

#5 Fix SetUI not work bug & CDN address
This commit is contained in:
陈文强 2018-09-15 10:31:09 +08:00 committed by GitHub
commit e37e18e720
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)
} }
cdn := DefaultCDN
if r.ui.CDN != "" {
cdn = r.ui.CDN
}
return func(c echo.Context) error { return func(c echo.Context) error {
cdn := DefaultCDN
if r.ui.CDN != "" {
cdn = r.ui.CDN
}
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)