-
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 HTTP cookies get/set JS interface #323
Conversation
I've now also added draft (hidden) documentation for this functionality: https://docs.k6.io/v1.0/docs/cookies |
I have read the doc and example and I have some questions:
|
|
A late comment around the API here: I don't like the fact that |
Yeah, I agree with @ragnarlonn, it makes no sense from a user's perspective for an argument to a request to make an implicit global state manipulation. |
Thanks @ragnarlonn, @ppcano and @liclac for feedback! Ok, how does the following changes sound:
Examples: Global cookie jar: export default function() {
// Add cookie to global cookie jar
let jar = http.getCookieJar();
jar.set("name", "value", { domain: "example.com", path: "/" });
// Global cookie jar is active for requests by default
let res = http.get("https://example.com/");
// Get object of active cookies (only name/value) for a particular URL
let cookiesForURL = jar.getCookiesForURL("https://example.com/");
} Response object export default function() {
let res = http.get("https://example.com/set-cookie?my_cookie=my_value");
check(res, {
"has correct value": (r) => r.cookies.my_cookie[0].value === "my_value",
"has correct expires": (r) => r.cookies.my_cookie[0].expires > Date.now()
});
} Local cookie jars: export default function() {
// Create local cookie jar
let jar = new http.CookieJar();
jar.set("name", "value", { domain: "example.com", path: "/" });
// When setting cookie jar like this it will only be "active" for the duration of this request/response.
let res = http.get("https://example.com/", { jar: jar });
// You can create multiple cookie jars, and switch which one is "active" for a particular requests
let jar2 = new http.CookieJar([
{ name: "name2", value: "value2", domain: "example.com", path: "/" }
]);
http.get("https://example.com/", { jar: jar2 });
} Per-request cookie overrides: export default function() {
// Simple key/value setting of cookies, will be added to outgoing request
// along with cookies from active jar (but cookie specified with same name as
// cookie in jar won't override, just be appended)
let res = http.get("https://example.com/", { cookies: { name: "value", name2: "value2" } });
// Set cookie with option to replace any existing cookie in jar with same name
let res = http.get("https://example.com/", { cookies: { name: { value: "name", replace: true }, name2: "value2" } });
} |
Sounds good! Just two things:
|
f0a2d0d
to
caabac6
Compare
@liclac @ppcano @ragnarlonn I've now updated the PR according to agreed changes above. Some changes:
If anyone has other suggestions for how to name these functions then please let me know. Note: one test is failing as it depends on #324. |
I like this. I'm just gonna hold off on merging it until #324 is merged, since the API on that one is still in flux. |
caabac6
to
8e6cc17
Compare
@liclac Have now:
|
This PR adds an easier to use (than header manipulation) cookie interface to the JS API, implementing #68.
The API has slightly changed from #68. It now looks like this:
The accessor interface returns a map of all currently known cookies for the requested URL (those that are in the VU cookie jar) with cookie name as the key and an array of cookie values (to support multiple cookies with same name).
The cookie objects that are used in the "advanced" setter interface can contain the the following properties:
name
value
domain
path
expires
max_age
secure
http_only