From ce286f9d9c00ceb24fb4eab4cab56c0b0678765a Mon Sep 17 00:00:00 2001 From: Jimmy Praet Date: Wed, 30 Jun 2021 23:31:54 +0200 Subject: [PATCH] Support custom mime type mapping for text files (#16304) * Support custom mime type mapping for text files * Apply suggested change to routers/common/repo.go Co-authored-by: KN4CK3R Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: KN4CK3R --- integrations/download_test.go | 28 ++++++++++++++++++ .../10/32bbf17fbc0d9c95bb5418dabe8f8c99278700 | 2 ++ .../26/f842bcad37fa40a1bb34cbb5ee219ee35d863d | Bin 0 -> 75 bytes .../ba/1aed4e2ea2443d76cec241b96be4ec990852ec | Bin 0 -> 117 bytes .../user2/repo2.git/refs/heads/master | 2 +- routers/common/repo.go | 20 ++++++++----- 6 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 integrations/gitea-repositories-meta/user2/repo2.git/objects/10/32bbf17fbc0d9c95bb5418dabe8f8c99278700 create mode 100644 integrations/gitea-repositories-meta/user2/repo2.git/objects/26/f842bcad37fa40a1bb34cbb5ee219ee35d863d create mode 100644 integrations/gitea-repositories-meta/user2/repo2.git/objects/ba/1aed4e2ea2443d76cec241b96be4ec990852ec diff --git a/integrations/download_test.go b/integrations/download_test.go index 305155e9a..38de75f47 100644 --- a/integrations/download_test.go +++ b/integrations/download_test.go @@ -8,6 +8,7 @@ import ( "net/http" "testing" + "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" ) @@ -62,3 +63,30 @@ func TestDownloadByIDMediaForSVGUsesSecureHeaders(t *testing.T) { assert.Equal(t, "image/svg+xml", resp.HeaderMap.Get("Content-Type")) assert.Equal(t, "nosniff", resp.HeaderMap.Get("X-Content-Type-Options")) } + +func TestDownloadRawTextFileWithoutMimeTypeMapping(t *testing.T) { + defer prepareTestEnv(t)() + + session := loginUser(t, "user2") + + req := NewRequest(t, "GET", "/user2/repo2/raw/branch/master/test.xml") + resp := session.MakeRequest(t, req, http.StatusOK) + + assert.Equal(t, "text/plain; charset=utf-8", resp.HeaderMap.Get("Content-Type")) +} + +func TestDownloadRawTextFileWithMimeTypeMapping(t *testing.T) { + defer prepareTestEnv(t)() + setting.MimeTypeMap.Map[".xml"] = "text/xml" + setting.MimeTypeMap.Enabled = true + + session := loginUser(t, "user2") + + req := NewRequest(t, "GET", "/user2/repo2/raw/branch/master/test.xml") + resp := session.MakeRequest(t, req, http.StatusOK) + + assert.Equal(t, "text/xml; charset=utf-8", resp.HeaderMap.Get("Content-Type")) + + delete(setting.MimeTypeMap.Map, ".xml") + setting.MimeTypeMap.Enabled = false +} diff --git a/integrations/gitea-repositories-meta/user2/repo2.git/objects/10/32bbf17fbc0d9c95bb5418dabe8f8c99278700 b/integrations/gitea-repositories-meta/user2/repo2.git/objects/10/32bbf17fbc0d9c95bb5418dabe8f8c99278700 new file mode 100644 index 000000000..736e40878 --- /dev/null +++ b/integrations/gitea-repositories-meta/user2/repo2.git/objects/10/32bbf17fbc0d9c95bb5418dabe8f8c99278700 @@ -0,0 +1,2 @@ +xK +0Eg %":u􊕦J|p˭Q~% 9لG6G ͦw(E4}*{)`YƆleMJOܚ>%^ݿL!]N[v#E6U~/0 ZU'gpJ5 \ No newline at end of file diff --git a/integrations/gitea-repositories-meta/user2/repo2.git/objects/26/f842bcad37fa40a1bb34cbb5ee219ee35d863d b/integrations/gitea-repositories-meta/user2/repo2.git/objects/26/f842bcad37fa40a1bb34cbb5ee219ee35d863d new file mode 100644 index 0000000000000000000000000000000000000000..c3e7e778c5bc1e5a44f0ff429b4aa801c0938199 GIT binary patch literal 75 zcmV-R0JQ&j0ZYosPf{>5VX(2U$jwnGOD!tS%+Iq`GSo9rQb^59&QHnAOSe@D4RO=8 hP_nnq6l9f8-1_{7XU4N7C{`8AG`nn literal 0 HcmV?d00001 diff --git a/integrations/gitea-repositories-meta/user2/repo2.git/objects/ba/1aed4e2ea2443d76cec241b96be4ec990852ec b/integrations/gitea-repositories-meta/user2/repo2.git/objects/ba/1aed4e2ea2443d76cec241b96be4ec990852ec new file mode 100644 index 0000000000000000000000000000000000000000..add9a3af0d4c37c916e85c963b20a314b8fe3294 GIT binary patch literal 117 zcmV-*0E+*30V^p=O;s>7FlR6{FfcPQQSivmP1VayVR+T_r@efUH~XP%EAKClN_3cu z?N&pTJ^uzGbB&l)+hgNx13LEwg3F=Md?`@Hr!A(C8@