Skip to content

Commit

Permalink
fix(routing_trie): floats must contain a dot
Browse files Browse the repository at this point in the history
  • Loading branch information
mookums committed Dec 23, 2024
1 parent 12d05b9 commit 9d89c94
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/http/router/routing_trie.zig
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,12 @@ pub const RoutingTrie = struct {
.unsigned => if (std.fmt.parseInt(u64, chunk, 10)) |value| {
captures[capture_idx] = Capture{ .unsigned = value };
} else |_| continue :child_loop,
.float => if (std.fmt.parseFloat(f64, chunk)) |value| {
captures[capture_idx] = Capture{ .float = value };
} else |_| continue :child_loop,
// Float types MUST have a '.' to differentiate them.
.float => if (std.mem.indexOfScalar(u8, chunk, '.')) |_| {
if (std.fmt.parseFloat(f64, chunk)) |value| {
captures[capture_idx] = Capture{ .float = value };
} else |_| continue :child_loop;
} else continue :child_loop,
.string => captures[capture_idx] = Capture{ .string = chunk },
.remaining => {
const rest = iter.buffer[(iter.index - chunk.len)..];
Expand Down Expand Up @@ -483,6 +486,7 @@ test "Routing with Queries" {
try testing.expectEqual(null, try s.get_bundle("/item/name", captures[0..], &q));

{
q.clear();
const captured = (try s.get_bundle("/item/name/HELLO?name=muki&food=waffle", captures[0..], &q)).?;
try testing.expectEqual(Route.init("/item/name/%r"), captured.bundle.route);
try testing.expectEqualStrings("HELLO", captured.captures[0].remaining);
Expand All @@ -492,6 +496,7 @@ test "Routing with Queries" {
}

{
q.clear();
// Purposefully bad format with no keys or values.
const captured = (try s.get_bundle("/item/2112.22121/price_float?", captures[0..], &q)).?;
try testing.expectEqual(Route.init("/item/%f/price_float"), captured.bundle.route);
Expand All @@ -500,6 +505,7 @@ test "Routing with Queries" {
}

{
q.clear();
// Purposefully bad format with incomplete key/value pair.
const captured = (try s.get_bundle("/item/100/price/283.21?help", captures[0..], &q)).?;
try testing.expectEqual(Route.init("/item/%i/price/%f"), captured.bundle.route);
Expand All @@ -509,6 +515,7 @@ test "Routing with Queries" {
}

{
q.clear();
// Purposefully have too many queries.
const captured = try s.get_bundle(
"/item/100/price/283.21?a=1&b=2&c=3&d=4&e=5&f=6&g=7&h=8&i=9&j=10&k=11",
Expand Down

0 comments on commit 9d89c94

Please sign in to comment.