Skip to content

Commit

Permalink
test content disposition filename encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
kstuart committed Jan 23, 2023
1 parent 7e45db7 commit 66d91bf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions files/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestMultipartFiles(t *testing.T) {
data := `
--Boundary!
Content-Type: text/plain
Content-Disposition: form-data; name="file-0?mode=0754&mtime=1604320500&mtime-nsecs=55555"; filename="file1"
Content-Disposition: form-data; name="file-0?mode=0754&mtime=1604320500&mtime-nsecs=55555"; filename="%C2%A3%E1%BA%9E%C7%91%C7%93%C3%86+%C3%A6+%E2%99%AB%E2%99%AC"
Some-Header: beep
beep
Expand Down Expand Up @@ -122,7 +122,7 @@ implicit file2
CheckDir(t, dir, []Event{
{
kind: TFile,
name: "file1",
name: "£ẞǑǓÆ æ ♫♬",
value: "beep",
mode: 0754,
mtime: time.Unix(1604320500, 55555),
Expand Down
4 changes: 2 additions & 2 deletions files/multifilereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {

// write the boundary and headers
header := make(textproto.MIMEHeader)
filename := url.QueryEscape(path.Join(path.Join(mfr.path...), entry.Name()))
filename := path.Join(path.Join(mfr.path...), entry.Name())
mfr.addContentDisposition(header, filename)

var contentType string
Expand Down Expand Up @@ -168,7 +168,7 @@ func (mfr *MultiFileReader) addContentDisposition(header textproto.MIMEHeader, f
sb.WriteString("attachment")
}

fmt.Fprintf(sb, "; filename=\"%s\"", filename)
fmt.Fprintf(sb, "; filename=\"%s\"", url.QueryEscape(filename))

header.Set(contentDispositionHeader, sb.String())
}
Expand Down
23 changes: 23 additions & 0 deletions files/multifilereader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package files
import (
"io"
"mime/multipart"
"net/textproto"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -213,3 +214,25 @@ func TestCommonPrefix(t *testing.T) {
},
})
}

func TestContentDispositonEncoding(t *testing.T) {
testContentDispositionEncoding(t, false, "£ẞǑǓÆ æ ♫♬",
"attachment; filename=\"%C2%A3%E1%BA%9E%C7%91%C7%93%C3%86+%C3%A6+%E2%99%AB%E2%99%AC\"")
testContentDispositionEncoding(t, true, "£ẞǑǓÆ æ ♫♬",
"form-data; name=\"file\"; filename=\"%C2%A3%E1%BA%9E%C7%91%C7%93%C3%86+%C3%A6+%E2%99%AB%E2%99%AC\"")
}

func testContentDispositionEncoding(t *testing.T, form bool, filename string, expected string) {
sf := NewMapDirectory(map[string]Node{"": NewBytesFile([]byte(""))})
mfr := NewMultiFileReader(sf, form)
if _, err := mfr.Read(nil); err != nil {
t.Fatal("MultiFileReader.Read failed")
}

header := make(textproto.MIMEHeader)
mfr.addContentDisposition(header, filename)
v := header.Get(contentDispositionHeader)
if v != expected {
t.Fatalf("expected content-disposition to be: %s", v)
}
}

0 comments on commit 66d91bf

Please sign in to comment.