| ... | @@ -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 | } |
| 62 | | 62 | |
| 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); |
| 65 | | 65 | |
| 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 | |
| | 468 | test "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 | } |