move queryparams test to queryparams. handler now has no reference to allocators
This commit is contained in:
parent
03ca266e1d
commit
05d470871f
3 changed files with 29 additions and 29 deletions
|
|
@ -149,3 +149,25 @@ test "parse transparency" {
|
||||||
const params_custom = try QueryParams.parse(allocator, "transparency=200");
|
const params_custom = try QueryParams.parse(allocator, "transparency=200");
|
||||||
try std.testing.expectEqual(@as(u8, 200), params_custom.transparency.?);
|
try std.testing.expectEqual(@as(u8, 200), params_custom.transparency.?);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "imperial units selection logic" {
|
||||||
|
// This test documents the priority order for unit selection:
|
||||||
|
// 1. Explicit ?u or ?m parameter (highest priority)
|
||||||
|
// 2. lang=us parameter
|
||||||
|
// 3. US IP detection
|
||||||
|
// 4. Default to metric
|
||||||
|
|
||||||
|
// The actual logic is tested through integration tests
|
||||||
|
// This test just verifies the QueryParams parsing works
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
|
||||||
|
const params_u = try QueryParams.parse(allocator, "u");
|
||||||
|
try std.testing.expect(params_u.use_imperial.?);
|
||||||
|
|
||||||
|
const params_m = try QueryParams.parse(allocator, "m");
|
||||||
|
try std.testing.expect(!params_m.use_imperial.?);
|
||||||
|
|
||||||
|
const params_lang = try QueryParams.parse(allocator, "lang=us");
|
||||||
|
defer allocator.free(params_lang.lang.?);
|
||||||
|
try std.testing.expectEqualStrings("us", params_lang.lang.?);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,7 @@ fn handleWeatherInternal(
|
||||||
render_options.use_imperial = true; // this is a US IP
|
render_options.use_imperial = true; // this is a US IP
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add coordinates header using response allocator
|
// Add coordinates header to response
|
||||||
const coords_header = try std.fmt.allocPrint(res.arena, "{d:.4},{d:.4}", .{ location.coords.latitude, location.coords.longitude });
|
const coords_header = try std.fmt.allocPrint(res.arena, "{d:.4},{d:.4}", .{ location.coords.latitude, location.coords.longitude });
|
||||||
res.headers.add("X-Location-Coordinates", coords_header);
|
res.headers.add("X-Location-Coordinates", coords_header);
|
||||||
|
|
||||||
|
|
@ -147,7 +147,7 @@ fn handleWeatherInternal(
|
||||||
res.content_type = .JSON; // reset to json
|
res.content_type = .JSON; // reset to json
|
||||||
try Json.render(res.writer(), weather);
|
try Json.render(res.writer(), weather);
|
||||||
} else if (std.mem.eql(u8, fmt, "p1")) {
|
} else if (std.mem.eql(u8, fmt, "p1")) {
|
||||||
try Prometheus.render(res.writer(), weather, req_alloc);
|
try Prometheus.render(res.writer(), weather);
|
||||||
} else if (std.mem.eql(u8, fmt, "v2")) {
|
} else if (std.mem.eql(u8, fmt, "v2")) {
|
||||||
try V2.render(res.writer(), weather, render_options.use_imperial);
|
try V2.render(res.writer(), weather, render_options.use_imperial);
|
||||||
} else if (std.mem.startsWith(u8, fmt, "%")) {
|
} else if (std.mem.startsWith(u8, fmt, "%")) {
|
||||||
|
|
@ -198,25 +198,3 @@ fn determineFormat(params: QueryParams, user_agent: ?[]const u8) Formatted.Forma
|
||||||
return .ansi;
|
return .ansi;
|
||||||
return .html;
|
return .html;
|
||||||
}
|
}
|
||||||
|
|
||||||
test "imperial units selection logic" {
|
|
||||||
// This test documents the priority order for unit selection:
|
|
||||||
// 1. Explicit ?u or ?m parameter (highest priority)
|
|
||||||
// 2. lang=us parameter
|
|
||||||
// 3. US IP detection
|
|
||||||
// 4. Default to metric
|
|
||||||
|
|
||||||
// The actual logic is tested through integration tests
|
|
||||||
// This test just verifies the QueryParams parsing works
|
|
||||||
const allocator = std.testing.allocator;
|
|
||||||
|
|
||||||
const params_u = try QueryParams.parse(allocator, "u");
|
|
||||||
try std.testing.expect(params_u.use_imperial.?);
|
|
||||||
|
|
||||||
const params_m = try QueryParams.parse(allocator, "m");
|
|
||||||
try std.testing.expect(!params_m.use_imperial.?);
|
|
||||||
|
|
||||||
const params_lang = try QueryParams.parse(allocator, "lang=us");
|
|
||||||
defer allocator.free(params_lang.lang.?);
|
|
||||||
try std.testing.expectEqualStrings("us", params_lang.lang.?);
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ const types = @import("../weather/types.zig");
|
||||||
const Moon = @import("../Moon.zig");
|
const Moon = @import("../Moon.zig");
|
||||||
const utils = @import("utils.zig");
|
const utils = @import("utils.zig");
|
||||||
|
|
||||||
pub fn render(writer: *std.Io.Writer, weather: types.WeatherData, allocator: std.mem.Allocator) !void {
|
pub fn render(writer: *std.Io.Writer, weather: types.WeatherData) !void {
|
||||||
|
|
||||||
// Current conditions
|
// Current conditions
|
||||||
try writer.print("# HELP temperature_feels_like_celsius Feels Like Temperature in Celsius\n", .{});
|
try writer.print("# HELP temperature_feels_like_celsius Feels Like Temperature in Celsius\n", .{});
|
||||||
|
|
@ -62,8 +62,8 @@ pub fn render(writer: *std.Io.Writer, weather: types.WeatherData, allocator: std
|
||||||
|
|
||||||
// Forecast days
|
// Forecast days
|
||||||
for (weather.forecast, 0..) |day, i| {
|
for (weather.forecast, 0..) |day, i| {
|
||||||
const forecast_label = try std.fmt.allocPrint(allocator, "{d}d", .{i});
|
var buf: [16]u8 = undefined;
|
||||||
defer allocator.free(forecast_label);
|
const forecast_label = try std.fmt.bufPrint(&buf, "{d}d", .{i});
|
||||||
|
|
||||||
try writer.print("uv_index{{forecast=\"{s}\"}} 0\n", .{forecast_label}); // Not in our data
|
try writer.print("uv_index{{forecast=\"{s}\"}} 0\n", .{forecast_label}); // Not in our data
|
||||||
|
|
||||||
|
|
@ -145,7 +145,7 @@ test "prometheus format includes required metrics" {
|
||||||
var output_buf: [8192]u8 = undefined;
|
var output_buf: [8192]u8 = undefined;
|
||||||
var writer = std.Io.Writer.fixed(&output_buf);
|
var writer = std.Io.Writer.fixed(&output_buf);
|
||||||
|
|
||||||
try render(&writer, weather, allocator);
|
try render(&writer, weather);
|
||||||
|
|
||||||
const output = output_buf[0..writer.end];
|
const output = output_buf[0..writer.end];
|
||||||
|
|
||||||
|
|
@ -182,7 +182,7 @@ test "prometheus format has proper help comments" {
|
||||||
var output_buf: [4096]u8 = undefined;
|
var output_buf: [4096]u8 = undefined;
|
||||||
var writer = std.Io.Writer.fixed(&output_buf);
|
var writer = std.Io.Writer.fixed(&output_buf);
|
||||||
|
|
||||||
try render(&writer, weather, allocator);
|
try render(&writer, weather);
|
||||||
|
|
||||||
const output = output_buf[0..writer.end];
|
const output = output_buf[0..writer.end];
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue