From dac7f057b1e51578e94c369dfd6142aa19ff0b93 Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Thu, 18 Feb 2021 15:42:14 +0200 Subject: [PATCH] use Number.isInteger to check the arguments for expectedStatuses --- js/modules/k6/http/response_callback.go | 7 ++++--- js/modules/k6/http/response_callback_test.go | 10 +++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/js/modules/k6/http/response_callback.go b/js/modules/k6/http/response_callback.go index c9cec7a221c..bbe9168c29b 100644 --- a/js/modules/k6/http/response_callback.go +++ b/js/modules/k6/http/response_callback.go @@ -76,7 +76,7 @@ func (*HTTP) ExpectedStatuses(ctx context.Context, args ...goja.Value) *expected common.Throw(rt, fmt.Errorf("argument number %d to expectedStatuses was neither an integer nor an object like {min:100, max:329}", i+1)) } - if o.ClassName() == "Number" { + if checkNumber(arg, rt) { result.exact = append(result.exact, int(o.ToInteger())) } else { min := o.Get("min") @@ -96,8 +96,9 @@ func (*HTTP) ExpectedStatuses(ctx context.Context, args ...goja.Value) *expected } func checkNumber(a goja.Value, rt *goja.Runtime) bool { - o := a.ToObject(rt) - return o != nil && o.ClassName() == "Number" + c, _ := goja.AssertFunction(rt.GlobalObject().Get("Number").ToObject(rt).Get("isInteger")) + v, err := c(goja.Undefined(), a) + return err == nil && v.ToBoolean() } // SetResponseCallback .. diff --git a/js/modules/k6/http/response_callback_test.go b/js/modules/k6/http/response_callback_test.go index 7e7346b90fc..15187b5d7cd 100644 --- a/js/modules/k6/http/response_callback_test.go +++ b/js/modules/k6/http/response_callback_test.go @@ -63,11 +63,15 @@ func TestExpectedStatuses(t *testing.T) { code: `(http.expectedStatuses(200, 300, {min: 200, max:"300"}))`, err: "both min and max need to be number for argument number 3", }, - "float status code": { // TODO probably should not work - expected: expectedStatuses{exact: []int{200, 300}, minmax: [][2]int{{200, 300}}}, - code: `(http.expectedStatuses(200, 300.5, {min: 200, max:300}))`, + "float status code": { + err: "argument number 2 to expectedStatuses was neither an integer nor an object like {min:100, max:329}", + code: `(http.expectedStatuses(200, 300.5, {min: 200, max:300}))`, }, + "float max status code": { + err: "both min and max need to be number for argument number 3", + code: `(http.expectedStatuses(200, 300, {min: 200, max:300.5}))`, + }, "no arguments": { code: `(http.expectedStatuses())`, err: "no arguments",