Skip to content

Commit

Permalink
fix(http): add check for validating args length in get and head methods
Browse files Browse the repository at this point in the history
Signed-off-by: arjitGopher <agarwalarjit.agarwal@gmail.com>
  • Loading branch information
ArjitGopher committed Oct 2, 2024
1 parent baba871 commit b800cf2
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions js/modules/k6/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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...)
})
Expand Down

0 comments on commit b800cf2

Please sign in to comment.