xhr: fix unit tests for 0.12.1

This commit is contained in:
Pierre Tachoire
2024-06-18 09:11:56 +02:00
parent 25ee34e65d
commit 9cdf1f5762
6 changed files with 24 additions and 17 deletions

View File

@@ -25,7 +25,7 @@ const Case = jsruntime.test_utils.Case;
const checkCases = jsruntime.test_utils.checkCases;
const parser = @import("netsurf");
const event_handler = @import("../events/event.zig").event_handler;
const EventHandler = @import("../events/event.zig").EventHandler;
const DOMException = @import("exceptions.zig").DOMException;
const Nod = @import("node.zig");
@@ -75,7 +75,7 @@ pub const EventTarget = struct {
eventType,
cbk,
capture orelse false,
event_handler,
EventHandler,
);
}

View File

@@ -240,7 +240,7 @@ pub fn testExecFn(
try checkCases(js_env, &remove);
}
pub const event_handler = struct {
pub const EventHandler = struct {
fn handle(event: ?*parser.Event, data: ?*anyopaque) callconv(.C) void {
if (data) |d| {
const func = parser.event_handler_cbk(d);
@@ -252,9 +252,9 @@ pub const event_handler = struct {
if (event) |evt| {
func.trycall(.{
Event.toInterface(evt) catch unreachable,
}, &res) catch {};
}, &res) catch |e| log.err("event handler error: {any}", .{e});
} else {
func.trycall(.{event}, &res) catch {};
func.trycall(.{event}, &res) catch |e| log.err("event handler error: {any}", .{e});
}
// in case of function error, we log the result and the trace.

View File

@@ -608,13 +608,15 @@ pub fn eventTargetHasListener(
return null;
}
const EventHandler = fn (event: ?*Event, data: ?*anyopaque) callconv(.C) void;
pub fn eventTargetAddEventListener(
et: *EventTarget,
alloc: std.mem.Allocator,
typ: []const u8,
cbk: Callback,
capture: bool,
handler: anytype,
handler: EventHandler,
) !void {
// this allocation will be removed either on
// eventTargetRemoveEventListener or eventTargetRemoveAllEventListeners

View File

@@ -105,7 +105,7 @@ pub const URL = struct {
var q = std.ArrayList(u8).init(alloc);
defer q.deinit();
try self.search_params.values.encode(q.writer());
self.uri.query = .{ .raw = q.items };
self.uri.query = .{ .percent_encoded = q.items };
return try self.format(alloc);
}

View File

@@ -22,7 +22,7 @@ const jsruntime = @import("jsruntime");
const Callback = jsruntime.Callback;
const EventTarget = @import("../dom/event_target.zig").EventTarget;
const event_handler = @import("../events/event.zig").event_handler;
const EventHandler = @import("../events/event.zig").EventHandler;
const parser = @import("netsurf");
@@ -54,7 +54,7 @@ pub const XMLHttpRequestEventTarget = struct {
typ,
cbk,
false,
event_handler,
EventHandler,
);
}
fn unregister(self: *XMLHttpRequestEventTarget, alloc: std.mem.Allocator, typ: []const u8, cbk: Callback) !void {

View File

@@ -118,7 +118,12 @@ pub const XMLHttpRequest = struct {
// https://lightpanda.slack.com/archives/C05TRU6RBM1/p1707819010681019
// upload: ?XMLHttpRequestUpload = null,
timeout: u32 = 0,
// TODO uncomment this field causes casting issue with
// XMLHttpRequestEventTarget. I think it's dueto an alignement issue, but
// not sure. see
// https://lightpanda.slack.com/archives/C05TRU6RBM1/p1707819010681019
// timeout: u32 = 0,
withCredentials: bool = false,
// TODO: response readonly attribute any response;
response_bytes: ?[]const u8 = null,
@@ -195,7 +200,7 @@ pub const XMLHttpRequest = struct {
fn has(self: Headers, k: []const u8) bool {
for (self.list.items) |h| {
if (std.ascii.eqlIgnoreCase(k, h.value)) {
if (std.ascii.eqlIgnoreCase(k, h.name)) {
return true;
}
}
@@ -205,7 +210,7 @@ pub const XMLHttpRequest = struct {
fn getFirstValue(self: Headers, k: []const u8) ?[]const u8 {
for (self.list.items) |h| {
if (std.ascii.eqlIgnoreCase(k, h.value)) {
if (std.ascii.eqlIgnoreCase(k, h.name)) {
return h.value;
}
}
@@ -216,7 +221,7 @@ pub const XMLHttpRequest = struct {
// replace any existing header with the same key
fn set(self: *Headers, k: []const u8, v: []const u8) !void {
for (self.list.items, 0..) |h, i| {
if (std.ascii.eqlIgnoreCase(k, h.value)) {
if (std.ascii.eqlIgnoreCase(k, h.name)) {
const hh = self.list.swapRemove(i);
self.alloc.free(hh.name);
self.alloc.free(hh.value);
@@ -330,16 +335,16 @@ pub const XMLHttpRequest = struct {
return self.state;
}
pub fn get_timeout(self: *XMLHttpRequest) u32 {
return self.timeout;
pub fn get_timeout(_: *XMLHttpRequest) u32 {
return 0;
}
pub fn set_timeout(self: *XMLHttpRequest, timeout: u32) !void {
// TODO, the value is ignored for now.
pub fn set_timeout(_: *XMLHttpRequest, _: u32) !void {
// TODO If the current global object is a Window object and thiss
// synchronous flag is set, then throw an "InvalidAccessError"
// DOMException.
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-timeout
self.timeout = timeout;
}
pub fn get_withCredentials(self: *XMLHttpRequest) bool {