tsung
tsung copied to clipboard
Set dynamic variable to a cookie?
I want to get the session cookie from a login response. Is there a way to get a cookie and use it in subsequent requests?
I notice that dynvars can read response headers via <dyn_variable name="..." header="..."/>. But this is not appropriate for cookies, because there may be multiple Set-Cookie headers. These headers are concatenated by mochiweb_headers:get_value, making subsequent parsing failed.
For example, POST /api/login on my website sets the following cookies on a successful login:
Set-Cookie:sessionid=AAA; expires=Tue, 27-Feb-2018 10:30:05 GMT; HttpOnly; Max-Age=28800; Path=/
Set-Cookie:csrftoken=BBB; expires=Tue, 26-Feb-2019 02:30:05 GMT; Max-Age=31449600; Path=/
And my config looks like:
<request>
<dyn_variable name="sessionid" header="Set-Cookie/sessionid"/>
<http url="/api/login" method="POST" contents="username=user&password=pass"/>
</request>
It fails to set the dynvar sessionid to AAA. The reason is that mochiweb_headers:get_value concatenates the two headers by ", ":
csrftoken=BBB; expires=Tue, 26-Feb-2019 02:30:05 GMT; Max-Age=31449600; Path=/, sessionid=AAA; expires=Tue, 27-Feb-2018 10:30:05 GMT; HttpOnly; Max-Age=28800; Path=/
and the subsequent parse_header(SubV, ";") gets an item Path=/, sessionid=AAA instead of sessionid=AAA.
I change ", " to "; " in the line https://github.com/processone/tsung/blob/v1.7.0/src/lib/mochiweb_headers.erl#L257 to workaround this issue. But this is only a hack.
Maybe there should be a dedicated way to access response cookies which takes into account.
I was also wondering if it would be a good idea, to have some sort of mechanism to ask tsung's cookie jar for information. This way we could also get a value, that represents what tsung understood.
That said, I haven't fully understood how Set-Cookie/sessionid works at all ^^ Still reading the cookie handling code…
@xiaotianrandom do you have an example of how you worked around this?
You can find my patch to work around this here. Note that it's only a hack and not thoroughly tested.
Okay thanks, that work-around/hack is working for me.