Merge pull request #465 from kemalcr/rework-param-parser

Rework param parser
This commit is contained in:
Serdar Dogruyol 2018-08-06 20:03:12 +03:00 committed by GitHub
commit 64fbe406d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 67 additions and 183 deletions

View file

@ -13,8 +13,8 @@ describe "Kemal::FilterHandler" do
test_filter.modified.should eq("false")
request = HTTP::Request.new("GET", "/greetings")
create_request_and_return_io(filter_middleware, request)
io_with_context = create_request_and_return_io(kemal, request)
create_request_and_return_io_and_context(filter_middleware, request)
io_with_context = create_request_and_return_io_and_context(kemal, request)[0]
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.body.should eq("true")
end
@ -33,14 +33,14 @@ describe "Kemal::FilterHandler" do
test_filter.modified.should eq("false")
request = HTTP::Request.new("GET", "/greetings")
create_request_and_return_io(filter_middleware, request)
io_with_context = create_request_and_return_io(kemal, request)
create_request_and_return_io_and_context(filter_middleware, request)
io_with_context = create_request_and_return_io_and_context(kemal, request)[0]
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.body.should eq("true")
request = HTTP::Request.new("POST", "/greetings")
create_request_and_return_io(filter_middleware, request)
io_with_context = create_request_and_return_io(kemal, request)
create_request_and_return_io_and_context(filter_middleware, request)
io_with_context = create_request_and_return_io_and_context(kemal, request)[0]
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.body.should eq("true")
end
@ -61,14 +61,14 @@ describe "Kemal::FilterHandler" do
test_filter.modified.should eq("false")
request = HTTP::Request.new("GET", "/greetings")
create_request_and_return_io(filter_middleware, request)
io_with_context = create_request_and_return_io(kemal, request)
create_request_and_return_io_and_context(filter_middleware, request)
io_with_context = create_request_and_return_io_and_context(kemal, request)[0]
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.body.should eq("false")
request = HTTP::Request.new("POST", "/greetings")
create_request_and_return_io(filter_middleware, request)
io_with_context = create_request_and_return_io(kemal, request)
create_request_and_return_io_and_context(filter_middleware, request)
io_with_context = create_request_and_return_io_and_context(kemal, request)[0]
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.body.should eq("false")
end
@ -85,8 +85,8 @@ describe "Kemal::FilterHandler" do
test_filter.modified.should eq("false")
request = HTTP::Request.new("GET", "/greetings")
create_request_and_return_io(filter_middleware, request)
io_with_context = create_request_and_return_io(kemal, request)
create_request_and_return_io_and_context(filter_middleware, request)
io_with_context = create_request_and_return_io_and_context(kemal, request)[0]
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.body.should eq("true")
end
@ -105,14 +105,14 @@ describe "Kemal::FilterHandler" do
test_filter.modified.should eq("false")
request = HTTP::Request.new("GET", "/greetings")
create_request_and_return_io(filter_middleware, request)
io_with_context = create_request_and_return_io(kemal, request)
create_request_and_return_io_and_context(filter_middleware, request)
io_with_context = create_request_and_return_io_and_context(kemal, request)[0]
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.body.should eq("true")
request = HTTP::Request.new("POST", "/greetings")
create_request_and_return_io(filter_middleware, request)
io_with_context = create_request_and_return_io(kemal, request)
create_request_and_return_io_and_context(filter_middleware, request)
io_with_context = create_request_and_return_io_and_context(kemal, request)[0]
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.body.should eq("true")
end
@ -132,14 +132,14 @@ describe "Kemal::FilterHandler" do
test_filter.modified.should eq("false")
request = HTTP::Request.new("GET", "/greetings")
create_request_and_return_io(filter_middleware, request)
io_with_context = create_request_and_return_io(kemal, request)
create_request_and_return_io_and_context(filter_middleware, request)
io_with_context = create_request_and_return_io_and_context(kemal, request)[0]
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.body.should eq("false")
request = HTTP::Request.new("POST", "/greetings")
create_request_and_return_io(filter_middleware, request)
io_with_context = create_request_and_return_io(kemal, request)
create_request_and_return_io_and_context(filter_middleware, request)
io_with_context = create_request_and_return_io_and_context(kemal, request)[0]
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.body.should eq("false")
end
@ -166,20 +166,20 @@ describe "Kemal::FilterHandler" do
test_filter_second.modified.should eq("false")
test_filter_third.modified.should eq("false")
request = HTTP::Request.new("GET", "/greetings")
create_request_and_return_io(filter_middleware, request)
io_with_context = create_request_and_return_io(kemal, request)
create_request_and_return_io_and_context(filter_middleware, request)
io_with_context = create_request_and_return_io_and_context(kemal, request)[0]
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.body.should eq("true")
request = HTTP::Request.new("POST", "/greetings")
create_request_and_return_io(filter_middleware, request)
io_with_context = create_request_and_return_io(kemal, request)
create_request_and_return_io_and_context(filter_middleware, request)
io_with_context = create_request_and_return_io_and_context(kemal, request)[0]
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.body.should eq("false")
request = HTTP::Request.new("PUT", "/greetings")
create_request_and_return_io(filter_middleware, request)
io_with_context = create_request_and_return_io(kemal, request)
create_request_and_return_io_and_context(filter_middleware, request)
io_with_context = create_request_and_return_io_and_context(kemal, request)[0]
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.body.should eq("true")
end

