authorgravatar for karlseguin@users.noreply.github.comKarl Seguin <karlseguin@users.noreply.github.com> 2023-10-31 04:35:24+08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-10-30 20:35:24+00:00
logf52a228a5db89386e4ec0ef40370e53d99be8666
treef921fb959ab291f57d18aa25e2c480dcb03cf558
parent10d03acdb5ff81c112280854629c9f9032e14330
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Fix http.Headers.initList

Use correct return type (error union), cast field length for hashmap capacity.

1 files changed, 18 insertions(+), 2 deletions(-)

lib/std/http/Headers.zig+18-2
...@@ -60,11 +60,11 @@ pub const Headers = struct {...@@ -60,11 +60,11 @@ pub const Headers = struct {
60 return .{ .allocator = allocator };60 return .{ .allocator = allocator };
61 }61 }
6262
63 pub fn initList(allocator: Allocator, list: []const Field) Headers {63 pub fn initList(allocator: Allocator, list: []const Field) !Headers {
64 var new = Headers.init(allocator);64 var new = Headers.init(allocator);
6565
66 try new.list.ensureTotalCapacity(allocator, list.len);66 try new.list.ensureTotalCapacity(allocator, list.len);
67 try new.index.ensureTotalCapacity(allocator, list.len);67 try new.index.ensureTotalCapacity(allocator, @intCast(list.len));
68 for (list) |field| {68 for (list) |field| {
69 try new.append(field.name, field.value);69 try new.append(field.name, field.value);
70 }70 }
...@@ -464,3 +464,19 @@ test "Headers.clearRetainingCapacity and clearAndFree" {...@@ -464,3 +464,19 @@ test "Headers.clearRetainingCapacity and clearAndFree" {
464 try testing.expectEqual(@as(usize, 0), h.list.capacity);464 try testing.expectEqual(@as(usize, 0), h.list.capacity);
465 try testing.expectEqual(@as(usize, 0), h.index.capacity());465 try testing.expectEqual(@as(usize, 0), h.index.capacity());
466}466}
467
468test "Headers.initList" {
469 var h = try Headers.initList(std.testing.allocator, &.{
470 .{ .name = "Accept-Encoding", .value = "gzip" },
471 .{ .name = "Authorization", .value = "it's over 9000!" },
472 });
473 defer h.deinit();
474
475 const encoding_values = (try h.getValues(testing.allocator, "Accept-Encoding")).?;
476 defer testing.allocator.free(encoding_values);
477 try testing.expectEqualDeep(@as([]const []const u8, &[_][]const u8{"gzip"}), encoding_values);
478
479 const authorization_values = (try h.getValues(testing.allocator, "Authorization")).?;
480 defer testing.allocator.free(authorization_values);
481 try testing.expectEqualDeep(@as([]const []const u8, &[_][]const u8{"it's over 9000!"}), authorization_values);
482}