Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use correct mime type when no content is sent #2515

Merged
merged 5 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 string, contentTypeName 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, contentTypeName, 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
mimetype string
}{
{
fileUrl: "/assets/custom.js",
mimetype: "application/javascript",
},
{
fileUrl: "/assets/custom.css",
mimetype: "text/css",
},
}

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.mimetype)
})
}
}
Expand Down