2023-12-12 11:15:50 +01:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2024-08-16 12:37:59 +02:00
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2023-12-12 11:15:50 +01:00
|
|
|
"testing"
|
|
|
|
|
2024-08-16 12:37:59 +02:00
|
|
|
"github.com/matrix-org/dendrite/mediaapi/types"
|
2023-12-12 11:15:50 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_dispositionFor(t *testing.T) {
|
|
|
|
assert.Equal(t, "attachment", contentDispositionFor(""), "empty content type")
|
|
|
|
assert.Equal(t, "attachment", contentDispositionFor("image/svg"), "image/svg")
|
|
|
|
assert.Equal(t, "inline", contentDispositionFor("image/jpeg"), "image/jpg")
|
|
|
|
}
|
2024-08-16 12:37:59 +02:00
|
|
|
|
|
|
|
func Test_Multipart(t *testing.T) {
|
|
|
|
r := &downloadRequest{
|
|
|
|
MediaMetadata: &types.MediaMetadata{},
|
|
|
|
}
|
|
|
|
data := bytes.Buffer{}
|
|
|
|
responseBody := "This media is plain text. Maybe somebody used it as a paste bin."
|
|
|
|
data.WriteString(responseBody)
|
|
|
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
_, err := multipartResponse(w, r, "text/plain", &data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
resp, err := srv.Client().Get(srv.URL)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
// contentLength is always 0, since there's no Content-Length header on the multipart part.
|
2024-09-13 09:39:30 +02:00
|
|
|
_, reader, err := parseMultipartResponse(r, resp, 1000)
|
2024-08-16 12:37:59 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
gotResponse, err := io.ReadAll(reader)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, responseBody, string(gotResponse))
|
|
|
|
}
|