Skip to content

Commit

Permalink
test expressions validation
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Nov 3, 2023
1 parent a1fad05 commit 8768266
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions spec/01-unit/01-db/01-schema/06-routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1416,3 +1416,98 @@ describe("routes schema (flavor = traditional_compatible)", function()
assert.is_nil(errs)
end)
end)


describe("routes schema (flavor = expressions)", function()
local a_valid_uuid = "cbb297c0-a956-486d-ad1d-f9b42df9465a"
local another_uuid = "64a8670b-900f-44e7-a900-6ec7ef5aa4d3"

reload_flavor("expressions")
setup_global_env()

it("validates a valid http route", function()
local route = {
id = a_valid_uuid,
name = "my_route",
protocols = { "http" },
expression = [[http.method == "GET" && http.host == "example.com" && http.path == "/ovo"]],
priority = 100,
strip_path = false,
preserve_host = true,
service = { id = another_uuid },
}
route = Routes:process_auto_fields(route, "insert")
assert.truthy(route.created_at)
assert.truthy(route.updated_at)
assert.same(route.created_at, route.updated_at)
assert.truthy(Routes:validate(route))
assert.falsy(route.strip_path)
end)

it("validates a valid stream route", function()
local route = {
id = a_valid_uuid,
name = "my_route",
protocols = { "tcp" },
expression = [[net.src.ip == 1.2.3.4 && net.src.port == 80]],
priority = 100,
service = { id = another_uuid },
}
route = Routes:process_auto_fields(route, "insert")
assert.truthy(route.created_at)
assert.truthy(route.updated_at)
assert.same(route.created_at, route.updated_at)
assert.truthy(Routes:validate(route))
end)

it("fails when path is invalid", function()
local route = {
id = a_valid_uuid,
name = "my_route",
protocols = { "http" },
expression = [[http.method == "GET" && http.path ~ "/[abc/*/user$"]],
priority = 100,
service = { id = another_uuid },
}
route = Routes:process_auto_fields(route, "insert")
local ok, errs = Routes:validate_insert(route)
assert.falsy(ok)

-- verified by `schema/typedefs.lua`
assert.truthy(errs["@entity"])
end)

it("fails when ip address is invalid", function()
local route = {
id = a_valid_uuid,
name = "my_route",
protocols = { "tcp" },
expression = [[net.src.ip in 1.2.3.4/16 && net.src.port == 80]],
priority = 100,
service = { id = another_uuid },
}
route = Routes:process_auto_fields(route, "insert")
local ok, errs = Routes:validate_insert(route)
assert.falsy(ok)

-- verified by `schema/typedefs.lua`
assert.truthy(errs["@entity"])
end)

it("fails if invalid field appears", function()
local route = {
id = a_valid_uuid,
name = "my_route",
protocols = { "tcp" },
expression = [[http.method == "GET" && net.src.ip == 1.2.3.4 && net.src.port == 80]],
priority = 100,
service = { id = another_uuid },
}
route = Routes:process_auto_fields(route, "insert")
local ok, errs = Routes:validate_insert(route)
assert.falsy(ok)

-- verified by `schema/typedefs.lua`
assert.truthy(errs["@entity"])
end)
end)

0 comments on commit 8768266

Please sign in to comment.