Add context to WebSocket. This allows one to use context.params like url,query, body e.g

This commit is contained in:
Sdogruyol 2017-08-20 20:01:49 +03:00
parent 5a83522866
commit 2b604dfb30
9 changed files with 71 additions and 41 deletions

View file

@ -55,6 +55,7 @@ describe "Context" do
before_get_context_test: env.get("before_get_context_test"),
}
end
request = HTTP::Request.new("GET", "/")
io = IO::Memory.new
response = HTTP::Server::Response.new(io)

View file

@ -9,7 +9,7 @@ describe "Kemal::FilterHandler" do
filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = "true" }
kemal = Kemal::RouteHandler::INSTANCE
kemal.add_route "GET", "/greetings" { test_filter.modified }
kemal.add_http_route "GET", "/greetings" { test_filter.modified }
test_filter.modified.should eq("false")
request = HTTP::Request.new("GET", "/greetings")
@ -27,8 +27,8 @@ describe "Kemal::FilterHandler" do
filter_middleware._add_route_filter("GET", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
kemal = Kemal::RouteHandler::INSTANCE
kemal.add_route "GET", "/greetings" { test_filter.modified }
kemal.add_route "POST", "/greetings" { test_filter.modified }
kemal.add_http_route "GET", "/greetings" { test_filter.modified }
kemal.add_http_route "POST", "/greetings" { test_filter.modified }
test_filter.modified.should eq("false")
@ -55,8 +55,8 @@ describe "Kemal::FilterHandler" do
filter_middleware._add_route_filter("POST", "/greetings", :before) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
kemal = Kemal::RouteHandler::INSTANCE
kemal.add_route "GET", "/greetings" { test_filter.modified }
kemal.add_route "POST", "/greetings" { test_filter.modified }
kemal.add_http_route "GET", "/greetings" { test_filter.modified }
kemal.add_http_route "POST", "/greetings" { test_filter.modified }
test_filter.modified.should eq("false")
@ -81,7 +81,7 @@ describe "Kemal::FilterHandler" do
filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = "true" }
kemal = Kemal::RouteHandler::INSTANCE
kemal.add_route "GET", "/greetings" { test_filter.modified }
kemal.add_http_route "GET", "/greetings" { test_filter.modified }
test_filter.modified.should eq("false")
request = HTTP::Request.new("GET", "/greetings")
@ -99,8 +99,8 @@ describe "Kemal::FilterHandler" do
filter_middleware._add_route_filter("GET", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
kemal = Kemal::RouteHandler::INSTANCE
kemal.add_route "GET", "/greetings" { test_filter.modified }
kemal.add_route "POST", "/greetings" { test_filter.modified }
kemal.add_http_route "GET", "/greetings" { test_filter.modified }
kemal.add_http_route "POST", "/greetings" { test_filter.modified }
test_filter.modified.should eq("false")
@ -127,8 +127,8 @@ describe "Kemal::FilterHandler" do
filter_middleware._add_route_filter("POST", "/greetings", :after) { test_filter.modified = test_filter.modified == "true" ? "false" : "true" }
kemal = Kemal::RouteHandler::INSTANCE
kemal.add_route "GET", "/greetings" { test_filter.modified }
kemal.add_route "POST", "/greetings" { test_filter.modified }
kemal.add_http_route "GET", "/greetings" { test_filter.modified }
kemal.add_http_route "POST", "/greetings" { test_filter.modified }
test_filter.modified.should eq("false")
request = HTTP::Request.new("GET", "/greetings")
@ -158,9 +158,9 @@ describe "Kemal::FilterHandler" do
filter_middleware._add_route_filter("ALL", "/greetings", :before) { test_filter_third.modified = test_filter_third.modified == "true" ? "false" : "true" }
kemal = Kemal::RouteHandler::INSTANCE
kemal.add_route "GET", "/greetings" { test_filter.modified }
kemal.add_route "POST", "/greetings" { test_filter_second.modified }
kemal.add_route "PUT", "/greetings" { test_filter_third.modified }
kemal.add_http_route "GET", "/greetings" { test_filter.modified }
kemal.add_http_route "POST", "/greetings" { test_filter_second.modified }
kemal.add_http_route "PUT", "/greetings" { test_filter_third.modified }
test_filter.modified.should eq("false")
test_filter_second.modified.should eq("false")

View file

@ -23,7 +23,7 @@ describe "ParamParser" do
it "parses url params" do
kemal = Kemal::RouteHandler::INSTANCE
kemal.add_route "POST", "/hello/:hasan" do |env|
kemal.add_http_route "POST", "/hello/:hasan" do |env|
"hello #{env.params.url["hasan"]}"
end
request = HTTP::Request.new("POST", "/hello/cemal")
@ -35,7 +35,7 @@ describe "ParamParser" do
it "decodes url params" do
kemal = Kemal::RouteHandler::INSTANCE
kemal.add_route "POST", "/hello/:email/:money/:spanish" do |env|
kemal.add_http_route "POST", "/hello/:email/:money/:spanish" do |env|
email = env.params.url["email"]
money = env.params.url["money"]
spanish = env.params.url["spanish"]

View file

@ -81,5 +81,6 @@ end
Spec.after_each do
Kemal.config.clear
Kemal::RouteHandler::INSTANCE.tree = Radix::Tree(Route).new
Kemal::RouteHandler::INSTANCE.http_routes = Radix::Tree(Route).new
Kemal::RouteHandler::INSTANCE.ws_routes = Radix::Tree(String).new
end

View file

@ -1,19 +1,6 @@
require "./spec_helper"
describe "Kemal::WebSocketHandler" do
it "doesn't match on wrong route" do
handler = Kemal::WebSocketHandler.new "/" { }
headers = HTTP::Headers{
"Upgrade" => "websocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
}
request = HTTP::Request.new("GET", "/asd", headers)
io_with_context = create_request_and_return_io(handler, request)
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.status_code.should eq(404)
end
it "matches on given route" do
handler = Kemal::WebSocketHandler.new "/" { }
headers = HTTP::Headers{
@ -25,4 +12,16 @@ describe "Kemal::WebSocketHandler" do
io_with_context = create_ws_request_and_return_io(handler, request)
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
it "fetches named url parameters" do
handler = Kemal::WebSocketHandler.new "/:id" { |s, c| c.params.url["id"] }
headers = HTTP::Headers{
"Upgrade" => "websocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
}
request = HTTP::Request.new("GET", "/1234", headers)
io_with_context = create_ws_request_and_return_io(handler, request)
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
end