Skip to content

Commit

Permalink
Allow passing ArrayBuffer to http.file()
Browse files Browse the repository at this point in the history
Part of #1020
  • Loading branch information
Ivan Mirić committed Jan 29, 2021
1 parent b147269 commit bfdf164
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
12 changes: 10 additions & 2 deletions js/modules/k6/http/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
package http

import (
"context"
"fmt"
"strings"
"time"

"github.com/loadimpact/k6/js/common"
)

// FileData represents a binary file requiring multipart request encoding
Expand All @@ -40,7 +43,7 @@ func escapeQuotes(s string) string {
}

// File returns a FileData parameter
func (h *HTTP) File(data []byte, args ...string) FileData {
func (h *HTTP) File(ctx context.Context, data interface{}, args ...string) FileData {
// supply valid default if filename and content-type are not specified
fname, ct := fmt.Sprintf("%d", time.Now().UnixNano()), "application/octet-stream"

Expand All @@ -52,8 +55,13 @@ func (h *HTTP) File(data []byte, args ...string) FileData {
}
}

dt, err := common.ToBytes(data)
if err != nil {
common.Throw(common.GetRuntime(ctx), err)
}

return FileData{
Data: data,
Data: dt,
Filename: fname,
ContentType: ct,
}
Expand Down
77 changes: 77 additions & 0 deletions js/modules/k6/http/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2021 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package http

import (
"context"
"fmt"
"testing"

"github.com/dop251/goja"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/loadimpact/k6/js/common"
)

func TestHTTPFile(t *testing.T) {
t.Parallel()
rt := goja.New()
input := []byte{104, 101, 108, 108, 111}

testCases := []struct {
input interface{}
args []string
expected FileData
expErr string
}{
// We can't really test without specifying a filename argument,
// as File() calls time.Now(), so we'd need some time freezing/mocking
// or refactoring, or to exclude the field from the assertion.
{input, []string{"test.bin"},
FileData{Data: input, Filename: "test.bin", ContentType: "application/octet-stream"}, ""},
{string(input), []string{"test.txt", "text/plain"},
FileData{Data: input, Filename: "test.txt", ContentType: "text/plain"}, ""},
{rt.NewArrayBuffer(input), []string{"test-ab.bin"},
FileData{Data: input, Filename: "test-ab.bin", ContentType: "application/octet-stream"}, ""},
{struct{}{}, []string{}, FileData{}, "invalid type struct {}, expected string, []byte or ArrayBuffer"},
}

for _, tc := range testCases {
tc := tc
t.Run(fmt.Sprintf("%T", tc.input), func(t *testing.T) {
if tc.expErr != "" {
defer func() {
err := recover()
require.NotNil(t, err)
require.IsType(t, &goja.Object{}, err)
require.IsType(t, map[string]interface{}{}, err.(*goja.Object).Export())
val := err.(*goja.Object).Export().(map[string]interface{})
assert.Equal(t, tc.expErr, fmt.Sprintf("%s", val["value"]))
}()
}
h := New()
ctx := common.WithRuntime(context.Background(), rt)
out := h.File(ctx, tc.input, tc.args...)
assert.Equal(t, tc.expected, out)
})
}
}

0 comments on commit bfdf164

Please sign in to comment.