From 1b10b3c3065fa784a2d5162d9f51c30b3ee222a7 Mon Sep 17 00:00:00 2001 From: Maksimall89 Date: Mon, 2 May 2022 16:44:14 +0400 Subject: [PATCH 1/9] feat: add full clear cookies --- js/modules/k6/http/cookiejar.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/js/modules/k6/http/cookiejar.go b/js/modules/k6/http/cookiejar.go index 108f14dd6c2..fe03a03f03c 100644 --- a/js/modules/k6/http/cookiejar.go +++ b/js/modules/k6/http/cookiejar.go @@ -100,3 +100,19 @@ func (j CookieJar) Set(url, name, value string, opts goja.Value) (bool, error) { j.Jar.SetCookies(u, []*http.Cookie{&c}) return true, nil } + +// Clear all cookies for a particular URL +func (j CookieJar) Clear(url string, opts goja.Value) (bool, error) { + u, err := neturl.Parse(url) + if err != nil { + return false, err + } + + cookies := j.Jar.Cookies(u) + for _, c := range cookies { + c.MaxAge = -1 + } + j.Jar.SetCookies(u, cookies) + + return true, nil +} From ad3bdccd22bd815acdec95f705d5abc688d532ed Mon Sep 17 00:00:00 2001 From: Maksimall89 Date: Mon, 2 May 2022 17:16:03 +0400 Subject: [PATCH 2/9] feat: add full delete cookie --- js/modules/k6/http/cookiejar.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/js/modules/k6/http/cookiejar.go b/js/modules/k6/http/cookiejar.go index fe03a03f03c..c499753ea28 100644 --- a/js/modules/k6/http/cookiejar.go +++ b/js/modules/k6/http/cookiejar.go @@ -116,3 +116,16 @@ func (j CookieJar) Clear(url string, opts goja.Value) (bool, error) { return true, nil } + +// Delete cookies for a particular URL +func (j CookieJar) Delete(url, name string, opts goja.Value) (bool, error) { + u, err := neturl.Parse(url) + if err != nil { + return false, err + } + + c := http.Cookie{Name: name, MaxAge: -1} + j.Jar.SetCookies(u, []*http.Cookie{&c}) + + return true, nil +} From c2fd0630d6d49ca1cdda0bf43c4be7e92a83bbf9 Mon Sep 17 00:00:00 2001 From: Maksimall89 Date: Thu, 5 May 2022 19:31:44 +0400 Subject: [PATCH 3/9] style: deleted unused parameters from functions --- js/modules/k6/http/cookiejar.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/js/modules/k6/http/cookiejar.go b/js/modules/k6/http/cookiejar.go index c499753ea28..85c2e8896c7 100644 --- a/js/modules/k6/http/cookiejar.go +++ b/js/modules/k6/http/cookiejar.go @@ -21,6 +21,7 @@ package http import ( + "errors" "fmt" "net/http" "net/http/cookiejar" @@ -102,10 +103,10 @@ func (j CookieJar) Set(url, name, value string, opts goja.Value) (bool, error) { } // Clear all cookies for a particular URL -func (j CookieJar) Clear(url string, opts goja.Value) (bool, error) { +func (j CookieJar) Clear(url string) error { u, err := neturl.Parse(url) if err != nil { - return false, err + return err } cookies := j.Jar.Cookies(u) @@ -114,18 +115,22 @@ func (j CookieJar) Clear(url string, opts goja.Value) (bool, error) { } j.Jar.SetCookies(u, cookies) - return true, nil + return nil } // Delete cookies for a particular URL -func (j CookieJar) Delete(url, name string, opts goja.Value) (bool, error) { +func (j CookieJar) Delete(url, name string) error { + if name == "" { + return errors.New("cookie: is null") + } + u, err := neturl.Parse(url) if err != nil { - return false, err + return err } c := http.Cookie{Name: name, MaxAge: -1} j.Jar.SetCookies(u, []*http.Cookie{&c}) - return true, nil + return nil } From 7d850cd27dbdd69e9a2a38849ff1ec474beeb43d Mon Sep 17 00:00:00 2001 From: Maksimall89 Date: Thu, 5 May 2022 20:43:45 +0400 Subject: [PATCH 4/9] feat: add tests for cookies --- js/modules/k6/http/request_test.go | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/js/modules/k6/http/request_test.go b/js/modules/k6/http/request_test.go index 3ad6947cd29..59d07ded23f 100644 --- a/js/modules/k6/http/request_test.go +++ b/js/modules/k6/http/request_test.go @@ -908,6 +908,43 @@ func TestRequestAndBatch(t *testing.T) { assert.NoError(t, err) assertRequestMetricsEmitted(t, metrics.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/cookies"), "", 200, "") }) + + t.Run("clear", func(t *testing.T) { + cookieJar, err := cookiejar.New(nil) + assert.NoError(t, err) + state.CookieJar = cookieJar + _, err = rt.RunString(sr(` + var jar = http.cookieJar(); + var res = http.request("GET", "HTTPBIN_URL/cookies/set?key1=value1&key2=value2"); + if (res.status != 200) { throw new Error("wrong status: " + res.status); } + var jarCookies = jar.cookiesForURL("HTTPBIN_URL/cookies"); + if (jarCookies.key1[0] != "value1" || jarCookies.key2[0] != "value2" || Object.keys(jarCookies).length != 2) { throw new Error("wrong cookies values in jar"); } + jar.clear('HTTPBIN_URL/cookies'); + var jarCookies = jar.cookiesForURL("HTTPBIN_URL/cookies"); + if (Object.keys(jarCookies).length != 0) { throw new Error("wrong clean: unexpected cookie in jar"); } + `)) + assert.NoError(t, err) + assertRequestMetricsEmitted(t, metrics.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/cookies"), "", 200, "") + }) + + t.Run("delete", func(t *testing.T) { + cookieJar, err := cookiejar.New(nil) + assert.NoError(t, err) + state.CookieJar = cookieJar + _, err = rt.RunString(sr(` + var jar = http.cookieJar(); + var res = http.request("GET", "HTTPBIN_URL/cookies/set?key1=value1&key2=value2"); + if (res.status != 200) { throw new Error("wrong status: " + res.status); } + var jarCookies = jar.cookiesForURL("HTTPBIN_URL/cookies"); + if (jarCookies.key1[0] != "value1" || jarCookies.key2[0] != "value2" || Object.keys(jarCookies).length != 2) { throw new Error("wrong cookies values in jar"); } + jar.delete('HTTPBIN_URL/cookies', 'key1'); + var jarCookies = jar.cookiesForURL("HTTPBIN_URL/cookies"); + if (Object.keys(jarCookies).length != 1 || jarCookies.key2[0] != "value2") { throw new Error("wrong delete: unexpected cookie in jar"); } + `)) + assert.NoError(t, err) + assertRequestMetricsEmitted(t, metrics.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/cookies"), "", 200, "") + }) + }) t.Run("auth", func(t *testing.T) { From 14644afd4b556ad4777c47aa7f00488d59198c99 Mon Sep 17 00:00:00 2001 From: Maksimall89 Date: Thu, 5 May 2022 21:12:16 +0400 Subject: [PATCH 5/9] fix: style go fmt --- js/modules/k6/http/request_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/js/modules/k6/http/request_test.go b/js/modules/k6/http/request_test.go index 59d07ded23f..854c9d8e5c2 100644 --- a/js/modules/k6/http/request_test.go +++ b/js/modules/k6/http/request_test.go @@ -944,7 +944,6 @@ func TestRequestAndBatch(t *testing.T) { assert.NoError(t, err) assertRequestMetricsEmitted(t, metrics.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/cookies"), "", 200, "") }) - }) t.Run("auth", func(t *testing.T) { From 02bac023023dee54ca78e3816c99ce788290a82a Mon Sep 17 00:00:00 2001 From: Maksimall89 Date: Thu, 5 May 2022 22:42:38 +0400 Subject: [PATCH 6/9] fix: tests for cookies --- js/modules/k6/http/request_test.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/js/modules/k6/http/request_test.go b/js/modules/k6/http/request_test.go index 854c9d8e5c2..6041e28b658 100644 --- a/js/modules/k6/http/request_test.go +++ b/js/modules/k6/http/request_test.go @@ -915,13 +915,12 @@ func TestRequestAndBatch(t *testing.T) { state.CookieJar = cookieJar _, err = rt.RunString(sr(` var jar = http.cookieJar(); - var res = http.request("GET", "HTTPBIN_URL/cookies/set?key1=value1&key2=value2"); - if (res.status != 200) { throw new Error("wrong status: " + res.status); } - var jarCookies = jar.cookiesForURL("HTTPBIN_URL/cookies"); - if (jarCookies.key1[0] != "value1" || jarCookies.key2[0] != "value2" || Object.keys(jarCookies).length != 2) { throw new Error("wrong cookies values in jar"); } + jar.set("HTTPBIN_URL/cookies", "key", "value"); + var res = http.request("GET", "HTTPBIN_URL/cookies"); + if (res.json().key != "value") { throw new Error("cookie 'key' unexpectedly don't found"); } jar.clear('HTTPBIN_URL/cookies'); - var jarCookies = jar.cookiesForURL("HTTPBIN_URL/cookies"); - if (Object.keys(jarCookies).length != 0) { throw new Error("wrong clean: unexpected cookie in jar"); } + res = http.request("GET", "HTTPBIN_URL/cookies"); + if (res.json().key == "value") { throw new Error("wrong clean: unexpected cookie in jar"); } `)) assert.NoError(t, err) assertRequestMetricsEmitted(t, metrics.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/cookies"), "", 200, "") @@ -933,13 +932,13 @@ func TestRequestAndBatch(t *testing.T) { state.CookieJar = cookieJar _, err = rt.RunString(sr(` var jar = http.cookieJar(); - var res = http.request("GET", "HTTPBIN_URL/cookies/set?key1=value1&key2=value2"); - if (res.status != 200) { throw new Error("wrong status: " + res.status); } - var jarCookies = jar.cookiesForURL("HTTPBIN_URL/cookies"); - if (jarCookies.key1[0] != "value1" || jarCookies.key2[0] != "value2" || Object.keys(jarCookies).length != 2) { throw new Error("wrong cookies values in jar"); } - jar.delete('HTTPBIN_URL/cookies', 'key1'); - var jarCookies = jar.cookiesForURL("HTTPBIN_URL/cookies"); - if (Object.keys(jarCookies).length != 1 || jarCookies.key2[0] != "value2") { throw new Error("wrong delete: unexpected cookie in jar"); } + jar.set("HTTPBIN_URL/cookies", "key1", "value1"); + jar.set("HTTPBIN_URL/cookies", "key2", "value2"); + var res = http.request("GET", "HTTPBIN_URL/cookies"); + if (res.json().key1 != "value1" || res.json().key2 != "value2") { throw new Error("cookie 'keys' unexpectedly don't found"); } + jar.delete('HTTPBIN_URL/cookies', "key1"); + res = http.request("GET", "HTTPBIN_URL/cookies"); + if (res.json().key1 == "value1" || res.json().key2 != "value2" ) { throw new Error("wrong clean: unexpected cookie in jar"); } `)) assert.NoError(t, err) assertRequestMetricsEmitted(t, metrics.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/cookies"), "", 200, "") From 99eaa8e5135e1d06f797cc3f496984ce3e419dd2 Mon Sep 17 00:00:00 2001 From: Max Rogozhnikov Date: Fri, 6 May 2022 11:51:50 +0300 Subject: [PATCH 7/9] feat: add t.Parallel() --- js/modules/k6/http/request_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/js/modules/k6/http/request_test.go b/js/modules/k6/http/request_test.go index 6041e28b658..5e15437faec 100644 --- a/js/modules/k6/http/request_test.go +++ b/js/modules/k6/http/request_test.go @@ -910,6 +910,7 @@ func TestRequestAndBatch(t *testing.T) { }) t.Run("clear", func(t *testing.T) { + t.Parallel() cookieJar, err := cookiejar.New(nil) assert.NoError(t, err) state.CookieJar = cookieJar @@ -927,6 +928,7 @@ func TestRequestAndBatch(t *testing.T) { }) t.Run("delete", func(t *testing.T) { + t.Parallel() cookieJar, err := cookiejar.New(nil) assert.NoError(t, err) state.CookieJar = cookieJar From 4506459f15652280a6bb77858f22ff7a34d834c5 Mon Sep 17 00:00:00 2001 From: Max Rogozhnikov Date: Fri, 6 May 2022 12:18:28 +0300 Subject: [PATCH 8/9] fix: add nolint parallel test --- js/modules/k6/http/request_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/modules/k6/http/request_test.go b/js/modules/k6/http/request_test.go index 5e15437faec..84f8f4ea0bc 100644 --- a/js/modules/k6/http/request_test.go +++ b/js/modules/k6/http/request_test.go @@ -909,8 +909,8 @@ func TestRequestAndBatch(t *testing.T) { assertRequestMetricsEmitted(t, metrics.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/cookies"), "", 200, "") }) + //nolint:parallel test t.Run("clear", func(t *testing.T) { - t.Parallel() cookieJar, err := cookiejar.New(nil) assert.NoError(t, err) state.CookieJar = cookieJar @@ -927,8 +927,8 @@ func TestRequestAndBatch(t *testing.T) { assertRequestMetricsEmitted(t, metrics.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/cookies"), "", 200, "") }) + //nolint:parallel test t.Run("delete", func(t *testing.T) { - t.Parallel() cookieJar, err := cookiejar.New(nil) assert.NoError(t, err) state.CookieJar = cookieJar From 85356a8f57fd650eb35eb28b8f9ba91cc9e68d61 Mon Sep 17 00:00:00 2001 From: Max Rogozhnikov Date: Fri, 6 May 2022 12:50:59 +0300 Subject: [PATCH 9/9] fix: name paralleltest --- js/modules/k6/http/request_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/modules/k6/http/request_test.go b/js/modules/k6/http/request_test.go index 84f8f4ea0bc..6cc66b59c42 100644 --- a/js/modules/k6/http/request_test.go +++ b/js/modules/k6/http/request_test.go @@ -909,7 +909,7 @@ func TestRequestAndBatch(t *testing.T) { assertRequestMetricsEmitted(t, metrics.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/cookies"), "", 200, "") }) - //nolint:parallel test + //nolint:paralleltest t.Run("clear", func(t *testing.T) { cookieJar, err := cookiejar.New(nil) assert.NoError(t, err) @@ -927,7 +927,7 @@ func TestRequestAndBatch(t *testing.T) { assertRequestMetricsEmitted(t, metrics.GetBufferedSamples(samples), "GET", sr("HTTPBIN_URL/cookies"), "", 200, "") }) - //nolint:parallel test + //nolint:paralleltest t.Run("delete", func(t *testing.T) { cookieJar, err := cookiejar.New(nil) assert.NoError(t, err)