From b800cf2edce20f5c67aacdf117b251df00308544 Mon Sep 17 00:00:00 2001 From: arjitGopher Date: Wed, 2 Oct 2024 22:40:00 +0530 Subject: [PATCH 1/5] fix(http): add check for validating args length in get and head methods Signed-off-by: arjitGopher --- js/modules/k6/http/http.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/js/modules/k6/http/http.go b/js/modules/k6/http/http.go index 5fcfc2ae1ed..ff8af935cea 100644 --- a/js/modules/k6/http/http.go +++ b/js/modules/k6/http/http.go @@ -60,6 +60,13 @@ func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { } } + + validateArgCount := func(methodName string,args ...sobek.Value){ + if len(args)>2 { + vu.State().Logger.Warningf("%s method has more than two arguments",methodName) + } + } + mustExport("url", mi.URL) mustExport("CookieJar", mi.newCookieJar) mustExport("cookieJar", mi.getVUCookieJar) @@ -71,12 +78,18 @@ func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { mustExport("get", func(url sobek.Value, args ...sobek.Value) (*Response, error) { // http.get(url, params) doesn't have a body argument, so we add undefined // as the third argument to http.request(method, url, body, params) + + // http.get method should not have more than two arguments + validateArgCount("get",args...) args = append([]sobek.Value{sobek.Undefined()}, args...) return mi.defaultClient.Request(http.MethodGet, url, args...) }) mustExport("head", func(url sobek.Value, args ...sobek.Value) (*Response, error) { // http.head(url, params) doesn't have a body argument, so we add undefined // as the third argument to http.request(method, url, body, params) + + // http.head method should not have more than two arguments + validateArgCount("head",args...) args = append([]sobek.Value{sobek.Undefined()}, args...) return mi.defaultClient.Request(http.MethodHead, url, args...) }) From f9bbc5c9e3263ca9b5de6721aef8a66989247132 Mon Sep 17 00:00:00 2001 From: Arjit Agarwal Date: Thu, 3 Oct 2024 00:53:50 +0530 Subject: [PATCH 2/5] fix(http.go): fixed method name in comment --- js/modules/k6/http/http.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/modules/k6/http/http.go b/js/modules/k6/http/http.go index ff8af935cea..ffc177ac626 100644 --- a/js/modules/k6/http/http.go +++ b/js/modules/k6/http/http.go @@ -79,7 +79,7 @@ func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { // http.get(url, params) doesn't have a body argument, so we add undefined // as the third argument to http.request(method, url, body, params) - // http.get method should not have more than two arguments + // get method should not have more than two arguments validateArgCount("get",args...) args = append([]sobek.Value{sobek.Undefined()}, args...) return mi.defaultClient.Request(http.MethodGet, url, args...) @@ -88,7 +88,7 @@ func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { // http.head(url, params) doesn't have a body argument, so we add undefined // as the third argument to http.request(method, url, body, params) - // http.head method should not have more than two arguments + // head method should not have more than two arguments validateArgCount("head",args...) args = append([]sobek.Value{sobek.Undefined()}, args...) return mi.defaultClient.Request(http.MethodHead, url, args...) From 29f1cdc11f43601a72d3268b3b2e66231c8bbcb3 Mon Sep 17 00:00:00 2001 From: arjitGopher Date: Fri, 15 Nov 2024 20:59:05 +0530 Subject: [PATCH 3/5] fix(http.go): use more meaningful log message and format file --- js/modules/k6/http/http.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/js/modules/k6/http/http.go b/js/modules/k6/http/http.go index ffc177ac626..dd59d19ea24 100644 --- a/js/modules/k6/http/http.go +++ b/js/modules/k6/http/http.go @@ -60,10 +60,10 @@ func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { } } - - validateArgCount := func(methodName string,args ...sobek.Value){ - if len(args)>2 { - vu.State().Logger.Warningf("%s method has more than two arguments",methodName) + validateArgCount := func(methodName string, args ...sobek.Value) { + numberOfArgs := len(args) + if numberOfArgs > 2 { + vu.State().Logger.Warningf("http.%s method has more than two arguments, but %d were provided", methodName, len(args)) } } @@ -80,7 +80,7 @@ func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { // as the third argument to http.request(method, url, body, params) // get method should not have more than two arguments - validateArgCount("get",args...) + validateArgCount("get", args...) args = append([]sobek.Value{sobek.Undefined()}, args...) return mi.defaultClient.Request(http.MethodGet, url, args...) }) @@ -89,7 +89,7 @@ func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { // as the third argument to http.request(method, url, body, params) // head method should not have more than two arguments - validateArgCount("head",args...) + validateArgCount("head", args...) args = append([]sobek.Value{sobek.Undefined()}, args...) return mi.defaultClient.Request(http.MethodHead, url, args...) }) From 1efc0925f96890eadea25c95d0fdef86243b4ecc Mon Sep 17 00:00:00 2001 From: arjitGopher Date: Fri, 15 Nov 2024 21:03:12 +0530 Subject: [PATCH 4/5] refactor(http.go): use variable numberOfArgs in log --- js/modules/k6/http/http.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/js/modules/k6/http/http.go b/js/modules/k6/http/http.go index dd59d19ea24..c280f5de250 100644 --- a/js/modules/k6/http/http.go +++ b/js/modules/k6/http/http.go @@ -63,7 +63,8 @@ func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { validateArgCount := func(methodName string, args ...sobek.Value) { numberOfArgs := len(args) if numberOfArgs > 2 { - vu.State().Logger.Warningf("http.%s method has more than two arguments, but %d were provided", methodName, len(args)) + vu.State().Logger.Warningf("http.%s method has more than two arguments, but %d were provided", + methodName, numberOfArgs) } } From def8e9d887af77d08d0fb7866bd59344041b903b Mon Sep 17 00:00:00 2001 From: arjitGopher Date: Tue, 19 Nov 2024 13:14:28 +0530 Subject: [PATCH 5/5] fix(http): provide actionable log message --- js/modules/k6/http/http.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/js/modules/k6/http/http.go b/js/modules/k6/http/http.go index c280f5de250..fe7876ed672 100644 --- a/js/modules/k6/http/http.go +++ b/js/modules/k6/http/http.go @@ -63,7 +63,8 @@ func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { validateArgCount := func(methodName string, args ...sobek.Value) { numberOfArgs := len(args) if numberOfArgs > 2 { - vu.State().Logger.Warningf("http.%s method has more than two arguments, but %d were provided", + vu.State().Logger.Warningf( + "http.%s requires two arguments, but %d were provided. Please adjust the input.", methodName, numberOfArgs) } } @@ -77,20 +78,24 @@ func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { // wrappers (facades) that convert the old k6 idiosyncratic APIs to the new // proper Client ones that accept Request objects and don't suck mustExport("get", func(url sobek.Value, args ...sobek.Value) (*Response, error) { + // get method should not have more than two arguments + + validateArgCount("get", args...) + // http.get(url, params) doesn't have a body argument, so we add undefined // as the third argument to http.request(method, url, body, params) - // get method should not have more than two arguments - validateArgCount("get", args...) args = append([]sobek.Value{sobek.Undefined()}, args...) return mi.defaultClient.Request(http.MethodGet, url, args...) }) mustExport("head", func(url sobek.Value, args ...sobek.Value) (*Response, error) { + // head method should not have more than two arguments + + validateArgCount("head", args...) + // http.head(url, params) doesn't have a body argument, so we add undefined // as the third argument to http.request(method, url, body, params) - // head method should not have more than two arguments - validateArgCount("head", args...) args = append([]sobek.Value{sobek.Undefined()}, args...) return mi.defaultClient.Request(http.MethodHead, url, args...) })