Skip to content

Commit

Permalink
Merge branch 'main' into renovate-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pat-s authored Oct 4, 2023
2 parents 9f2bfd2 + 570141e commit d444fe2
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 d444fe2

Please sign in to comment.