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

Add support for cookie jar to k6/ws #2193

Merged
merged 2 commits into from
Oct 26, 2021
Merged

Add support for cookie jar to k6/ws #2193

merged 2 commits into from
Oct 26, 2021

Conversation

mstoykov
Copy link
Contributor

@mstoykov mstoykov commented Oct 21, 2021

This does require the ws module to get access to the http.CookieJar's
jar so we need to make it exported, but through the magic of js tags
we can make it not accessible from the js side.

@mstoykov
Copy link
Contributor Author

IMO this fixes the major problem with cookies in the websocket and specifically #1226. Adding the cookies param as in HTTP IMO is not a good idea:

  1. the merging in k6/http is already very ... strange and not really noticeable
  2. I would argue setting a single cookie is likely not a common problem
  3. Setting the Cookie header is almost the same amount of complication IMO and at least hopefully will be easier to explain how it's merged with the jar cookies

js/modules/k6/ws/ws_test.go Outdated Show resolved Hide resolved
@@ -40,6 +40,12 @@ type HTTPCookieJar struct {
ctx *context.Context
}

// GetJarFromHTTPCookieJar is a helper function for modules outside of http to get the jar from inside the cookie jar
// TODO figure out how to do this through only goja
func GetJarFromHTTPCookieJar(jar *HTTPCookieJar) *cookiejar.Jar {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would be more readable/convenient as a method on HTTPCookieJar. GetStdlibJar?

It's unfortunate that the ws module now depends on the http one, but hopefully we can clean this up in an upcoming refactor.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, GetJarFromHTTPCookieJar needs an HTTPCookieJar anyway. By the way, I think the method name can be just: StdlibJar.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The whole point of this not being on the HTTPCookieJar is that I don't want this to be callable from inside the js code ;)

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough:) http.StdlibJar() instead of `GetJarFromHTTPCookieJar‘? It’s already clear that you’re getting an HTTP jar.

Copy link
Member

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

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

LGTM, nice work! I have some minor suggestions.

@@ -91,7 +93,7 @@ func assertMetricEmittedCount(t *testing.T, metricName string, sampleContainers
assert.Equal(t, count, actualCount, "url %s emitted %s %d times, expected was %d times", url, metricName, actualCount, count)
}

func newRuntime(t testing.TB) (*httpmultibin.HTTPMultiBin, chan stats.SampleContainer, *goja.Runtime) {
func newRuntime(t testing.TB) (*httpmultibin.HTTPMultiBin, chan stats.SampleContainer, *goja.Runtime, *context.Context) {
Copy link
Member

Choose a reason for hiding this comment

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

Can it be better if we make it:

func newRuntime(t testing.TB) runtimeState {
    return runtimeState{
        ctx: ...,
        tb: ..., // httpmultibin.HTTPMultiBin
        samples: ...,
        rt: ...,
}

It will be clearer and we won't need to discard some of the values if we don't need them like this:

tb, _, rt, _ := newRuntime(t)

func TestCookieJar(t *testing.T) {
t.Parallel()
tb, samples, rt, ctxPtr := newRuntime(t)
sr := tb.Replacer.Replace
Copy link
Member

Choose a reason for hiding this comment

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

Instead sr := tb.Replacer.Replace, we can also move this into the runtimeState:

rs := newRuntime()
_, err := rs.RunString(`...`) // this can call rs.rt.RunString(sr(`...`))

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are other places we need both the runtime and the replacer that aren't for running a script

conn, err := (&websocket.Upgrader{}).Upgrade(w, req, responseHeaders)
if err != nil {
t.Fatalf("/ws-echo-someheader cannot upgrade request: %v", err)
return
Copy link
Member

Choose a reason for hiding this comment

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

You don't need to return here.

})
var someheader = res.headers["Echo-Someheader"];
if (someheader != undefined) {
throw new Error("somehader is echoed back by test server even though it doesn't exist");
Copy link
Member

Choose a reason for hiding this comment

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

Tiny typo: someheader

@mstoykov mstoykov force-pushed the wsJarSupport branch 2 times, most recently from 7d11be2 to 84547f3 Compare October 25, 2021 15:14
This does require the ws module to get access to the http.CookieJar's
jar so we need to make it exported, but through the magic of `js` tags
we can make it not accessible from the js side.
@@ -36,7 +36,8 @@ import (

// HTTPCookieJar is cookiejar.Jar wrapper to be used in js scripts
type HTTPCookieJar struct {
jar *cookiejar.Jar
// js is to make it not be accessible from inside goja/js, the json is because it's used if we return it from setup
Jar *cookiejar.Jar `js:"-" json:"-"`
Copy link
Member

Choose a reason for hiding this comment

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

You came up with a better solution, cool!

@mstoykov mstoykov merged commit a8f6b73 into master Oct 26, 2021
@mstoykov mstoykov deleted the wsJarSupport branch October 26, 2021 08:00
@mstoykov mstoykov added this to the v0.35.0 milestone Oct 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants