Skip to content

Commit 8126cdd

Browse files
committed
334 - updated endpoint paths validation to be more clear
1 parent ac4e733 commit 8126cdd

File tree

5 files changed

+85
-17
lines changed

5 files changed

+85
-17
lines changed

lib/rig_api/v1/apis_test.exs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,7 @@ defmodule RigApi.V1.APIsTest do
367367
"invalid-config/" => [
368368
%{"id" => "must be string"},
369369
%{"id" => "must have a length of at least 1"},
370-
%{"path" => "must be string"},
371-
%{"path" => "must have a length of at least 1"},
372-
%{"path_regex" => "must be string"},
373-
%{"path_regex" => "must have a length of at least 1"},
370+
%{"path, path_regex" => "Either path or path_regex must be set"},
374371
%{"method" => "must be string"},
375372
%{"method" => "must have a length of at least 1"}
376373
]

lib/rig_api/v2/apis_test.exs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,7 @@ defmodule RigApi.V2.APIsTest do
367367
"invalid-config/" => [
368368
%{"id" => "must be string"},
369369
%{"id" => "must have a length of at least 1"},
370-
%{"path" => "must be string"},
371-
%{"path" => "must have a length of at least 1"},
372-
%{"path_regex" => "must be string"},
373-
%{"path_regex" => "must have a length of at least 1"},
370+
%{"path, path_regex" => "Either path or path_regex must be set"},
374371
%{"method" => "must be string"},
375372
%{"method" => "must have a length of at least 1"}
376373
]

lib/rig_api/v3/apis_test.exs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,7 @@ defmodule RigApi.V3.APIsTest do
367367
"invalid-config/" => [
368368
%{"id" => "must be string"},
369369
%{"id" => "must have a length of at least 1"},
370-
%{"path" => "must be string"},
371-
%{"path" => "must have a length of at least 1"},
372-
%{"path_regex" => "must be string"},
373-
%{"path_regex" => "must have a length of at least 1"},
370+
%{"path, path_regex" => "Either path or path_regex must be set"},
374371
%{"method" => "must be string"},
375372
%{"method" => "must have a length of at least 1"}
376373
]

lib/rig_inbound_gateway/api_proxy/validations.ex

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ defmodule RigInboundGateway.ApiProxy.Validations do
1212

1313
require Logger
1414

15+
@endpoint_paths ["path", "path_regex"]
16+
@endpoint_paths_error_key "path, path_regex"
17+
1518
@type error_t :: [{:error, String.t() | atom, atom, String.t()}]
1619
@type error_list_t :: [{String.t(), error_t()}]
1720
@type error_map_t :: %{String.t() => [%{(String.t() | atom) => String.t()}]}
@@ -90,10 +93,23 @@ defmodule RigInboundGateway.ApiProxy.Validations do
9093

9194
@spec validate_endpoint_path(Api.endpoint()) :: error_list_t()
9295
def validate_endpoint_path(endpoint) do
93-
errors = validate_string(endpoint, "path") ++ validate_string(endpoint, "path_regex")
94-
95-
# each option can produce 2 errors, so 4 in total, therefore 2 is the min value for number of errors
96-
with_any_error(errors, 2)
96+
present_paths =
97+
@endpoint_paths
98+
|> Enum.filter(fn key -> Map.has_key?(endpoint, key) end)
99+
100+
case present_paths do
101+
[] ->
102+
[{:error, @endpoint_paths_error_key, :by, "Either path or path_regex must be set"}]
103+
104+
[path] ->
105+
validate_string(endpoint, path)
106+
107+
_ ->
108+
[
109+
{:error, @endpoint_paths_error_key, :by,
110+
"You can't set path and path_regex at the same time"}
111+
]
112+
end
97113
end
98114

99115
# ---

test/rig_inbound_gateway/rig/proxy_test.exs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,68 @@ defmodule RigInboundGateway.ProxyTest do
154154

155155
test_proxy_exit(
156156
ctx,
157-
"[{\"invalid-config/\", [{:error, \"id\", :by, \"must be string\"}, {:error, \"id\", :length, \"must have a length of at least 1\"}, {:error, \"path\", :by, \"must be string\"}, {:error, \"path\", :length, \"must have a length of at least 1\"}, {:error, \"path_regex\", :by, \"must be string\"}, {:error, \"path_regex\", :length, \"must have a length of at least 1\"}, {:error, \"method\", :by, \"must be string\"}, {:error, \"method\", :length, \"must have a length of at least 1\"}]}]"
157+
"[{\"invalid-config/\", [{:error, \"id\", :by, \"must be string\"}, {:error, \"id\", :length, \"must have a length of at least 1\"}, {:error, \"path, path_regex\", :by, \"Either path or path_regex must be set\"}, {:error, \"method\", :by, \"must be string\"}, {:error, \"method\", :length, \"must have a length of at least 1\"}]}]"
158+
)
159+
160+
ProxyConfig.restore()
161+
end
162+
163+
test "should exit the process when 'endpoint' has 'path' and 'path_regex' set at the same time",
164+
ctx do
165+
endpoints = [
166+
%{
167+
"id" => @invalid_config_id <> "1",
168+
"method" => "GET",
169+
"path" => "/",
170+
"path_regex" => "/"
171+
}
172+
]
173+
174+
ProxyConfig.set_proxy_config(@invalid_config_id, endpoints)
175+
176+
test_proxy_exit(
177+
ctx,
178+
"[{\"invalid-config/invalid-config1\", [{:error, \"path, path_regex\", :by, \"You can't set path and path_regex at the same time\"}]}]"
179+
)
180+
181+
ProxyConfig.restore()
182+
end
183+
184+
test "should exit the process when 'path' is set, but incorrect",
185+
ctx do
186+
endpoints = [
187+
%{
188+
"id" => @invalid_config_id <> "1",
189+
"method" => "GET",
190+
"path" => ""
191+
}
192+
]
193+
194+
ProxyConfig.set_proxy_config(@invalid_config_id, endpoints)
195+
196+
test_proxy_exit(
197+
ctx,
198+
"[{\"invalid-config/invalid-config1\", [{:error, \"path\", :length, \"must have a length of at least 1\"}]}]"
199+
)
200+
201+
ProxyConfig.restore()
202+
end
203+
204+
test "should exit the process when 'path_regex' is set, but incorrect",
205+
ctx do
206+
endpoints = [
207+
%{
208+
"id" => @invalid_config_id <> "1",
209+
"method" => "GET",
210+
"path_regex" => ""
211+
}
212+
]
213+
214+
ProxyConfig.set_proxy_config(@invalid_config_id, endpoints)
215+
216+
test_proxy_exit(
217+
ctx,
218+
"[{\"invalid-config/invalid-config1\", [{:error, \"path_regex\", :length, \"must have a length of at least 1\"}]}]"
158219
)
159220

160221
ProxyConfig.restore()

0 commit comments

Comments
 (0)