summaryrefslogtreecommitdiff
path: root/workhorse/internal/headers/content_headers_test.go
blob: 7cfce335d88188df72cad83cf1be85f085e4e85a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package headers

import (
	"os"
	"testing"

	"github.com/stretchr/testify/require"
)

func fileContents(fileName string) []byte {
	fileContents, _ := os.ReadFile(fileName)
	return fileContents
}

func TestHeaders(t *testing.T) {
	tests := []struct {
		desc                       string
		fileContents               []byte
		expectedContentType        string
		expectedContentDisposition string
	}{
		{
			desc:                       "XML file",
			fileContents:               fileContents("../../testdata/test.xml"),
			expectedContentType:        "text/plain; charset=utf-8",
			expectedContentDisposition: "inline; filename=blob",
		},
		{
			desc:                       "XHTML file",
			fileContents:               fileContents("../../testdata/index.xhtml"),
			expectedContentType:        "text/plain; charset=utf-8",
			expectedContentDisposition: "inline; filename=blob",
		},
		{
			desc:                       "svg+xml file",
			fileContents:               fileContents("../../testdata/xml.svg"),
			expectedContentType:        "image/svg+xml",
			expectedContentDisposition: "attachment",
		},
		{
			desc:                       "text file",
			fileContents:               []byte(`a text file`),
			expectedContentType:        "text/plain; charset=utf-8",
			expectedContentDisposition: "inline",
		},
	}

	for _, test := range tests {
		t.Run(test.desc, func(t *testing.T) {
			contentType, newContentDisposition := SafeContentHeaders(test.fileContents, "")

			require.Equal(t, test.expectedContentType, contentType)
			require.Equal(t, test.expectedContentDisposition, newContentDisposition)
		})
	}
}