Skip to content

Commit

Permalink
fix(#22): adds support for multipart multiple form values. This intro…
Browse files Browse the repository at this point in the history
…duces a minor interface breaking change
  • Loading branch information
h2non committed Mar 16, 2017
1 parent cd30ed4 commit b8321b8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
16 changes: 11 additions & 5 deletions plugins/multipart/multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ package multipart
import (
"bytes"
"errors"
c "gopkg.in/h2non/gentleman.v1/context"
p "gopkg.in/h2non/gentleman.v1/plugin"
"io"
"io/ioutil"
"mime/multipart"
"strconv"
"strings"

c "gopkg.in/h2non/gentleman.v1/context"
p "gopkg.in/h2non/gentleman.v1/plugin"
)

// Values represents multiple multipart from values.
type Values []string

// DataFields represents a map of text based fields.
type DataFields map[string]string
type DataFields map[string]Values

// FormFile represents the file form field data.
type FormFile struct {
Expand Down Expand Up @@ -80,8 +84,10 @@ func createForm(data FormData, ctx *c.Context) error {
}

// Populate the other parts of the form (if there are any)
for key, value := range data.Data {
multipartWriter.WriteField(key, value)
for key, values := range data.Data {
for _, value := range values {
multipartWriter.WriteField(key, value)
}
}
if err := multipartWriter.Close(); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions plugins/multipart/multipart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestFiles(t *testing.T) {
func TestFields(t *testing.T) {
ctx := context.New()
fn := newHandler()
fields := map[string]string{"foo": "data=bar", "bar": "data=baz"}
fields := map[string]Values{"foo": Values{"data=bar"}, "bar": Values{"data=baz"}}

Fields(fields).Exec("request", ctx, fn.fn)
st.Expect(t, fn.called, true)
Expand All @@ -61,7 +61,7 @@ func TestData(t *testing.T) {
ctx := context.New()
fn := newHandler()
reader := bytes.NewReader([]byte("hello world"))
fields := map[string]string{"foo": "data=bar", "bar": "data=baz"}
fields := map[string]Values{"foo": Values{"data=bar"}, "bar": Values{"data=baz"}}
data := FormData{
Files: []FormFile{{Name: "foo", Reader: reader}},
Data: fields,
Expand Down
5 changes: 4 additions & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,10 @@ func TestRequestXML(t *testing.T) {

func TestRequestForm(t *testing.T) {
reader := bytes.NewReader([]byte("hello world"))
fields := map[string]string{"foo": "data=bar", "bar": "data=baz"}
fields := map[string]multipart.Values{
"foo": multipart.Values{"data=bar"},
"bar": multipart.Values{"data=baz"},
}
data := multipart.FormData{
Files: []multipart.FormFile{{Name: "foo", Reader: reader}},
Data: fields,
Expand Down

0 comments on commit b8321b8

Please sign in to comment.