Skip to content

Commit

Permalink
main: ci skip: http, base64 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flowerinthenight committed Sep 21, 2024
1 parent 9083e42 commit 1307920
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 32 deletions.
175 changes: 144 additions & 31 deletions src/scratch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,6 @@ test "atomic" {
dbg("took {any}\n", .{std.fmt.fmtDuration(tm.read())});
}

test "block" {
const flag = false;
section: {
if (flag) {
dbg("early return\n", .{});
break :section;
}

dbg("final return\n", .{});
}
}

test "tuple" {
var tuple: std.meta.Tuple(&.{ u32, bool }) = .{ 100, true };
dbg("{any}\n", .{tuple.len});
tuple[0] = 200;
dbg("{any}, {d}\n", .{ tuple.len, tuple[0] });
}

test "dupe" {
const alloc = std.testing.allocator;
const m1 = try std.fmt.allocPrint(alloc, "zig is the man", .{});
defer alloc.free(m1);
const dup1 = try alloc.dupe(u8, m1);
defer alloc.free(dup1);
const dup2 = try alloc.dupe(u8, m1);
defer alloc.free(dup2);
dbg("{s},{d}\n", .{ dup1, dup1.len });
dbg("{s},{d}\n", .{ dup2, dup2.len });
}

test "view" {
const en = enum(u4) {
change,
Expand All @@ -108,3 +77,147 @@ test "view" {
dbg("val={x}\n", .{val & 0x0fffffffffffffff});
dbg("{x}\n", .{0xffffffffffffffff & (0b11 << 62)});
}

test "httpget" {
var parent = std.heap.ArenaAllocator.init(std.testing.allocator);
defer parent.deinit();
const arena = parent.allocator();

var client = std.http.Client{ .allocator = arena };
defer client.deinit();

const endpoint = "https://keyvalue.immanuel.co/api/KeyVal/GetValue/seegmed7/chew";
const uri = try std.Uri.parse(endpoint);

const server_header_buffer: []u8 = try arena.alloc(u8, 8 * 1024 * 4);
var req = try client.open(.GET, uri, std.http.Client.RequestOptions{
.server_header_buffer = server_header_buffer,
});

defer req.deinit();

try req.send();
try req.finish();
try req.wait();

const repstr = try req.reader().readAllAlloc(arena, std.math.maxInt(usize));

dbg("reply={s}\n", .{repstr});
}

test "httppost" {
var parent = std.heap.ArenaAllocator.init(std.testing.allocator);
defer parent.deinit();
const arena = parent.allocator();

var client = std.http.Client{ .allocator = arena };
defer client.deinit();

const endpoint = "https://keyvalue.immanuel.co/api/KeyVal/UpdateValue/seegmed7/chew/something";
const uri = try std.Uri.parse(endpoint);

const server_header_buffer: []u8 = try arena.alloc(u8, 8 * 1024 * 4);
var req = try client.open(.POST, uri, std.http.Client.RequestOptions{
.server_header_buffer = server_header_buffer,
.extra_headers = &[_]std.http.Header{.{ .name = "content-length", .value = "9" }},
});

defer req.deinit();

try req.send();
try req.finish();
try req.wait();

const repstr = try req.reader().readAllAlloc(arena, std.math.maxInt(usize));

dbg("reply={s}\n", .{repstr});
}

test "httpfetch" {
var parent = std.heap.ArenaAllocator.init(std.testing.allocator);
defer parent.deinit();
const arena = parent.allocator();

var client = std.http.Client{ .allocator = arena };
defer client.deinit();

// https://api.keyval.org/get/chew
// const endpoint = "https://keyvalue.immanuel.co/api/KeyVal/UpdateValue/seegmed7/chew/something";
const endpoint = "https://api.keyval.org/set/chew/bloodboil";
const uri = try std.Uri.parse(endpoint);

var response_body = std.ArrayList(u8).init(arena);

const response = try client.fetch(std.http.Client.FetchOptions{
.method = std.http.Method.POST,
.location = .{ .uri = uri },
// .extra_headers = &[_]std.http.Header{.{ .name = "Content-Length", .value = "9" }},
.response_storage = .{ .dynamic = &response_body },
});

if (response.status != .ok) dbg("booooooo\n", .{});

const parsed_body = try response_body.toOwnedSlice();
dbg("RESPONSE: {s}\n", .{parsed_body});
}

test "execpost" {
var tm = try std.time.Timer.start();
defer dbg("took {any}", .{std.fmt.fmtDuration(tm.read())});
const allocator = std.testing.allocator;
const result = try std.process.Child.run(.{
.allocator = allocator,
.argv = &[_][]const u8{
"curl",
"-X",
"POST",
"-H",
"Content-Length: 1",
"https://keyvalue.immanuel.co/api/KeyVal/UpdateValue/seegmed7/chew/MTI3LjAuMC4xOjgwODA=",
},
});

defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}

dbg("stdout: {s}\n", .{result.stdout});
}

test "execget" {
var tm = try std.time.Timer.start();
defer dbg("took {any}", .{std.fmt.fmtDuration(tm.read())});
const allocator = std.testing.allocator;
const result = try std.process.Child.run(.{
.allocator = allocator,
.argv = &[_][]const u8{
"curl",
"https://keyvalue.immanuel.co/api/KeyVal/GetValue/seegmed7/chew",
},
});

defer {
allocator.free(result.stdout);
allocator.free(result.stderr);
}

dbg("stdout: {s}\n", .{result.stdout});
}

test "base64" {
var tm = try std.time.Timer.start();
defer dbg("took {any}", .{std.fmt.fmtDuration(tm.read())});
const src = "127.0.0.1:8080";
const enc = std.base64.Base64Encoder.init(std.base64.url_safe_alphabet_chars, '=');
const buf = try std.testing.allocator.alloc(u8, enc.calcSize(src.len));
defer std.testing.allocator.free(buf);
const out = enc.encode(buf, src);
dbg("enc={s}\n", .{out});

const dec = std.base64.Base64Decoder.init(std.base64.url_safe_alphabet_chars, '=');
const buf2 = try std.testing.allocator.alloc(u8, try dec.calcSizeUpperBound(out.len));
defer std.testing.allocator.free(buf2);
try dec.decode(buf2, out);
dbg("dec={s}\n", .{buf2});
}
2 changes: 1 addition & 1 deletion src/zgroup.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! It is based on the SWIM Protocol, specifically, SWIM+Inf.+Sus. variant of the gossip
//! protocol. Linux-only for now.
//!
//! Ref: https://www.cs.cornell.edu/projects/Quicksilver/public_pdfs/SWIM.pdf
//! Reference: https://www.cs.cornell.edu/projects/Quicksilver/public_pdfs/SWIM.pdf
//!
const std = @import("std");
const backoff = @import("zbackoff");
Expand Down

0 comments on commit 1307920

Please sign in to comment.