Skip to content

Commit 9f89341

Browse files
committed
CDP Network cookie tests
1 parent 0c030fd commit 9f89341

File tree

1 file changed

+86
-14
lines changed

1 file changed

+86
-14
lines changed

src/cdp/domains/network.zig

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,25 @@ fn cookieMatches(cookie: *const Cookie, name: []const u8, domain: ?[]const u8, p
8989
if (!std.mem.eql(u8, cookie.name, name)) return false;
9090

9191
if (domain) |domain_| {
92-
if (!std.mem.eql(u8, cookie.domain, domain_)) return false;
92+
const c_no_dot = if (std.mem.startsWith(u8, cookie.domain, ".")) cookie.domain[1..] else cookie.domain;
93+
const d_no_dot = if (std.mem.startsWith(u8, domain_, ".")) domain_[1..] else domain_;
94+
if (!std.mem.eql(u8, c_no_dot, d_no_dot)) return false;
9395
}
9496
if (path) |path_| {
9597
if (!std.mem.eql(u8, cookie.path, path_)) return false;
9698
}
9799
return true;
98100
}
99101

100-
// Only matches the cookie on provided parameters
101-
fn cookieAppliesTo(cookie: *const Cookie, domain: []const u8, path: []const u8) bool {
102-
if (!std.mem.eql(u8, cookie.domain, domain)) return false;
103-
return std.mem.startsWith(u8, path, cookie.path);
104-
}
105-
106102
fn deleteCookies(cmd: anytype) !void {
107103
const params = (try cmd.params(struct {
108104
name: []const u8,
109105
url: ?[]const u8 = null,
110106
domain: ?[]const u8 = null,
111107
path: ?[]const u8 = null,
112-
// partitionKey: ?CookiePartitionKey,
108+
partitionKey: ?CdpStorage.CookiePartitionKey = null,
113109
})) orelse return error.InvalidParams;
110+
if (params.partitionKey != null) return error.NotYetImplementedParams;
114111

115112
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
116113
const cookies = &bc.session.cookie_jar.cookies;
@@ -135,11 +132,8 @@ fn deleteCookies(cmd: anytype) !void {
135132
}
136133

137134
fn clearBrowserCookies(cmd: anytype) !void {
138-
_ = (try cmd.params(struct {})) orelse return error.InvalidParams;
139-
140135
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
141136
bc.session.cookie_jar.clearRetainingCapacity();
142-
143137
return cmd.sendResult(null, .{});
144138
}
145139

@@ -170,11 +164,15 @@ fn setCookies(cmd: anytype) !void {
170164
fn getCookies(cmd: anytype) !void {
171165
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
172166
const params = (try cmd.params(struct {
173-
urls: []const []const u8,
167+
urls: ?[]const []const u8 = null,
174168
})) orelse return error.InvalidParams;
175169

176-
var urls = try std.ArrayListUnmanaged(CdpStorage.PreparedUri).initCapacity(cmd.arena, params.urls.len);
177-
for (params.urls) |url| {
170+
// If not specified, use the URLs of the page and all of its subframes. TODO subframes
171+
const page_url = if (bc.session.page) |*page| page.url.raw else null; // @speed: avoid repasing the URL
172+
const param_urls = params.urls orelse &[_][]const u8{page_url orelse return error.InvalidParams};
173+
174+
var urls = try std.ArrayListUnmanaged(CdpStorage.PreparedUri).initCapacity(cmd.arena, param_urls.len);
175+
for (param_urls) |url| {
178176
const uri = std.Uri.parse(url) catch return error.InvalidParams;
179177

180178
urls.appendAssumeCapacity(.{
@@ -354,3 +352,77 @@ test "cdp.network setExtraHTTPHeaders" {
354352
try ctx.processMessage(.{ .id = 5, .method = "Target.attachToTarget", .params = .{ .targetId = bc.target_id.? } });
355353
try testing.expectEqual(bc.cdp.extra_headers.items.len, 0);
356354
}
355+
356+
test "cdp.Network: cookies" {
357+
const ResCookie = CdpStorage.ResCookie;
358+
const CdpCookie = CdpStorage.CdpCookie;
359+
360+
var ctx = testing.context();
361+
defer ctx.deinit();
362+
_ = try ctx.loadBrowserContext(.{ .id = "BID-S" });
363+
364+
// Initially empty
365+
try ctx.processMessage(.{
366+
.id = 3,
367+
.method = "Network.getCookies",
368+
.params = .{ .urls = &[_][]const u8{"https://example.com/pancakes"} },
369+
});
370+
try ctx.expectSentResult(.{ .cookies = &[_]ResCookie{} }, .{ .id = 3 });
371+
372+
// Has cookies after setting them
373+
try ctx.processMessage(.{
374+
.id = 4,
375+
.method = "Network.setCookie",
376+
.params = CdpCookie{ .name = "test3", .value = "valuenot3", .url = "https://car.example.com/defnotpancakes" },
377+
});
378+
try ctx.expectSentResult(null, .{ .id = 4 });
379+
try ctx.processMessage(.{
380+
.id = 5,
381+
.method = "Network.setCookies",
382+
.params = .{
383+
.cookies = &[_]CdpCookie{
384+
.{ .name = "test3", .value = "value3", .url = "https://car.example.com/pan/cakes" },
385+
.{ .name = "test4", .value = "value4", .domain = "example.com", .path = "/mango" },
386+
},
387+
},
388+
});
389+
try ctx.expectSentResult(null, .{ .id = 5 });
390+
try ctx.processMessage(.{
391+
.id = 6,
392+
.method = "Network.getCookies",
393+
.params = .{ .urls = &[_][]const u8{"https://car.example.com/pan/cakes"} },
394+
});
395+
try ctx.expectSentResult(.{
396+
.cookies = &[_]ResCookie{
397+
.{ .name = "test3", .value = "value3", .domain = "car.example.com", .path = "/", .secure = true }, // No Pancakes!
398+
},
399+
}, .{ .id = 6 });
400+
401+
// deleteCookies
402+
try ctx.processMessage(.{
403+
.id = 7,
404+
.method = "Network.deleteCookies",
405+
.params = .{ .name = "test3", .domain = "car.example.com" },
406+
});
407+
try ctx.expectSentResult(null, .{ .id = 7 });
408+
try ctx.processMessage(.{
409+
.id = 8,
410+
.method = "Storage.getCookies",
411+
.params = .{ .browserContextId = "BID-S" },
412+
});
413+
// Just the untouched test4 should be in the result
414+
try ctx.expectSentResult(.{ .cookies = &[_]ResCookie{.{ .name = "test4", .value = "value4", .domain = ".example.com", .path = "/mango" }} }, .{ .id = 8 });
415+
416+
// Empty after clearBrowserCookies
417+
try ctx.processMessage(.{
418+
.id = 9,
419+
.method = "Network.clearBrowserCookies",
420+
});
421+
try ctx.expectSentResult(null, .{ .id = 9 });
422+
try ctx.processMessage(.{
423+
.id = 10,
424+
.method = "Storage.getCookies",
425+
.params = .{ .browserContextId = "BID-S" },
426+
});
427+
try ctx.expectSentResult(.{ .cookies = &[_]ResCookie{} }, .{ .id = 10 });
428+
}

0 commit comments

Comments
 (0)