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

Update Sobek and fix http.File to not use []byte #4009

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/pprof v0.0.0-20230728192033-2ba5b33183c6 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grafana/sobek v0.0.0-20240927094302-19dd311f018f
github.com/grafana/sobek v0.0.0-20241023145759-2dc9daf5bfa2
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grafana/sobek v0.0.0-20240927094302-19dd311f018f h1:9r05Uxs+Pq1UqZqGG/PoCUWd6xk2ab+mRWAM5xZgM9I=
github.com/grafana/sobek v0.0.0-20240927094302-19dd311f018f/go.mod h1:FmcutBFPLiGgroH42I4/HBahv7GxVjODcVWFTw1ISes=
github.com/grafana/sobek v0.0.0-20241023145759-2dc9daf5bfa2 h1:qgthy9RbAIxinOmXaB9nSaT/w00VTqeEQ7JI0a+ScUU=
github.com/grafana/sobek v0.0.0-20241023145759-2dc9daf5bfa2/go.mod h1:FmcutBFPLiGgroH42I4/HBahv7GxVjODcVWFTw1ISes=
github.com/grafana/xk6-browser v1.8.5 h1:dNAG8dhcaEx/HOELEnGzAw8ShCvkpukfyTGUhebZsj0=
github.com/grafana/xk6-browser v1.8.5/go.mod h1:yCtZ4G8U/imVBikBO4HJoMyNoejmECcJk4CK5XGSxis=
github.com/grafana/xk6-dashboard v0.7.5 h1:TcILyffT/Ea/XD7xG1jMA5lwtusOPRbEQsQDHmO30Mk=
Expand Down
19 changes: 10 additions & 9 deletions js/modules/k6/http/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"strings"
"time"

"go.k6.io/k6/js/common"
"github.com/grafana/sobek"
)

// FileData represents a binary file requiring multipart request encoding
type FileData struct {
Data []byte
Data any
Filename string
ContentType string
}
Expand All @@ -22,7 +22,7 @@ func escapeQuotes(s string) string {
}

// File returns a FileData object.
func (mi *ModuleInstance) file(data interface{}, args ...string) FileData {
func (mi *ModuleInstance) file(data any, args ...string) (*FileData, error) {
// supply valid default if filename and content-type are not specified
fname, ct := fmt.Sprintf("%d", time.Now().UnixNano()), "application/octet-stream"

Expand All @@ -34,14 +34,15 @@ func (mi *ModuleInstance) file(data interface{}, args ...string) FileData {
}
}

dt, err := common.ToBytes(data)
if err != nil {
common.Throw(mi.vu.Runtime(), err)
switch data.(type) {
case string, sobek.ArrayBuffer:
default:
return nil, fmt.Errorf("invalid type %T, expected string or ArrayBuffer", data)
}

return FileData{
Data: dt,
return &FileData{
Data: data,
Filename: fname,
ContentType: ct,
}
}, nil
}
16 changes: 8 additions & 8 deletions js/modules/k6/http/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

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

testCases := []struct {
input func(rt *sobek.Runtime) interface{}
args []string
expected FileData
expected *FileData
expErr string
}{
// We can't really test without specifying a filename argument,
Expand All @@ -24,22 +24,22 @@ func TestHTTPFile(t *testing.T) {
{
func(*sobek.Runtime) interface{} { return input },
[]string{"test.bin"},
FileData{Data: input, Filename: "test.bin", ContentType: "application/octet-stream"},
&FileData{Data: input, Filename: "test.bin", ContentType: "application/octet-stream"},
"",
},
{
func(*sobek.Runtime) interface{} { return string(input) },
func(*sobek.Runtime) interface{} { return input },
[]string{"test.txt", "text/plain"},
FileData{Data: input, Filename: "test.txt", ContentType: "text/plain"},
&FileData{Data: input, Filename: "test.txt", ContentType: "text/plain"},
"",
},
{
func(rt *sobek.Runtime) interface{} { return rt.NewArrayBuffer(input) },
func(rt *sobek.Runtime) interface{} { return rt.NewArrayBuffer([]byte(input)) },
[]string{"test-ab.bin"},
FileData{Data: input, Filename: "test-ab.bin", ContentType: "application/octet-stream"},
&FileData{Data: input, Filename: "test-ab.bin", ContentType: "application/octet-stream"},
"",
},
{func(*sobek.Runtime) interface{} { return struct{}{} }, []string{}, FileData{}, "GoError: invalid type struct {}, expected string, []byte or ArrayBuffer"},
{func(*sobek.Runtime) interface{} { return struct{}{} }, []string{}, &FileData{}, "GoError: invalid type struct {}, expected string or ArrayBuffer"},
}

for i, tc := range testCases {
Expand Down
10 changes: 7 additions & 3 deletions js/modules/k6/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (c *Client) parseRequest(
// Otherwise parameters are treated as standard form field.
for k, v := range data {
switch ve := v.(type) {
case FileData:
case *FileData:
// writing our own part to handle receiving
// different content-type than the default application/octet-stream
h := make(textproto.MIMEHeader)
Expand All @@ -228,7 +228,11 @@ func (c *Client) parseRequest(
return err
}

if _, err := fw.Write(ve.Data); err != nil {
data, err := common.ToBytes(ve.Data)
if err != nil {
return err
}
if _, err := fw.Write(data); err != nil {
return err
}
default:
Expand Down Expand Up @@ -575,7 +579,7 @@ func (c *Client) parseBatchRequest(key interface{}, val interface{}) (*httpext.P

func requestContainsFile(data map[string]interface{}) bool {
for _, v := range data {
if _, ok := v.(FileData); ok {
if _, ok := v.(*FileData); ok {
return true
}
}
Expand Down
3 changes: 3 additions & 0 deletions vendor/github.com/grafana/sobek/modules_sourcetext.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/grafana/sobek/object_goreflect.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/grafana/sobek/parser/expression.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ github.com/google/uuid
# github.com/gorilla/websocket v1.5.3
## explicit; go 1.12
github.com/gorilla/websocket
# github.com/grafana/sobek v0.0.0-20240927094302-19dd311f018f
# github.com/grafana/sobek v0.0.0-20241023145759-2dc9daf5bfa2
## explicit; go 1.20
github.com/grafana/sobek
github.com/grafana/sobek/ast
Expand Down