Skip to content

Commit

Permalink
Use correct mime type when no content is sent (#2515)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
nitram509 and qwerty287 authored Oct 3, 2023
1 parent e8ef1fb commit 570141e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
8 changes: 4 additions & 4 deletions server/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
26 changes: 18 additions & 8 deletions server/web/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
})
}
}
Expand Down

0 comments on commit 570141e

Please sign in to comment.