From b800cf2edce20f5c67aacdf117b251df00308544 Mon Sep 17 00:00:00 2001 From: arjitGopher Date: Wed, 2 Oct 2024 22:40:00 +0530 Subject: [PATCH] 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...) })