zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit f52a228a5db89386e4ec0ef40370e53d99be8666 (tree)
parent 10d03acdb5ff81c112280854629c9f9032e14330
Author: Karl Seguin <karlseguin@users.noreply.github.com>
Date:   Tue, 31 Oct 2023 04:35:24 +0800

Fix http.Headers.initList

Use correct return type (error union), cast field length for hashmap capacity.
Diffstat:
Mlib/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 @@ -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); +}