mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-20 14:19:00 +00:00
Use correct mime type when no content is sent (#2515)
closes #2514 The fix is simple, just providing a file name, so `http.ServeContent(...)` can set the correct mimeype in case the content is zero bytes. The test was just extended. PS: I would appreciate a `hacktoberfest-accepted` label ;) --------- Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
This commit is contained in:
parent
e8ef1fb3c1
commit
570141eae7
2 changed files with 22 additions and 12 deletions
|
@ -72,19 +72,19 @@ func New() (*gin.Engine, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleCustomFilesAndAssets(fs *prefixFS) func(ctx *gin.Context) {
|
func handleCustomFilesAndAssets(fs *prefixFS) func(ctx *gin.Context) {
|
||||||
serveFileOrEmptyContent := func(w http.ResponseWriter, r *http.Request, localFileName string) {
|
serveFileOrEmptyContent := func(w http.ResponseWriter, r *http.Request, localFileName, fileName string) {
|
||||||
if len(localFileName) > 0 {
|
if len(localFileName) > 0 {
|
||||||
http.ServeFile(w, r, localFileName)
|
http.ServeFile(w, r, localFileName)
|
||||||
} else {
|
} else {
|
||||||
// prefer zero content over sending a 404 Not Found
|
// prefer zero content over sending a 404 Not Found
|
||||||
http.ServeContent(w, r, localFileName, time.Now(), bytes.NewReader([]byte{}))
|
http.ServeContent(w, r, fileName, time.Now(), bytes.NewReader([]byte{}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return func(ctx *gin.Context) {
|
return func(ctx *gin.Context) {
|
||||||
if strings.HasSuffix(ctx.Request.RequestURI, "/assets/custom.js") {
|
if strings.HasSuffix(ctx.Request.RequestURI, "/assets/custom.js") {
|
||||||
serveFileOrEmptyContent(ctx.Writer, ctx.Request, server.Config.Server.CustomJsFile)
|
serveFileOrEmptyContent(ctx.Writer, ctx.Request, server.Config.Server.CustomJsFile, "file.js")
|
||||||
} else if strings.HasSuffix(ctx.Request.RequestURI, "/assets/custom.css") {
|
} else if strings.HasSuffix(ctx.Request.RequestURI, "/assets/custom.css") {
|
||||||
serveFileOrEmptyContent(ctx.Writer, ctx.Request, server.Config.Server.CustomCSSFile)
|
serveFileOrEmptyContent(ctx.Writer, ctx.Request, server.Config.Server.CustomCSSFile, "file.css")
|
||||||
} else {
|
} else {
|
||||||
serveFile(fs)(ctx)
|
serveFile(fs)(ctx)
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,18 +25,27 @@ import (
|
||||||
"github.com/woodpecker-ci/woodpecker/server"
|
"github.com/woodpecker-ci/woodpecker/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_custom_file_returns_OK_and_empty_content(t *testing.T) {
|
func Test_custom_file_returns_OK_and_empty_content_and_fitting_mimetype(t *testing.T) {
|
||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
|
|
||||||
customFiles := []string{
|
filesToTest := []struct {
|
||||||
"/assets/custom.js",
|
fileURL string
|
||||||
"/assets/custom.css",
|
shortMimetype string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
fileURL: "/assets/custom.js",
|
||||||
|
shortMimetype: "javascript", // using just the short version, since it depends on the go runtime/version
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fileURL: "/assets/custom.css",
|
||||||
|
shortMimetype: "css", // using just the short version, since it depends on the go runtime/version
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, f := range customFiles {
|
for _, f := range filesToTest {
|
||||||
t.Run(f, func(t *testing.T) {
|
t.Run(f.fileURL, func(t *testing.T) {
|
||||||
request, err := http.NewRequest(http.MethodGet, f, nil)
|
request, err := http.NewRequest(http.MethodGet, f.fileURL, nil)
|
||||||
request.RequestURI = f // additional required for mocking
|
request.RequestURI = f.fileURL // additional required for mocking
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
rr := httptest.NewRecorder()
|
rr := httptest.NewRecorder()
|
||||||
|
@ -45,6 +54,7 @@ func Test_custom_file_returns_OK_and_empty_content(t *testing.T) {
|
||||||
|
|
||||||
assert.Equal(t, 200, rr.Code)
|
assert.Equal(t, 200, rr.Code)
|
||||||
assert.Equal(t, []byte(nil), rr.Body.Bytes())
|
assert.Equal(t, []byte(nil), rr.Body.Bytes())
|
||||||
|
assert.Contains(t, rr.Header().Get("Content-Type"), f.shortMimetype)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue