mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Route declaration must start with / fixes #242
This commit is contained in:
parent
7dd834ad28
commit
e6810c4516
5 changed files with 24 additions and 0 deletions
|
@ -11,6 +11,7 @@ Request -> Middleware -> Filter -> Route
|
||||||
```
|
```
|
||||||
|
|
||||||
- Rename `return_with` as `halt`.
|
- Rename `return_with` as `halt`.
|
||||||
|
- Route declaration must start with `/`. Fixes [#242](https://github.com/sdogruyol/kemal/issues/242)
|
||||||
|
|
||||||
# 0.16.1 (12-10-2016)
|
# 0.16.1 (12-10-2016)
|
||||||
|
|
||||||
|
|
|
@ -13,5 +13,13 @@ describe "Route" do
|
||||||
client_response = call_request_on_app(request)
|
client_response = call_request_on_app(request)
|
||||||
client_response.body.should eq("Route 2")
|
client_response.body.should eq("Route 2")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "doesn't allow a route declaration start without /" do
|
||||||
|
expect_raises Kemal::Exceptions::PathStartInvalidException, "Route declaration get \"route\" needs to start with '/', should be get \"/route\"" do
|
||||||
|
get "route" do |env|
|
||||||
|
"Route 1"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,11 +5,13 @@ HTTP_METHODS = %w(get post put patch delete options)
|
||||||
|
|
||||||
{% for method in HTTP_METHODS %}
|
{% for method in HTTP_METHODS %}
|
||||||
def {{method.id}}(path, &block : HTTP::Server::Context -> _)
|
def {{method.id}}(path, &block : HTTP::Server::Context -> _)
|
||||||
|
raise Kemal::Exceptions::PathStartInvalidException.new({{method}}, path) unless Kemal::Utils.path_starts_with_backslash?(path)
|
||||||
Kemal::RouteHandler::INSTANCE.add_route({{method}}.upcase, path, &block)
|
Kemal::RouteHandler::INSTANCE.add_route({{method}}.upcase, path, &block)
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
def ws(path, &block : HTTP::WebSocket, HTTP::Server::Context -> Void)
|
def ws(path, &block : HTTP::WebSocket, HTTP::Server::Context -> Void)
|
||||||
|
raise Kemal::Exceptions::PathStartInvalidException.new("ws", path) unless Kemal::Utils.path_starts_with_backslash?(path)
|
||||||
Kemal::WebSocketHandler.new path, &block
|
Kemal::WebSocketHandler.new path, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
# Exceptions for 404 and custom errors are defined here.
|
# Exceptions for 404 and custom errors are defined here.
|
||||||
module Kemal::Exceptions
|
module Kemal::Exceptions
|
||||||
|
class PathStartInvalidException < Exception
|
||||||
|
def initialize(method, path)
|
||||||
|
super "Route declaration #{method} \"#{path}\" needs to start with '/', should be #{method} \"/#{path}\""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class RouteNotFound < Exception
|
class RouteNotFound < Exception
|
||||||
def initialize(context)
|
def initialize(context)
|
||||||
super "Requested path: '#{context.request.override_method.as(String)}:#{context.request.path}' was not found."
|
super "Requested path: '#{context.request.override_method.as(String)}:#{context.request.path}' was not found."
|
||||||
|
|
7
src/kemal/helpers/utils.cr
Normal file
7
src/kemal/helpers/utils.cr
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
module Kemal
|
||||||
|
class Utils
|
||||||
|
def self.path_starts_with_backslash?(path)
|
||||||
|
path.starts_with?("/")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue