Compare commits
2 commits
2fab8ac0b8
...
cfc8aee1a6
Author | SHA1 | Date | |
---|---|---|---|
cfc8aee1a6 | |||
214c580db4 |
2 changed files with 145 additions and 6 deletions
|
@ -262,9 +262,6 @@ pub const AwsHttp = struct {
|
||||||
log.debug("Request url: {s}", .{url});
|
log.debug("Request url: {s}", .{url});
|
||||||
// TODO: Fix this proxy stuff. This is all a kludge just to compile, but std.http.Client has it all built in now
|
// TODO: Fix this proxy stuff. This is all a kludge just to compile, but std.http.Client has it all built in now
|
||||||
var cl = std.http.Client{ .allocator = self.allocator, .https_proxy = if (self.proxy) |*p| @constCast(p) else null };
|
var cl = std.http.Client{ .allocator = self.allocator, .https_proxy = if (self.proxy) |*p| @constCast(p) else null };
|
||||||
// Not sure this if statement is correct here. deinit seems to assume
|
|
||||||
// that client.request was called at least once, but we don't do that
|
|
||||||
// if we're in a test harness
|
|
||||||
defer cl.deinit(); // TODO: Connection pooling
|
defer cl.deinit(); // TODO: Connection pooling
|
||||||
const method = std.meta.stringToEnum(std.http.Method, request_cp.method).?;
|
const method = std.meta.stringToEnum(std.http.Method, request_cp.method).?;
|
||||||
|
|
||||||
|
@ -289,7 +286,7 @@ pub const AwsHttp = struct {
|
||||||
try m.request(method, uri, req_options) // This will call the test harness
|
try m.request(method, uri, req_options) // This will call the test harness
|
||||||
else
|
else
|
||||||
try cl.request(method, uri, req_options);
|
try cl.request(method, uri, req_options);
|
||||||
|
defer req.deinit();
|
||||||
// TODO: Need to test for payloads > 2^14. I believe one of our tests does this, but not sure
|
// TODO: Need to test for payloads > 2^14. I believe one of our tests does this, but not sure
|
||||||
// if (request_cp.body.len > 0) {
|
// if (request_cp.body.len > 0) {
|
||||||
// // Workaround for https://github.com/ziglang/zig/issues/15626
|
// // Workaround for https://github.com/ziglang/zig/issues/15626
|
||||||
|
@ -318,6 +315,7 @@ pub const AwsHttp = struct {
|
||||||
try req.sendBodyComplete(req_body);
|
try req.sendBodyComplete(req_body);
|
||||||
} else if (options.mock == null) try req.sendBodiless();
|
} else if (options.mock == null) try req.sendBodiless();
|
||||||
|
|
||||||
|
// if (options.mock == null) log.err("Request sent. Body len {d}, uri {f}", .{ request_cp.body.len, uri });
|
||||||
var response = if (options.mock) |m| try m.receiveHead() else try req.receiveHead(&.{});
|
var response = if (options.mock) |m| try m.receiveHead() else try req.receiveHead(&.{});
|
||||||
|
|
||||||
// TODO: Timeout - is this now above us?
|
// TODO: Timeout - is this now above us?
|
||||||
|
|
145
src/aws_test.zig
145
src/aws_test.zig
|
@ -5,6 +5,8 @@ const date = @import("date");
|
||||||
const json = @import("json");
|
const json = @import("json");
|
||||||
|
|
||||||
const aws = @import("aws.zig");
|
const aws = @import("aws.zig");
|
||||||
|
const aws_auth = @import("aws_authentication.zig");
|
||||||
|
const aws_creds = @import("aws_credentials.zig");
|
||||||
const awshttp = @import("aws_http.zig");
|
const awshttp = @import("aws_http.zig");
|
||||||
|
|
||||||
const Services = aws.Services;
|
const Services = aws.Services;
|
||||||
|
@ -314,8 +316,6 @@ const TestSetup = struct {
|
||||||
};
|
};
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
const aws_creds = @import("aws_credentials.zig");
|
|
||||||
const aws_auth = @import("aws_authentication.zig");
|
|
||||||
const signing_time =
|
const signing_time =
|
||||||
date.dateTimeToTimestamp(
|
date.dateTimeToTimestamp(
|
||||||
date.parseIso8601ToDateTime("20230908T170252Z") catch @compileError("Cannot parse date"),
|
date.parseIso8601ToDateTime("20230908T170252Z") catch @compileError("Cannot parse date"),
|
||||||
|
@ -477,6 +477,7 @@ const TestSetup = struct {
|
||||||
self.allocator.destroy(self.call_options);
|
self.allocator.destroy(self.call_options);
|
||||||
self.call_options = undefined;
|
self.call_options = undefined;
|
||||||
self.allocator.destroy(self);
|
self.allocator.destroy(self);
|
||||||
|
aws_creds.static_credentials = null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
test "query_no_input: sts getCallerIdentity comptime" {
|
test "query_no_input: sts getCallerIdentity comptime" {
|
||||||
|
@ -1303,3 +1304,143 @@ test "jsonStringify nullable object" {
|
||||||
try std.testing.expectEqualStrings("bar", json_parsed.value.CiphertextBlob);
|
try std.testing.expectEqualStrings("bar", json_parsed.value.CiphertextBlob);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "works against a live server" {
|
||||||
|
const Server = struct {
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
ready: std.Thread.Semaphore = .{},
|
||||||
|
requests_received: usize = 0,
|
||||||
|
thread: ?std.Thread = null,
|
||||||
|
listening_uri: []const u8 = undefined,
|
||||||
|
|
||||||
|
const Server = @This();
|
||||||
|
|
||||||
|
const response =
|
||||||
|
\\{"GetCallerIdentityResponse":{"GetCallerIdentityResult":{"Account":"123456789012","Arn":"arn:aws:iam::123456789012:user/admin","UserId":"AIDAYAM4POHXHRVANDQBQ"},"ResponseMetadata":{"RequestId":"8f0d54da-1230-40f7-b4ac-95015c4b84cd"}}}
|
||||||
|
;
|
||||||
|
const server_response_headers: []const std.http.Header = &[_]std.http.Header{
|
||||||
|
.{ .name = "Content-Type", .value = "application/json" },
|
||||||
|
.{ .name = "x-amzn-RequestId", .value = "8f0d54da-1230-40f7-b4ac-95015c4b84cd" },
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn start(self: *Server) !void {
|
||||||
|
if (self.thread) |_| return error.ThreadAlreadyStarted;
|
||||||
|
self.thread = try std.Thread.spawn(
|
||||||
|
.{},
|
||||||
|
threadMain,
|
||||||
|
.{self},
|
||||||
|
);
|
||||||
|
try self.ready.timedWait(1000 * std.time.ns_per_ms);
|
||||||
|
awshttp.endpoint_override = self.listening_uri;
|
||||||
|
if (awshttp.endpoint_override == null) return error.TestSetupStartFailure;
|
||||||
|
std.log.debug("endpoint override set to {?s}", .{awshttp.endpoint_override});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stop(self: *Server) !void {
|
||||||
|
if (self.thread == null) return; // thread not started, nothing to do
|
||||||
|
// post stop message
|
||||||
|
var client = std.http.Client{ .allocator = self.allocator };
|
||||||
|
_ = try client.fetch(.{ // we ignore return because that should just shut down
|
||||||
|
.method = .POST,
|
||||||
|
.payload = "quit",
|
||||||
|
.location = .{ .url = self.listening_uri },
|
||||||
|
});
|
||||||
|
awshttp.endpoint_override = null;
|
||||||
|
self.thread.?.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn threadMain(self: *Server) !void {
|
||||||
|
const address = try std.net.Address.parseIp("127.0.0.1", 0);
|
||||||
|
var server = try address.listen(.{});
|
||||||
|
defer server.deinit();
|
||||||
|
const server_port = server.listen_address.in.getPort();
|
||||||
|
self.listening_uri = try std.fmt.allocPrint(self.allocator, "http://127.0.0.1:{d}", .{server_port});
|
||||||
|
defer {
|
||||||
|
self.allocator.free(self.listening_uri);
|
||||||
|
self.listening_uri = undefined;
|
||||||
|
}
|
||||||
|
self.ready.post();
|
||||||
|
while (true) {
|
||||||
|
var connection = try server.accept();
|
||||||
|
defer connection.stream.close();
|
||||||
|
var recv_buffer: [4000]u8 = undefined;
|
||||||
|
var send_buffer: [4000]u8 = undefined;
|
||||||
|
var conn_reader = connection.stream.reader(&recv_buffer);
|
||||||
|
var conn_writer = connection.stream.writer(&send_buffer);
|
||||||
|
var http_server = std.http.Server.init(conn_reader.interface(), &conn_writer.interface);
|
||||||
|
while (http_server.reader.state == .ready) {
|
||||||
|
var req = try http_server.receiveHead();
|
||||||
|
if (req.head.content_length) |l| {
|
||||||
|
if (l == "quit".len) {
|
||||||
|
try req.respond("okie dokie", .{ .keep_alive = false });
|
||||||
|
return; // We're done here
|
||||||
|
}
|
||||||
|
if (l == 43) {
|
||||||
|
// log.err("Got Request", .{});
|
||||||
|
self.requests_received += 1;
|
||||||
|
try req.respond(response, .{
|
||||||
|
.extra_headers = server_response_headers,
|
||||||
|
.keep_alive = false,
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return error.UnexpectedRequest;
|
||||||
|
}
|
||||||
|
return error.MustHaveBodyLength43;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
var server = Server{ .allocator = allocator };
|
||||||
|
try server.start();
|
||||||
|
var stopped = false;
|
||||||
|
defer if (!stopped) server.stop() catch log.err("error stopping server", .{});
|
||||||
|
|
||||||
|
// {
|
||||||
|
// // plain request to see if this is working generally
|
||||||
|
// var client = std.http.Client{ .allocator = allocator };
|
||||||
|
// _ = try client.fetch(.{ // we ignore return because that should just shut down
|
||||||
|
// .method = .GET,
|
||||||
|
// .location = .{ .url = server.listening_uri },
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// try server.stop();
|
||||||
|
// stopped = true;
|
||||||
|
// try std.testing.expectEqual(@as(usize, 1), server.requests_received);
|
||||||
|
// if (true) return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
const sts = (Services(.{.sts}){}).sts;
|
||||||
|
const client = aws.Client.init(std.testing.allocator, .{});
|
||||||
|
const creds = aws_auth.Credentials.init(
|
||||||
|
allocator,
|
||||||
|
try allocator.dupe(u8, "ACCESS"),
|
||||||
|
try allocator.dupe(u8, "SECRET"),
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
aws_creds.static_credentials = creds;
|
||||||
|
defer aws_creds.static_credentials = null;
|
||||||
|
const call = try aws.Request(sts.get_caller_identity).call(.{}, .{ .client = client });
|
||||||
|
// const call = try client.call(services.sts.get_caller_identity.Request{}, options);
|
||||||
|
defer call.deinit();
|
||||||
|
try server.stop();
|
||||||
|
stopped = true;
|
||||||
|
// Request expectations
|
||||||
|
try std.testing.expectEqual(@as(usize, 1), server.requests_received);
|
||||||
|
// if (test_harness.request_actuals == null) return error.NoCallMade;
|
||||||
|
// const req_actuals = test_harness.request_actuals.?;
|
||||||
|
// try std.testing.expectEqual(std.http.Method.POST, req_actuals.request.method);
|
||||||
|
// try std.testing.expectEqualStrings("https://sts.us-west-2.amazonaws.com/", req_actuals.request_uri);
|
||||||
|
// try std.testing.expectEqualStrings(
|
||||||
|
// \\Action=GetCallerIdentity&Version=2011-06-15
|
||||||
|
// , req_actuals.body.?);
|
||||||
|
// Response expectations
|
||||||
|
try std.testing.expectEqualStrings(
|
||||||
|
"arn:aws:iam::123456789012:user/admin",
|
||||||
|
call.response.arn.?,
|
||||||
|
);
|
||||||
|
try std.testing.expectEqualStrings("AIDAYAM4POHXHRVANDQBQ", call.response.user_id.?);
|
||||||
|
try std.testing.expectEqualStrings("123456789012", call.response.account.?);
|
||||||
|
try std.testing.expectEqualStrings("8f0d54da-1230-40f7-b4ac-95015c4b84cd", call.response_metadata.request_id);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue