Skip to content

Commit

Permalink
feat: return 405 on not allowed method (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
LoisSotoLopez authored Oct 24, 2023
1 parent 6629780 commit b7d7381
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
28 changes: 26 additions & 2 deletions src/erf_router.erl
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ handle_ast(API, #{callback := Callback} = Opts) ->
)
),
EndpointParameters = maps:get(parameters, Endpoint),
lists:map(
AllowedMethods = lists:map(
fun(Operation) ->
Method = erl_syntax:atom(
maps:get(method, Operation)
Expand Down Expand Up @@ -350,7 +350,31 @@ handle_ast(API, #{callback := Callback} = Opts) ->
)
end,
maps:get(operations, Endpoint, [])
)
),
NotAllowedMethod =
erl_syntax:clause(
[
erl_syntax:tuple([
Path,
erl_syntax:variable('_Method'),
erl_syntax:variable('_QueryParameters'),
erl_syntax:variable('_Headers'),
erl_syntax:variable('_Body')
])
],
none,
[
erl_syntax:tuple(
[
erl_syntax:integer(405),
erl_syntax:list([]),
erl_syntax:atom(undefined)
]
)
]
),

AllowedMethods ++ [NotAllowedMethod]
end,
maps:get(endpoints, API, [])
),
Expand Down
18 changes: 14 additions & 4 deletions test/erf_router_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ foo(_Conf) ->
meck:expect(get_foo_request_body, is_valid, fun(_Value) -> true end),

Req = {
_Path = [<<"1">>, <<"foo">>],
Path = [<<"1">>, <<"foo">>],
_Method = get,
_QueryParameters = [],
_Headers = [],
_Body = <<>>
QueryParameters = [],
Headers = [],
Body = <<>>
},

?assertEqual({200, [], <<"bar">>}, Mod:handle(Req)),
Expand All @@ -161,6 +161,16 @@ foo(_Conf) ->

?assertEqual({400, [], undefined}, Mod:handle(Req)),

NotAllowedReq = {
Path,
_NotAllowedMethod = post,
QueryParameters,
Headers,
Body
},

?assertEqual({405, [], undefined}, Mod:handle(NotAllowedReq)),

meck:unload([
foo_callback,
version_foo_version,
Expand Down

0 comments on commit b7d7381

Please sign in to comment.