Skip to content

Commit 32859c2

Browse files
committed
Fix file upload, needs second pass to make it pretty
1 parent b55bd38 commit 32859c2

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

builder.go

+40-42
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"mime/multipart"
98
"net/http"
109
"net/url"
1110
"os"
11+
"path/filepath"
1212
"strings"
1313
)
1414

@@ -32,10 +32,9 @@ func buildRequest(input metaRequest) (req request, err error) {
3232
return
3333
}
3434

35-
var body io.Reader
36-
body, err = buildBody(input)
37-
if err != nil {
38-
err = fmt.Errorf("creating body %w", err)
35+
body, bodyHeaders, bodyerr := buildBody(input)
36+
if bodyerr != nil {
37+
err = fmt.Errorf("creating body %w", bodyerr)
3938
return
4039
}
4140
r, err = http.NewRequest(input.method, url, body)
@@ -46,14 +45,18 @@ func buildRequest(input metaRequest) (req request, err error) {
4645
for header, value := range input.headers {
4746
r.Header.Set(header, value)
4847
}
48+
for header, value := range bodyHeaders {
49+
r.Header.Set(header, value)
50+
}
4951

5052
}
5153
req = request{
5254
label: input.label,
5355
skip: input.skip,
5456
delay: input.delay,
5557
expectation: input.expectation,
56-
r: r,
58+
59+
r: r,
5760
}
5861

5962
if !req.skip {
@@ -66,43 +69,38 @@ func buildRequest(input metaRequest) (req request, err error) {
6669
return
6770
}
6871

69-
func buildBody(input metaRequest) (body io.Reader, err error) {
70-
if input.filename != "" {
71-
file, err := os.Open(input.filename)
72-
if err != nil {
73-
return nil, err
74-
}
75-
fileContents, err := ioutil.ReadAll(file)
76-
if err != nil {
77-
return nil, err
78-
}
79-
fi, err := file.Stat()
80-
if err != nil {
81-
return nil, err
82-
}
83-
file.Close()
72+
func buildFileBody(input metaRequest) (body *bytes.Buffer, headers map[string]string, err error) {
73+
file, err := os.Open(input.filepath)
74+
if err != nil {
75+
return
76+
}
77+
defer file.Close()
8478

85-
var b bytes.Buffer
86-
writer := multipart.NewWriter(&b)
87-
part, err := writer.CreateFormFile(input.filelabel, fi.Name())
88-
if err != nil {
89-
return nil, err
90-
}
91-
part.Write(fileContents)
79+
body = &bytes.Buffer{}
80+
writer := multipart.NewWriter(body)
81+
part, err := writer.CreateFormFile(input.filelabel, filepath.Base(input.filepath))
82+
if err != nil {
83+
return
84+
}
85+
_, err = io.Copy(part, file)
9286

93-
// TODO Add fields somehow
94-
/* for key, val := range params {
95-
_ = writer.WriteField(key, val)
96-
} */
97-
err = writer.Close()
98-
if err != nil {
99-
return nil, err
100-
}
101-
body = &b
87+
/* for key, val := range params {
88+
_ = writer.WriteField(key, val)
89+
} */
90+
err = writer.Close()
91+
if err != nil {
92+
return
93+
}
94+
95+
return body, map[string]string{"Content-Type": writer.FormDataContentType()}, err
96+
}
97+
98+
func buildBody(input metaRequest) (body io.Reader, headers map[string]string, err error) {
99+
if input.filepath != "" {
100+
return buildFileBody(input)
102101
}
103102

104-
// Assume json
105-
// if input.body != "" && input.headers["Content-Type"] == "application/json" {
103+
// unknown body
106104
if input.body != "" {
107105
body = strings.NewReader(input.body)
108106
return
@@ -118,11 +116,11 @@ func isValidMetaRequest(req metaRequest) error {
118116
if req.method == "" {
119117
return fmt.Errorf("No method found in request")
120118
}
121-
if req.filename != "" && req.headers["Content-Type"] == "" {
119+
if req.filepath != "" && req.headers["Content-Type"] == "" {
122120
return fmt.Errorf("Content-Type not set for request with file")
123121
}
124-
if req.filename != "" && req.filelabel == "" {
125-
return fmt.Errorf("file %s not labeled in request (ex file://path label)", req.filename)
122+
if req.filepath != "" && req.filelabel == "" {
123+
return fmt.Errorf("file %s not labeled in request (ex file://path label)", req.filepath)
126124
}
127125
return nil
128126
}

lex.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type metaRequest struct {
3232
method string
3333
path string
3434
body string
35-
filename string
35+
filepath string
3636
filelabel string
3737
delay time.Duration
3838
expectation expectation
@@ -214,9 +214,9 @@ func (l *lexer) parseBlock(block []string) (request, error) {
214214
// fn := l.rxFile.FindString(line)
215215
matches := l.rxFile.FindStringSubmatch(line)
216216
if isValidFile(matches[1]) {
217-
req.filename = matches[1]
217+
req.filepath = matches[1]
218218
req.filelabel = matches[2]
219-
log.Debug("Got File", req.filename, req.filelabel)
219+
log.Debug("Got File", req.filepath, req.filelabel)
220220
}
221221
state = stateHeaders
222222
case l.rxLabel.MatchString(line):

0 commit comments

Comments
 (0)