Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the right content type for files in multipart requests #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package httptest

import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/textproto"
"strings"
)

type File struct {
Expand All @@ -29,19 +32,35 @@ func (r *Request) MultiPartPut(body interface{}, files ...File) (*Response, erro
return r.Perform(req), nil
}

var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")

func escapeQuotes(s string) string {
return quoteEscaper.Replace(s)
}

// this helper method was inspired by this blog post by Matt Aimonetti:
// https://matt.aimonetti.net/posts/2013/07/01/golang-multipart-file-upload-example/
func newMultipart(url string, method string, body interface{}, files ...File) (*http.Request, error) {

bb := &bytes.Buffer{}
writer := multipart.NewWriter(bb)
defer writer.Close()
for _, f := range files {
part, err := writer.CreateFormFile(f.ParamName, f.FileName)
fBuffer, err := io.ReadAll(f)
if err != nil {
return nil, err
}

h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
escapeQuotes(f.ParamName), escapeQuotes(f.FileName)))
h.Set("Content-Type", http.DetectContentType(fBuffer))
part, err := writer.CreatePart(h)
if err != nil {
return nil, err
}
_, err = io.Copy(part, f)
fReader := bytes.NewReader(fBuffer)
_, err = io.Copy(part, fReader)
if err != nil {
return nil, err
}
Expand Down
36 changes: 22 additions & 14 deletions file_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package httptest

import (
"os"
"fmt"
"github.com/gobuffalo/httptest/testassets"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -11,20 +12,27 @@ func Test_FileUpload(t *testing.T) {
r := require.New(t)
w := New(App())

f := struct {
Name string
}{"Foo"}
foo := func(filename, expectedType string) {
f := struct {
Name string
}{"Foo"}

rr, err := os.Open("./file_test.go")
r.NoError(err)
wf := File{
ParamName: "MyFile",
FileName: "foo.go",
Reader: rr,
rr, err := testassets.FS.Open(filename)
r.NoError(err)
wf := File{
ParamName: "MyFile",
FileName: filename,
Reader: rr,
}
res, err := w.HTML("/up").MultiPartPost(f, wf)
r.NoError(err)
r.Equal(200, res.Code)
r.Equal(fmt.Sprintf("Foo\n%s\n%s\n", filename, expectedType), res.Body.String())
}

res, err := w.HTML("/up").MultiPartPost(f, wf)
r.NoError(err)
r.Equal(200, res.Code)
r.Equal("Foo\nfoo.go\n", res.Body.String())
foo("test.jpg", "image/jpeg")
foo("test.png", "image/png")
foo("test.pdf", "application/pdf")
foo("embed.go", "text/plain; charset=utf-8")
foo("random.bin", "application/octet-stream")
}
1 change: 1 addition & 0 deletions httptest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func App() http.Handler {
}
fmt.Fprintln(res, req.FormValue("Name"))
fmt.Fprintln(res, h.Filename)
fmt.Fprintln(res, h.Header.Get("Content-Type"))
})
return p
}
Expand Down
8 changes: 8 additions & 0 deletions testassets/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package testassets

import (
"embed"
)

//go:embed *
var FS embed.FS
1 change: 1 addition & 0 deletions testassets/random.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�>#�n���-p8
Expand Down
Binary file added testassets/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testassets/test.pdf
Binary file not shown.
Binary file added testassets/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.