-
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
Add support for cookie jar to k6/ws #2193
Conversation
IMO this fixes the major problem with cookies in the websocket and specifically #1226. Adding the
|
4b1ce3c
to
8fc3c36
Compare
js/modules/k6/http/cookiejar.go
Outdated
@@ -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 { |
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 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.
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.
Yes, GetJarFromHTTPCookieJar
needs an HTTPCookieJar
anyway. By the way, I think the method name can be just: StdlibJar
.
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.
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 ;)
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.
Fair enough:) http.StdlibJar()
instead of `GetJarFromHTTPCookieJar‘? It’s already clear that you’re getting an HTTP jar.
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.
LGTM, nice work! I have some minor suggestions.
js/modules/k6/ws/ws_test.go
Outdated
@@ -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) { |
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.
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)
js/modules/k6/ws/ws_test.go
Outdated
func TestCookieJar(t *testing.T) { | ||
t.Parallel() | ||
tb, samples, rt, ctxPtr := newRuntime(t) | ||
sr := tb.Replacer.Replace |
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.
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?
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.
There are other places we need both the runtime and the replacer that aren't for running a script
js/modules/k6/ws/ws_test.go
Outdated
conn, err := (&websocket.Upgrader{}).Upgrade(w, req, responseHeaders) | ||
if err != nil { | ||
t.Fatalf("/ws-echo-someheader cannot upgrade request: %v", err) | ||
return |
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.
You don't need to return
here.
js/modules/k6/ws/ws_test.go
Outdated
}) | ||
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"); |
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.
Tiny typo: someheader
7d11be2
to
84547f3
Compare
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.
84547f3
to
dc36cb2
Compare
@@ -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:"-"` |
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.
You came up with a better solution, cool!
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
tagswe can make it not accessible from the js side.