new server test
This commit is contained in:
parent
c18388c189
commit
1172db2a8a
2 changed files with 22 additions and 9 deletions
|
@ -51,7 +51,10 @@ pub const Server = struct {
|
||||||
// Detect Content-Length for assigning the body
|
// Detect Content-Length for assigning the body
|
||||||
const end_index = try std.fmt.parseUnsigned(u8, req.header("Content-Length").?, 0);
|
const end_index = try std.fmt.parseUnsigned(u8, req.header("Content-Length").?, 0);
|
||||||
req.body = buffer.items[header_index + 4 .. @as(usize, end_index + 1)];
|
req.body = buffer.items[header_index + 4 .. @as(usize, end_index + 1)];
|
||||||
} else break;
|
} else {
|
||||||
|
req.body = "";
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
defer allocator.free(req.headers);
|
defer allocator.free(req.headers);
|
||||||
|
|
|
@ -16,18 +16,28 @@ test "Test zerve test-app, run server and serve test page" {
|
||||||
const rt = [_]types.Route{.{ "/", handlefn }};
|
const rt = [_]types.Route{.{ "/", handlefn }};
|
||||||
try Server.listen("0.0.0.0", 8080, &rt, std.testing.allocator);
|
try Server.listen("0.0.0.0", 8080, &rt, std.testing.allocator);
|
||||||
}
|
}
|
||||||
// Function for test "Run Server"
|
// Function for test
|
||||||
fn handlefn(req: *types.Request) types.Response {
|
fn handlefn(req: *types.Request) types.Response {
|
||||||
// print headers of Request
|
const alloc = std.testing.allocator;
|
||||||
std.debug.print("\nSent headers:\n", .{});
|
// collect headers of Request
|
||||||
|
var headers = std.ArrayList(u8).init(alloc);
|
||||||
|
defer headers.deinit();
|
||||||
for (req.headers) |header| {
|
for (req.headers) |header| {
|
||||||
std.debug.print("{s}: {s}", .{ header.key, header.value });
|
headers.appendSlice(header.key) catch {};
|
||||||
|
headers.appendSlice(": ") catch {};
|
||||||
|
headers.appendSlice(header.value) catch {};
|
||||||
|
headers.appendSlice("\n") catch {};
|
||||||
}
|
}
|
||||||
// print cookies of Request
|
// collect cookies of Request
|
||||||
std.debug.print("\nSent cookies:\n", .{});
|
var cookies = std.ArrayList(u8).init(alloc);
|
||||||
|
defer cookies.deinit();
|
||||||
for (req.cookies) |cookie| {
|
for (req.cookies) |cookie| {
|
||||||
std.debug.print("{s}={s}\n", .{ cookie.name, cookie.value });
|
cookies.appendSlice(cookie.name) catch {};
|
||||||
|
cookies.appendSlice(" = ") catch {};
|
||||||
|
cookies.appendSlice(cookie.value) catch {};
|
||||||
|
cookies.appendSlice("\n") catch {};
|
||||||
}
|
}
|
||||||
var res = types.Response{ .body = "<h1>Run Server Test OK!</h1>", .cookies = &[_]Response.Cookie{.{ .name = "Test-Cookie", .value = "Test", .maxAge = 60 * 60 * 5 }} };
|
const res_string = std.fmt.allocPrint(alloc, "<h1>Run Server Test OK!</h1><br><h3>Sent headers:</h3><br><pre><code>{s}</code></pre><br><h3>Sent Cookies:</h3><br><pre><code>{s}</code></pre><br><h3>Request body:</h3><br>{s}", .{ headers.items, cookies.items, req.body }) catch "Memory error";
|
||||||
|
var res = types.Response{ .body = res_string, .cookies = &[_]Response.Cookie{.{ .name = "Test-Cookie", .value = "Test", .maxAge = 60 * 60 * 5 }} };
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue