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:
Martin W. Kirst 2023-10-03 12:28:20 +02:00 committed by GitHub
parent e8ef1fb3c1
commit 570141eae7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 12 deletions

View file

@ -72,19 +72,19 @@ func New() (*gin.Engine, error) {
}
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 {
http.ServeFile(w, r, localFileName)
} else {
// 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) {
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") {
serveFileOrEmptyContent(ctx.Writer, ctx.Request, server.Config.Server.CustomCSSFile)
serveFileOrEmptyContent(ctx.Writer, ctx.Request, server.Config.Server.CustomCSSFile, "file.css")
} else {
serveFile(fs)(ctx)
}

View file

@ -25,18 +25,27 @@ import (
"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)
customFiles := []string{
"/assets/custom.js",
"/assets/custom.css",
filesToTest := []struct {
fileURL string
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 {
t.Run(f, func(t *testing.T) {
request, err := http.NewRequest(http.MethodGet, f, nil)
request.RequestURI = f // additional required for mocking
for _, f := range filesToTest {
t.Run(f.fileURL, func(t *testing.T) {
request, err := http.NewRequest(http.MethodGet, f.fileURL, nil)
request.RequestURI = f.fileURL // additional required for mocking
assert.NoError(t, err)
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, []byte(nil), rr.Body.Bytes())
assert.Contains(t, rr.Header().Get("Content-Type"), f.shortMimetype)
})
}
}