@@ -4,11 +4,11 @@ import (
4
4
"bytes"
5
5
"fmt"
6
6
"io"
7
- "io/ioutil"
8
7
"mime/multipart"
9
8
"net/http"
10
9
"net/url"
11
10
"os"
11
+ "path/filepath"
12
12
"strings"
13
13
)
14
14
@@ -32,10 +32,9 @@ func buildRequest(input metaRequest) (req request, err error) {
32
32
return
33
33
}
34
34
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 )
39
38
return
40
39
}
41
40
r , err = http .NewRequest (input .method , url , body )
@@ -46,14 +45,18 @@ func buildRequest(input metaRequest) (req request, err error) {
46
45
for header , value := range input .headers {
47
46
r .Header .Set (header , value )
48
47
}
48
+ for header , value := range bodyHeaders {
49
+ r .Header .Set (header , value )
50
+ }
49
51
50
52
}
51
53
req = request {
52
54
label : input .label ,
53
55
skip : input .skip ,
54
56
delay : input .delay ,
55
57
expectation : input .expectation ,
56
- r : r ,
58
+
59
+ r : r ,
57
60
}
58
61
59
62
if ! req .skip {
@@ -66,43 +69,38 @@ func buildRequest(input metaRequest) (req request, err error) {
66
69
return
67
70
}
68
71
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 ()
84
78
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 )
92
86
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 )
102
101
}
103
102
104
- // Assume json
105
- // if input.body != "" && input.headers["Content-Type"] == "application/json" {
103
+ // unknown body
106
104
if input .body != "" {
107
105
body = strings .NewReader (input .body )
108
106
return
@@ -118,11 +116,11 @@ func isValidMetaRequest(req metaRequest) error {
118
116
if req .method == "" {
119
117
return fmt .Errorf ("No method found in request" )
120
118
}
121
- if req .filename != "" && req .headers ["Content-Type" ] == "" {
119
+ if req .filepath != "" && req .headers ["Content-Type" ] == "" {
122
120
return fmt .Errorf ("Content-Type not set for request with file" )
123
121
}
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 )
126
124
}
127
125
return nil
128
126
}
0 commit comments