mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 14:25:16 +00:00
Fix http.Headers.initList
Use correct return type (error union), cast field length for hashmap capacity.
This commit is contained in:
parent
10d03acdb5
commit
f52a228a5d
@ -60,11 +60,11 @@ pub const Headers = struct {
|
||||
return .{ .allocator = allocator };
|
||||
}
|
||||
|
||||
pub fn initList(allocator: Allocator, list: []const Field) Headers {
|
||||
pub fn initList(allocator: Allocator, list: []const Field) !Headers {
|
||||
var new = Headers.init(allocator);
|
||||
|
||||
try new.list.ensureTotalCapacity(allocator, list.len);
|
||||
try new.index.ensureTotalCapacity(allocator, list.len);
|
||||
try new.index.ensureTotalCapacity(allocator, @intCast(list.len));
|
||||
for (list) |field| {
|
||||
try new.append(field.name, field.value);
|
||||
}
|
||||
@ -464,3 +464,19 @@ test "Headers.clearRetainingCapacity and clearAndFree" {
|
||||
try testing.expectEqual(@as(usize, 0), h.list.capacity);
|
||||
try testing.expectEqual(@as(usize, 0), h.index.capacity());
|
||||
}
|
||||
|
||||
test "Headers.initList" {
|
||||
var h = try Headers.initList(std.testing.allocator, &.{
|
||||
.{ .name = "Accept-Encoding", .value = "gzip" },
|
||||
.{ .name = "Authorization", .value = "it's over 9000!" },
|
||||
});
|
||||
defer h.deinit();
|
||||
|
||||
const encoding_values = (try h.getValues(testing.allocator, "Accept-Encoding")).?;
|
||||
defer testing.allocator.free(encoding_values);
|
||||
try testing.expectEqualDeep(@as([]const []const u8, &[_][]const u8{"gzip"}), encoding_values);
|
||||
|
||||
const authorization_values = (try h.getValues(testing.allocator, "Authorization")).?;
|
||||
defer testing.allocator.free(authorization_values);
|
||||
try testing.expectEqualDeep(@as([]const []const u8, &[_][]const u8{"it's over 9000!"}), authorization_values);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user