summaryrefslogtreecommitdiff
path: root/workhorse/internal/headers/content_headers_test.go
blob: bd329dbc199c1765120621572a069e4b21a82929 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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) {
	xmlFileContents := fileContents("../../testdata/test.xml")
	svgFileContents := fileContents("../../testdata/xml.svg")
	xhtmlFileContents := fileContents("../../testdata/index.xhtml")

	tests := []struct {
		desc                       string
		fileContents               []byte
		contentDisposition         string
		expectedContentType        string
		expectedContentDisposition string
	}{
		{
			desc:                       "inline XML file",
			fileContents:               xmlFileContents,
			contentDisposition:         "inline; filename=test.xml",
			expectedContentType:        "text/plain; charset=utf-8",
			expectedContentDisposition: "inline; filename=blob",
		},
		{
			desc:                       "attachment XML file",
			fileContents:               xmlFileContents,
			contentDisposition:         "attachment; filename=test.xml",
			expectedContentType:        "application/octet-stream",
			expectedContentDisposition: "attachment; filename=test.xml",
		},
		{
			desc:                       "inline XHTML file",
			fileContents:               xhtmlFileContents,
			contentDisposition:         "inline; filename=index.xhtml",
			expectedContentType:        "text/plain; charset=utf-8",
			expectedContentDisposition: "inline; filename=blob",
		},
		{
			desc:                       "attachment XHTML file",
			fileContents:               xhtmlFileContents,
			contentDisposition:         "attachment; filename=index.xhtml",
			expectedContentType:        "application/octet-stream",
			expectedContentDisposition: "attachment; filename=index.xhtml",
		},
		{
			desc:                       "svg+xml file",
			fileContents:               svgFileContents,
			contentDisposition:         "",
			expectedContentType:        "image/svg+xml",
			expectedContentDisposition: "attachment",
		},
		{
			desc:                       "svg+xml file",
			fileContents:               svgFileContents,
			contentDisposition:         "inline; filename=xml.svg",
			expectedContentType:        "image/svg+xml",
			expectedContentDisposition: "attachment; filename=xml.svg",
		},
		{
			desc:                       "text file",
			fileContents:               []byte(`a text file`),
			contentDisposition:         "",
			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, test.contentDisposition)

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