View file

@ -28,8 +28,8 @@ describe "ParamParser" do
end
request = HTTP::Request.new("POST", "/hello/cemal")
# Radix tree MUST be run to parse url params.
create_request_and_return_io(kemal, request)
url_params = Kemal::ParamParser.new(request).url
context = create_request_and_return_io_and_context(kemal, request)[1]
url_params = Kemal::ParamParser.new(request, context.route_lookup.params).url
url_params["hasan"].should eq "cemal"
end
@ -43,8 +43,8 @@ describe "ParamParser" do
end
request = HTTP::Request.new("POST", "/hello/sam%2Bspec%40gmail.com/%2419.99/a%C3%B1o")
# Radix tree MUST be run to parse url params.
create_request_and_return_io(kemal, request)
url_params = Kemal::ParamParser.new(request).url
context = create_request_and_return_io_and_context(kemal, request)[1]
url_params = Kemal::ParamParser.new(request, context.route_lookup.params).url
url_params["email"].should eq "sam+spec@gmail.com"
url_params["money"].should eq "$19.99"
url_params["spanish"].should eq "año"

View file

@ -102,48 +102,6 @@ describe "Kemal::RouteHandler" do
client_response.body.should eq("Skills ruby,crystal")
end
it "checks for _method param in POST request to simulate PUT" do
put "/" do
"Hello World from PUT"
end
request = HTTP::Request.new(
"POST",
"/",
body: "_method=PUT",
headers: HTTP::Headers{"Content-Type" => "application/x-www-form-urlencoded"}
)
client_response = call_request_on_app(request)
client_response.body.should eq("Hello World from PUT")
end
it "checks for _method param in POST request to simulate PATCH" do
patch "/" do
"Hello World from PATCH"
end
request = HTTP::Request.new(
"POST",
"/",
body: "_method=PATCH",
headers: HTTP::Headers{"Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"}
)
client_response = call_request_on_app(request)
client_response.body.should eq("Hello World from PATCH")
end
it "checks for _method param in POST request to simulate DELETE" do
delete "/" do
"Hello World from DELETE"
end
request = HTTP::Request.new(
"POST",
"/",
body: "_method=DELETE",
headers: HTTP::Headers{"Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"}
)
client_response = call_request_on_app(request)
client_response.body.should eq("Hello World from DELETE")
end
it "can process HTTP HEAD requests for defined GET routes" do
get "/" do
"Hello World from GET"

View file

@ -29,17 +29,17 @@ end
add_context_storage_type(TestContextStorageType)
add_context_storage_type(AnotherContextStorageType)
def create_request_and_return_io(handler, request)
def create_request_and_return_io_and_context(handler, request)
io = IO::Memory.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
handler.call(context)
response.close
io.rewind
io
{io, context}
end
def create_ws_request_and_return_io(handler, request)
def create_ws_request_and_return_io_and_context(handler, request)
io = IO::Memory.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
@ -49,6 +49,7 @@ def create_ws_request_and_return_io(handler, request)
# Raises because the IO::Memory is empty
end
io.rewind
{io, context}
end
def call_request_on_app(request)

View file

@ -32,13 +32,13 @@ describe "Kemal::WebSocketHandler" do
}
request = HTTP::Request.new("GET", "/", headers)
io_with_context = create_ws_request_and_return_io(handler, request)
io_with_context = create_ws_request_and_return_io_and_context(handler, request)[0]
io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n\x81\u0005Match")
end
it "fetches named url parameters" do
handler = Kemal::WebSocketHandler::INSTANCE
ws "/:id" { |_, c| c.params.url["id"] }
ws "/:id" { |_, c| c.ws_route_lookup.params["id"] }
headers = HTTP::Headers{
"Upgrade" => "websocket",
"Connection" => "Upgrade",
@ -46,7 +46,7 @@ describe "Kemal::WebSocketHandler" do
"Sec-WebSocket-Version" => "13",
}
request = HTTP::Request.new("GET", "/1234", headers)
io_with_context = create_ws_request_and_return_io(handler, request)
io_with_context = create_ws_request_and_return_io_and_context(handler, request)[0]
io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n")
end