basic/bearer testing

This commit is contained in:
sjorsdonkers
2025-06-25 11:34:32 +02:00
parent 4560f31010
commit aea34264a9
3 changed files with 65 additions and 5 deletions

View File

@@ -59,15 +59,15 @@ pub const ProxyAuth = union(enum) {
var encoder = std.base64.standard.Encoder; var encoder = std.base64.standard.Encoder;
const size = encoder.calcSize(auth.user_pass.len); const size = encoder.calcSize(auth.user_pass.len);
var buffer = try allocator.alloc(u8, size + prefix.len); var buffer = try allocator.alloc(u8, size + prefix.len);
std.mem.copyForwards(u8, buffer, prefix); @memcpy(buffer[0..prefix.len], prefix);
_ = std.base64.standard.Encoder.encode(buffer[prefix.len..], auth.user_pass); _ = std.base64.standard.Encoder.encode(buffer[prefix.len..], auth.user_pass);
return buffer; return buffer;
}, },
.bearer => |*auth| { .bearer => |*auth| {
const prefix = "Bearer "; const prefix = "Bearer ";
var buffer = try allocator.alloc(u8, auth.token.len + prefix.len); var buffer = try allocator.alloc(u8, auth.token.len + prefix.len);
std.mem.copyForwards(u8, buffer, prefix); @memcpy(buffer[0..prefix.len], prefix);
std.mem.copyForwards(u8, buffer[prefix.len..], auth.token); @memcpy(buffer[prefix.len..], auth.token);
return buffer; return buffer;
}, },
} }
@@ -3078,15 +3078,56 @@ test "HttpClient: sync with body proxy CONNECT" {
} }
try testing.expectEqual("over 9000!", try res.next()); try testing.expectEqual("over 9000!", try res.next());
try testing.expectEqual(201, res.header.status); try testing.expectEqual(201, res.header.status);
try testing.expectEqual(5, res.header.count()); try testing.expectEqual(6, res.header.count());
try testing.expectEqual("Close", res.header.get("connection")); try testing.expectEqual("Close", res.header.get("connection"));
try testing.expectEqual("10", res.header.get("content-length")); try testing.expectEqual("10", res.header.get("content-length"));
try testing.expectEqual("127.0.0.1", res.header.get("_host")); try testing.expectEqual("127.0.0.1", res.header.get("_host"));
try testing.expectEqual("Lightpanda/1.0", res.header.get("_user-agent")); try testing.expectEqual("Lightpanda/1.0", res.header.get("_user-agent"));
try testing.expectEqual("*/*", res.header.get("_accept")); try testing.expectEqual("*/*", res.header.get("_accept"));
// Proxy headers
try testing.expectEqual("127.0.0.1:9582", res.header.get("__host"));
} }
} }
test "HttpClient: basic authentication CONNECT" {
const proxy_uri = try Uri.parse("http://127.0.0.1:9582/");
var client = try testClient(.{ .proxy_type = .connect, .http_proxy = proxy_uri, .proxy_auth = .{ .basic = .{ .user_pass = "user:pass" } } });
defer client.deinit();
const uri = try Uri.parse("http://127.0.0.1:9582/http_client/echo");
var req = try client.request(.GET, &uri);
defer req.deinit();
var res = try req.sendSync(.{});
try testing.expectEqual(201, res.header.status);
// Destination headers
try testing.expectEqual(null, res.header.get("_authorization"));
try testing.expectEqual(null, res.header.get("_proxy-authorization"));
// Proxy headers
try testing.expectEqual(null, res.header.get("__authorization"));
try testing.expectEqual("Basic dXNlcjpwYXNz", res.header.get("__proxy-authorization"));
}
test "HttpClient: bearer authentication CONNECT" {
const proxy_uri = try Uri.parse("http://127.0.0.1:9582/");
var client = try testClient(.{ .proxy_type = .connect, .http_proxy = proxy_uri, .proxy_auth = .{ .bearer = .{ .token = "fruitsalad" } } });
defer client.deinit();
const uri = try Uri.parse("http://127.0.0.1:9582/http_client/echo");
var req = try client.request(.GET, &uri);
defer req.deinit();
var res = try req.sendSync(.{});
try testing.expectEqual(201, res.header.status);
// Destination headers
try testing.expectEqual(null, res.header.get("_authorization"));
try testing.expectEqual(null, res.header.get("_proxy-authorization"));
// Proxy headers
try testing.expectEqual(null, res.header.get("__authorization"));
try testing.expectEqual("Bearer fruitsalad", res.header.get("__proxy-authorization"));
}
test "HttpClient: sync with gzip body" { test "HttpClient: sync with gzip body" {
for (0..2) |i| { for (0..2) |i| {
var client = try testClient(.{}); var client = try testClient(.{});

View File

@@ -648,6 +648,7 @@ fn serveHTTP(address: std.net.Address) !void {
var conn = try listener.accept(); var conn = try listener.accept();
defer conn.stream.close(); defer conn.stream.close();
var http_server = std.http.Server.init(conn, &read_buffer); var http_server = std.http.Server.init(conn, &read_buffer);
var connect_headers: std.ArrayListUnmanaged(std.http.Header) = .{};
REQUEST: while (true) { REQUEST: while (true) {
var request = http_server.receiveHead() catch |err| switch (err) { var request = http_server.receiveHead() catch |err| switch (err) {
error.HttpConnectionClosing => continue :ACCEPT, error.HttpConnectionClosing => continue :ACCEPT,
@@ -659,6 +660,16 @@ fn serveHTTP(address: std.net.Address) !void {
if (request.head.method == .CONNECT) { if (request.head.method == .CONNECT) {
try request.respond("", .{ .status = .ok }); try request.respond("", .{ .status = .ok });
// Proxy headers and destination headers are separated in the case of a CONNECT proxy
// We store the CONNECT headers, then continue with the request for the destination
var it = request.iterateHeaders();
while (it.next()) |hdr| {
try connect_headers.append(aa, .{
.name = try std.fmt.allocPrint(aa, "__{s}", .{hdr.name}),
.value = try aa.dupe(u8, hdr.value),
});
}
continue :REQUEST; continue :REQUEST;
} }
@@ -699,6 +710,11 @@ fn serveHTTP(address: std.net.Address) !void {
.value = hdr.value, .value = hdr.value,
}); });
} }
if (connect_headers.items.len > 0) {
try headers.appendSlice(aa, connect_headers.items);
connect_headers.clearRetainingCapacity();
}
try headers.append(aa, .{ .name = "Connection", .value = "Close" }); try headers.append(aa, .{ .name = "Connection", .value = "Close" });
try request.respond("over 9000!", .{ try request.respond("over 9000!", .{

View File

@@ -66,7 +66,10 @@ pub fn expectEqual(expected: anytype, actual: anytype) !void {
if (@typeInfo(@TypeOf(expected)) == .null) { if (@typeInfo(@TypeOf(expected)) == .null) {
return std.testing.expectEqual(null, actual); return std.testing.expectEqual(null, actual);
} }
return expectEqual(expected, actual.?); if (actual) |_actual| {
return expectEqual(expected, _actual);
}
return std.testing.expectEqual(expected, null);
}, },
.@"union" => |union_info| { .@"union" => |union_info| {
if (union_info.tag_type == null) { if (union_info.tag_type == null) {