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

fix/validate-args-length #2823 #3974

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions js/modules/k6/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}

// NewModuleInstance returns an HTTP module instance for each VU.
func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance {

Check failure on line 40 in js/modules/k6/http/http.go

View workflow job for this annotation

GitHub Actions / lint

Function 'NewModuleInstance' is too long (81 > 80) (funlen)
rt := vu.Runtime()
mi := &ModuleInstance{
vu: vu,
Expand All @@ -60,6 +60,15 @@
}
}

validateArgCount := func(methodName string, args ...sobek.Value) {
numberOfArgs := len(args)
if numberOfArgs > 2 {
vu.State().Logger.Warningf(
"http.%s requires two arguments, but %d were provided. Please adjust the input.",
methodName, numberOfArgs)
}
}

mustExport("url", mi.URL)
mustExport("CookieJar", mi.newCookieJar)
mustExport("cookieJar", mi.getVUCookieJar)
Expand All @@ -69,14 +78,24 @@
// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// get method should not have more than two arguments
// get method should not have more than one additional 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)

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)

args = append([]sobek.Value{sobek.Undefined()}, args...)
return mi.defaultClient.Request(http.MethodHead, url, args...)
})
Expand Down
Loading