From f52a228a5db89386e4ec0ef40370e53d99be8666 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Tue, 31 Oct 2023 04:35:24 +0800 Subject: [PATCH] Fix http.Headers.initList Use correct return type (error union), cast field length for hashmap capacity. --- lib/std/http/Headers.zig | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/std/http/Headers.zig b/lib/std/http/Headers.zig index d2e578b5ee..bf0b0755d6 100644 --- a/lib/std/http/Headers.zig +++ b/lib/std/http/Headers.zig @@ -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); +}