package contentprocessor
import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/headers"
"gitlab.com/gitlab-org/gitlab/workhorse/internal/testhelper"
"github.com/stretchr/testify/require"
)
func TestFailSetContentTypeAndDisposition(t *testing.T) {
testCaseBody := "Hello world!"
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, err := io.WriteString(w, testCaseBody)
require.NoError(t, err)
})
resp := makeRequest(t, h, testCaseBody, "")
require.Equal(t, "", resp.Header.Get(headers.ContentDispositionHeader))
require.Equal(t, "", resp.Header.Get(headers.ContentTypeHeader))
}
func TestSuccessSetContentTypeAndDispositionFeatureEnabled(t *testing.T) {
testCaseBody := "Hello world!"
resp := makeRequest(t, nil, testCaseBody, "")
require.Equal(t, "inline", resp.Header.Get(headers.ContentDispositionHeader))
require.Equal(t, "text/plain; charset=utf-8", resp.Header.Get(headers.ContentTypeHeader))
}
func TestSetProperContentTypeAndDisposition(t *testing.T) {
testCases := []struct {
desc string
contentType string
contentDisposition string
body string
}{
{
desc: "text type",
contentType: "text/plain; charset=utf-8",
contentDisposition: "inline",
body: "Hello world!",
},
{
desc: "HTML type",
contentType: "text/plain; charset=utf-8",
contentDisposition: "inline",
body: "
Hello world!",
},
{
desc: "Javascript type",
contentType: "text/plain; charset=utf-8",
contentDisposition: "inline",
body: "",
},
{
desc: "Image type",
contentType: "image/png",
contentDisposition: "inline",
body: testhelper.LoadFile(t, "testdata/image.png"),
},
{
desc: "SVG type",
contentType: "image/svg+xml",
contentDisposition: "attachment",
body: testhelper.LoadFile(t, "testdata/image.svg"),
},
{
desc: "Partial SVG type",
contentType: "image/svg+xml",
contentDisposition: "attachment",
body: "