Skip to content

Commit

Permalink
Merge pull request #617 from DaichiUeura/topic/octetstream-support
Browse files Browse the repository at this point in the history
Support application/octet-stream via http
  • Loading branch information
k1LoW authored Sep 23, 2023
2 parents d329402 + 19ec2e3 commit aef74fe
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 1 deletion.
29 changes: 28 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
MediaTypeTextPlain = "text/plain"
MediaTypeApplicationFormUrlencoded = "application/x-www-form-urlencoded"
MediaTypeMultipartFormData = "multipart/form-data"
MediaTypeApplicationOctetStream = "application/octet-stream"
)

const (
Expand Down Expand Up @@ -111,7 +112,7 @@ func (r *httpRequest) validate() error {
return nil
}
switch r.mediaType {
case MediaTypeApplicationJSON, MediaTypeTextPlain, MediaTypeApplicationFormUrlencoded, "":
case MediaTypeApplicationJSON, MediaTypeTextPlain, MediaTypeApplicationFormUrlencoded, MediaTypeApplicationOctetStream, "":
default:
return fmt.Errorf("unsupported mediaType: %s", r.mediaType)
}
Expand Down Expand Up @@ -142,6 +143,32 @@ func (r *httpRequest) encodeBody() (io.Reader, error) {
return nil, err
}
return buf, nil
case MediaTypeApplicationOctetStream:
switch v := r.body.(type) {
case map[string]any:
vv, ok := v["filename"]
if !ok {
return nil, fmt.Errorf("invalid body: %v", r.body)
}
fileName, ok := vv.(string)
if !ok {
return nil, fmt.Errorf("invalid body: %v", r.body)
}
b, err := readFile(filepath.Join(r.root, fileName))
if err != nil {
return nil, err
}
return bytes.NewBuffer(b), nil
case string:
s, ok := r.body.(string)
if !ok {
return nil, fmt.Errorf("invalid body: %v", r.body)
}
return strings.NewReader(s), nil
case []byte:
return bytes.NewBuffer(r.body.([]byte)), nil
}
return nil, fmt.Errorf("invalid body: %v", r.body)
case MediaTypeTextPlain:
s, ok := r.body.(string)
if !ok {
Expand Down
17 changes: 17 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ func TestHTTPRunnerRunUsingGitHubAPI(t *testing.T) {
}

func TestRequestBody(t *testing.T) {
dummy, err := os.ReadFile("testdata/dummy.png")
if err != nil {
t.Fatal(err)
}

tests := []struct {
in string
mediaType string
Expand Down Expand Up @@ -133,6 +138,18 @@ two: ni`,
MediaTypeApplicationFormUrlencoded,
`one=ichi&two=ni`,
},
{
`
filename: testdata/dummy.png`,
MediaTypeApplicationOctetStream,
string(dummy),
},
{
`
!!binary QUJD`,
MediaTypeApplicationOctetStream,
`ABC`,
},
}

for _, tt := range tests {
Expand Down
10 changes: 10 additions & 0 deletions testdata/book/http.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ steps:
upload0: ../dummy.png
test: |
current.res.status == 201
dataupload:
desc: Post /upload with octet-stream
req:
/upload:
post:
body:
application/octet-stream:
filename: ../dummy.jpg
test: |
current.res.status == 201
severalvalues:
desc: Get several values
req:
Expand Down
Binary file modified testdata/http.yml.debugger.golden
Binary file not shown.
28 changes: 28 additions & 0 deletions testdata/http.yml.runbook.golden
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,34 @@ steps:
&& current.res.headers["Content-Type"][0] == "text/html; charset=utf-8"
&& "Date" in current.res.headers
&& current.res.rawBody == "<h1>Posted</h1>"
- req:
/upload:
post:
body:
application/octet-stream: !!binary |
/9j/4AAQSkZJRgABAQEAYABgAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZy
BJSkcgSlBFRyB2ODApLCBkZWZhdWx0IHF1YWxpdHkK/9sAQwAIBgYHBgUIBwcHCQkICgwU
DQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQw
EJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy
MjIyMjIyMjIyMjIy/8AAEQgAKAA8AwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAA
ABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQy
gZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWV
pjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrC
w8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQ
EAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEH
YXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSl
NUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKz
tLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQ
MRAD8A+f6KKKACiiigAooooAKKKKACiiigDp5UtWgf7ELASJKiQGeSAh4cHLEEDB/1ed2W
5bGMNWWkOmyG8YyFArsbVS2PMHOA3HHY579O+5cyigDes005dOQedaST+YrASAht5R8Akg
DYGCZ5x1J4PGbqQUTRf6nzvLHneTt2bsnGNvy/d25x3zVOigAooooAKKKKACiiigAooooA
KKKKAP/Z
test: |
current.res.status == 201
&& current.res.headers["Content-Length"][0] == "15"
&& current.res.headers["Content-Type"][0] == "text/html; charset=utf-8"
&& "Date" in current.res.headers
&& current.res.rawBody == "<h1>Posted</h1>"
- req:
/ping:
get:
Expand Down
4 changes: 4 additions & 0 deletions testdata/openapi3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ paths:
post:
requestBody:
content:
application/octet-stream:
schema:
type: string
format: binary
multipart/form-data:
schema:
type: object
Expand Down
1 change: 1 addition & 0 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func CreateHTTPStepMapSlice(key string, req *http.Request) (yaml.MapSlice, error
}
default:
// case contentType == runn.MediaTypeTextPlain:
// case contentType == runn.MediaTypeApplicationOctetStream
b, err := io.ReadAll(save)
if err != nil {
return nil, fmt.Errorf("failed to io.ReadAll: %w", err)
Expand Down

0 comments on commit aef74fe

Please sign in to comment.