mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Decouple specs from global state to isolated tests
This commit is contained in:
parent
29b18c927c
commit
2e42b3f48c
19 changed files with 336 additions and 304 deletions
|
@ -1,10 +1,24 @@
|
|||
require "./spec_helper"
|
||||
|
||||
private def create_ws_request_and_return_io(handler, request, app)
|
||||
io = IO::Memory.new
|
||||
response = HTTP::Server::Response.new(io)
|
||||
context = HTTP::Server::Context.new(request, response)
|
||||
context.app = app
|
||||
begin
|
||||
handler.call context
|
||||
rescue IO::Error
|
||||
# Raises because the IO::Memory is empty
|
||||
end
|
||||
io
|
||||
end
|
||||
|
||||
describe "Kemal::WebSocketHandler" do
|
||||
it "doesn't match on wrong route" do
|
||||
app = Kemal::Base.new
|
||||
handler = Kemal::WebSocketHandler.new
|
||||
handler.next = Kemal::RouteHandler.new
|
||||
ws "/" { }
|
||||
app.ws "/" { }
|
||||
headers = HTTP::Headers{
|
||||
"Upgrade" => "websocket",
|
||||
"Connection" => "Upgrade",
|
||||
|
@ -14,7 +28,7 @@ describe "Kemal::WebSocketHandler" do
|
|||
io = IO::Memory.new
|
||||
response = HTTP::Server::Response.new(io)
|
||||
context = HTTP::Server::Context.new(request, response)
|
||||
context.app = Kemal.application
|
||||
context.app = app
|
||||
|
||||
expect_raises(Kemal::Exceptions::RouteNotFound) do
|
||||
handler.call context
|
||||
|
@ -22,9 +36,10 @@ describe "Kemal::WebSocketHandler" do
|
|||
end
|
||||
|
||||
it "matches on given route" do
|
||||
app = Kemal::Base.new
|
||||
handler = Kemal::WebSocketHandler.new
|
||||
ws "/" { |socket, context| socket.send("Match") }
|
||||
ws "/no_match" { |socket, context| socket.send "No Match" }
|
||||
app.ws "/" { |socket, context| socket.send("Match") }
|
||||
app.ws "/no_match" { |socket, context| socket.send "No Match" }
|
||||
headers = HTTP::Headers{
|
||||
"Upgrade" => "websocket",
|
||||
"Connection" => "Upgrade",
|
||||
|
@ -33,13 +48,14 @@ describe "Kemal::WebSocketHandler" do
|
|||
}
|
||||
request = HTTP::Request.new("GET", "/", headers)
|
||||
|
||||
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")
|
||||
io_with_context = create_ws_request_and_return_io(handler, request, app)
|
||||
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
|
||||
app = Kemal::Base.new
|
||||
handler = Kemal::WebSocketHandler.new
|
||||
ws "/:id" { |s, c| c.params.url["id"] }
|
||||
app.ws "/:id" { |s, c| c.params.url["id"] }
|
||||
headers = HTTP::Headers{
|
||||
"Upgrade" => "websocket",
|
||||
"Connection" => "Upgrade",
|
||||
|
@ -47,20 +63,21 @@ describe "Kemal::WebSocketHandler" do
|
|||
"Sec-WebSocket-Version" => "13",
|
||||
}
|
||||
request = HTTP::Request.new("GET", "/1234", headers)
|
||||
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")
|
||||
io_with_context = create_ws_request_and_return_io(handler, request, app)
|
||||
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 "matches correct verb" do
|
||||
app = Kemal::Base.new
|
||||
handler = Kemal::WebSocketHandler.new
|
||||
handler.next = Kemal::RouteHandler.new
|
||||
ws "/" { }
|
||||
get "/" { "get" }
|
||||
app.ws "/" { }
|
||||
app.get "/" { "get" }
|
||||
request = HTTP::Request.new("GET", "/")
|
||||
io = IO::Memory.new
|
||||
response = HTTP::Server::Response.new(io)
|
||||
context = HTTP::Server::Context.new(request, response)
|
||||
context.app = Kemal.application
|
||||
context.app = app
|
||||
handler.call(context)
|
||||
response.close
|
||||
io.rewind
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue