mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-06-11 01:25:53 -04:00
Merge pull request #2456 from lightpanda-io/cdp-proper-cache-disable
properly disable cache on `Network.setCacheDisabled`
This commit is contained in:
@@ -55,7 +55,7 @@ pub fn processMessage(cmd: *CDP.Command) !void {
|
||||
switch (action) {
|
||||
.enable => return enable(cmd),
|
||||
.disable => return disable(cmd),
|
||||
.setCacheDisabled => return cmd.sendResult(null, .{}),
|
||||
.setCacheDisabled => return setCacheDisabled(cmd),
|
||||
.setUserAgentOverride => return @import("emulation.zig").setUserAgentOverride(cmd),
|
||||
.setExtraHTTPHeaders => return setExtraHTTPHeaders(cmd),
|
||||
.deleteCookies => return deleteCookies(cmd),
|
||||
@@ -82,6 +82,17 @@ fn disable(cmd: *CDP.Command) !void {
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn setCacheDisabled(cmd: *CDP.Command) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
cacheDisabled: bool,
|
||||
})) orelse return error.InvalidParams;
|
||||
|
||||
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
|
||||
const client = &bc.cdp.browser.http_client;
|
||||
client.cache_layer.disabled = params.cacheDisabled;
|
||||
return cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
fn setExtraHTTPHeaders(cmd: *CDP.Command) !void {
|
||||
const params = (try cmd.params(struct {
|
||||
headers: std.json.ArrayHashMap([]const u8),
|
||||
@@ -734,3 +745,42 @@ test "cdp.Network: canClearBrowserCache" {
|
||||
// Cache is disabled in standard tests for now.
|
||||
try ctx.expectSentResult(.{ .result = false }, .{ .id = 1 });
|
||||
}
|
||||
|
||||
test "cdp.Network: setCacheDisabled disables cache" {
|
||||
var ctx = try testing.context();
|
||||
defer ctx.deinit();
|
||||
_ = try ctx.loadBrowserContext(.{ .id = "BID-CD1" });
|
||||
|
||||
try ctx.processMessage(.{
|
||||
.id = 1,
|
||||
.method = "Network.setCacheDisabled",
|
||||
.params = .{ .cacheDisabled = true },
|
||||
});
|
||||
try ctx.expectSentResult(null, .{ .id = 1 });
|
||||
|
||||
const client = ctx.cdp().browser.http_client;
|
||||
try testing.expectEqual(true, client.cache_layer.disabled);
|
||||
}
|
||||
|
||||
test "cdp.Network: setCacheDisabled re-enables cache" {
|
||||
var ctx = try testing.context();
|
||||
defer ctx.deinit();
|
||||
_ = try ctx.loadBrowserContext(.{ .id = "BID-CD2" });
|
||||
|
||||
try ctx.processMessage(.{
|
||||
.id = 1,
|
||||
.method = "Network.setCacheDisabled",
|
||||
.params = .{ .cacheDisabled = true },
|
||||
});
|
||||
try ctx.expectSentResult(null, .{ .id = 1 });
|
||||
|
||||
try ctx.processMessage(.{
|
||||
.id = 2,
|
||||
.method = "Network.setCacheDisabled",
|
||||
.params = .{ .cacheDisabled = false },
|
||||
});
|
||||
try ctx.expectSentResult(null, .{ .id = 2 });
|
||||
|
||||
const client = ctx.cdp().browser.http_client;
|
||||
try testing.expectEqual(false, client.cache_layer.disabled);
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ const log = lp.log;
|
||||
const CacheLayer = @This();
|
||||
|
||||
next: Layer = undefined,
|
||||
disabled: bool = false,
|
||||
|
||||
pub fn layer(self: *CacheLayer) Layer {
|
||||
return .{
|
||||
@@ -50,7 +51,7 @@ fn request(ptr: *anyopaque, transfer: *Transfer) anyerror!void {
|
||||
const self: *CacheLayer = @ptrCast(@alignCast(ptr));
|
||||
const req = &transfer.req;
|
||||
|
||||
if (req.params.method != .GET) {
|
||||
if (self.disabled or req.params.method != .GET) {
|
||||
return self.next.request(transfer);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user