Skip to content

Commit

Permalink
improve mimetype handling
Browse files Browse the repository at this point in the history
fix mimetype
  • Loading branch information
ubogdan authored Oct 12, 2021
2 parents d563780 + 53eec42 commit 231d156
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
22 changes: 14 additions & 8 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"html/template"
"net/http"
"path/filepath"
"regexp"
"sync"

Expand Down Expand Up @@ -37,6 +38,19 @@ func WrapHandler(h *webdav.Handler) buffalo.Handler {
h.Prefix = matches[1]
})

switch filepath.Ext(path) {
case ".html":
c.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
case ".css":
c.Response().Header().Set("Content-Type", "text/css; charset=utf-8")
case ".js":
c.Response().Header().Set("Content-Type", "application/javascript")
case ".json":
c.Response().Header().Set("Content-Type", "application/json; charset=utf-8")
case ".png":
c.Response().Header().Set("Content-Type", "image/png")
}

switch path {
case "index.html":
s := &pro{
Expand All @@ -46,14 +60,6 @@ func WrapHandler(h *webdav.Handler) buffalo.Handler {
case "doc.json":
doc, _ := swag.ReadDoc()
c.Response().Write([]byte(doc))
case "swagger-ui-bundle.js", "swagger-ui.js", "swagger-ui-standalone-preset.js":
// The browser needs to interpret JS as a JS scripts
c.Response().Header().Add("Content-Type", "application/javascript")
h.ServeHTTP(c.Response(), c.Request())
case "swagger-ui.css":
// The browser needs to interpret CSS as a stylesheet
c.Response().Header().Add("Content-Type", "text/css")
h.ServeHTTP(c.Response(), c.Request())
default:
h.ServeHTTP(c.Response(), c.Request())
}
Expand Down
11 changes: 9 additions & 2 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestWrapHandler(t *testing.T) {

w1 := performRequest("GET", "/swagger/index.html", router)
assert.Equal(t, 200, w1.Code)
assert.Equal(t, w1.Header()["Content-Type"][0], "text/html; charset=utf-8")

swag.Register(swag.Name, &mockedSwag{})

Expand All @@ -36,12 +37,18 @@ func TestWrapHandler(t *testing.T) {

w3 := performRequest("GET", "/swagger/favicon-16x16.png", router)
assert.Equal(t, 200, w3.Code)
assert.Equal(t, w3.Header()["Content-Type"][0], "image/png")

w4 := performRequest("GET", "/swagger/swagger-ui.css", router)
assert.Equal(t, 200, w4.Code)
assert.Equal(t, w4.Header()["Content-Type"][0], "text/css; charset=utf-8")

w5 := performRequest("GET", "/swagger/notfound", router)
assert.Equal(t, 404, w5.Code)
w5 := performRequest("GET", "/swagger/swagger-ui-bundle.js", router)
assert.Equal(t, 200, w5.Code)
assert.Equal(t, w5.Header()["Content-Type"][0], "application/javascript")

w6 := performRequest("GET", "/swagger/notfound", router)
assert.Equal(t, 404, w6.Code)
}

func performRequest(method, target string, router *buffalo.App) *httptest.ResponseRecorder {
Expand Down

0 comments on commit 231d156

Please sign in to comment.