-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
fix/validate-args-length #2823 #3974
Conversation
Signed-off-by: arjitGopher <[email protected]>
js/modules/k6/http/http.go
Outdated
validateArgCount := func(methodName string,args ...sobek.Value){ | ||
if len(args)>2 { | ||
vu.State().Logger.Warningf("%s method has more than two arguments",methodName) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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){ | |
if len(args)>2 { | |
vu.State().Logger.Warnf("http.%s method has more than two arguments",methodName) | |
} | |
} |
Small suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do agree with @joanlopez that it's better to add http
prefix.
Also, how about changing the message to something more actionable, like http.%s requires two arguments, but %d were provided. Please adjust the input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the new code is not correctly formatted, @ArjitGopher could you run the linter and fix the related issues, please?
Beyond that, I only have a small suggestion because I think these functions are usually called like http.get(...)
, as suggested in the official docs, and in any case, it's always good idea to disambiguate.
Other than that, it looks good! 👍🏻
cc/ @olegbespalov
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution!
I do agree with what @joanlopez said, also have a minor suggestion for the actual message
js/modules/k6/http/http.go
Outdated
validateArgCount := func(methodName string,args ...sobek.Value){ | ||
if len(args)>2 { | ||
vu.State().Logger.Warningf("%s method has more than two arguments",methodName) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do agree with @joanlopez that it's better to add http
prefix.
Also, how about changing the message to something more actionable, like http.%s requires two arguments, but %d were provided. Please adjust the input.
Hi @ArjitGopher. Out of curiosity, are you going to apply feedback, or you have no capacity of doing that? |
Hi @olegbespalov I agree with the suggestions and will add them today, Thanks |
js/modules/k6/http/http.go
Outdated
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", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit; I'd prefer if we can use the "actionable" version of the message suggested by @olegbespalov 👉🏻 here.
js/modules/k6/http/http.go
Outdated
// 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...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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...) | |
// 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) | |
args = append([]sobek.Value{sobek.Undefined()}, args...) | |
return mi.defaultClient.Request(http.MethodGet, url, args...) |
Nit; I'd prefer if we can keep the order as suggested here, as I think the already existing comment is tied to the args
slice we build below, so it kinda makes more sense to keep both together. Same for the "head"
method.
Could you confirm it, please @olegbespalov?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tested and, checked the UI and actually spotted a bug 😢
So my script was:
import http from "k6/http";
export default function () {
http.get("https://test-api.k6.io/", {}, "lorem")
}
and it didn't produce any warning
When I re-looked into the code I found that validateArgCount
doesn't get all arguments, it just gets all arguments, except url
which is separated in the signatures.
So to make it work as expected, we need to adjust the logical expression inside validate, like len(args) > 1
, but I'd also ask adjusting both comments (made inline suggestion)
@@ -69,14 +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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// get method should not have more than two arguments | |
// get method should not have more than one additional arguments |
What?
This PR aims to solve the issue #2823
Why?