jetzig/demo/src/app/views/format.zig
2025-05-17 17:19:52 +08:00

52 lines
1.5 KiB
Zig

const std = @import("std");
const jetzig = @import("jetzig");
// Define `pub const formats` to apply constraints to specific view functions. By default, all
// view functions respond to `json` and `html` requests. Use this feature to override those
// defaults.
pub const formats: jetzig.Route.Formats = .{
.index = &.{ .json, .html },
.get = &.{.html},
};
pub fn index(request: *jetzig.Request) !jetzig.View {
return request.render(.ok);
}
pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
_ = id;
return request.render(.ok);
}
test "index (json)" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const response = try app.request(.GET, "/format.json", .{});
try response.expectStatus(.ok);
}
test "index (html)" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const response = try app.request(.GET, "/format.html", .{});
try response.expectStatus(.ok);
}
test "get (html)" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const response = try app.request(.GET, "/format/example-id.html", .{});
try response.expectStatus(.ok);
}
test "get (json)" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const response = try app.request(.GET, "/format/example-id.json", .{});
try response.expectStatus(.not_found);
}