Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand ArrayBuffer support #1800

Merged
merged 5 commits into from
Feb 2, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow passing ArrayBuffer to http.file()
Part of #1020
  • Loading branch information
Ivan Mirić committed Jan 29, 2021
commit 0fdae7a08cf9b44bac3a40737b9b341ffab719fc
12 changes: 10 additions & 2 deletions js/modules/k6/http/file.go
Original file line number Diff line number Diff line change
@@ -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
@@ -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"

@@ -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,
}
89 changes: 89 additions & 0 deletions js/modules/k6/http/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
*
* 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)
})
}
}