Skip to content

Commit

Permalink
Contain fibers and open ports in HTTP::Client and HTTP::WebSocket specs
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Apr 1, 2019
1 parent d9ef50e commit 2a1e64b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
27 changes: 12 additions & 15 deletions spec/std/http/client/client_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "spec"
require "../spec_helper"
require "openssl"
require "http/client"
require "http/server"
Expand Down Expand Up @@ -132,37 +132,34 @@ module HTTP
context.response.print context.request.headers["Host"]
end
address = server.bind_unused_port "::1"
spawn { server.listen }

HTTP::Client.get("http://[::1]:#{address.port}/").body.should eq("[::1]:#{address.port}")

server.close
run_server(server) do
HTTP::Client.get("http://[::1]:#{address.port}/").body.should eq("[::1]:#{address.port}")
end
end

it "sends a 'connection: close' header on one-shot request" do
server = HTTP::Server.new do |context|
context.response.print context.request.headers["connection"]
end
address = server.bind_unused_port "::1"
spawn { server.listen }

HTTP::Client.get("http://[::1]:#{address.port}/").body.should eq("close")

server.close
run_server(server) do
HTTP::Client.get("http://[::1]:#{address.port}/").body.should eq("close")
end
end

it "sends a 'connection: close' header on one-shot request with block" do
server = HTTP::Server.new do |context|
context.response.print context.request.headers["connection"]
end
address = server.bind_unused_port "::1"
spawn { server.listen }

HTTP::Client.get("http://[::1]:#{address.port}/") do |response|
response.body_io.gets_to_end
end.should eq("close")

server.close
run_server(server) do
HTTP::Client.get("http://[::1]:#{address.port}/") do |response|
response.body_io.gets_to_end
end.should eq("close")
end
end

it "doesn't read the body if request was HEAD" do
Expand Down
28 changes: 13 additions & 15 deletions spec/std/http/web_socket_spec.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "./spec_helper"
require "../spec_helper"
require "http/web_socket"
require "random/secure"
Expand Down Expand Up @@ -380,13 +381,12 @@ describe HTTP::WebSocket do
end

address = http_server.bind_unused_port
spawn http_server.not_nil!.listen # TODO: Remove .not_nil! when #6037 is fixed

expect_raises(Socket::Error, "Handshake got denied. Status code was 200.") do
HTTP::WebSocket::Protocol.new(address.address, port: address.port, path: "/")
run_server(http_server) do
expect_raises(Socket::Error, "Handshake got denied. Status code was 200.") do
HTTP::WebSocket::Protocol.new(address.address, port: address.port, path: "/")
end
end
ensure
# http_server.try &.close # TODO: Uncomment when #5958 is fixed
end

describe "handshake fails if server does not verify Sec-WebSocket-Key" do
Expand All @@ -399,13 +399,12 @@ describe HTTP::WebSocket do
end

address = http_server.bind_unused_port
spawn http_server.not_nil!.listen # TODO: Remove .not_nil! when #6037 is fixed

expect_raises(Socket::Error, "Handshake got denied. Server did not verify WebSocket challenge.") do
HTTP::WebSocket::Protocol.new(address.address, port: address.port, path: "/")
run_server(http_server) do
expect_raises(Socket::Error, "Handshake got denied. Server did not verify WebSocket challenge.") do
HTTP::WebSocket::Protocol.new(address.address, port: address.port, path: "/")
end
end
ensure
# http_server.try &.close # TODO: Uncomment when #5958 is fixed
end

it "Sec-WebSocket-Accept incorrect" do
Expand All @@ -418,13 +417,12 @@ describe HTTP::WebSocket do
end

address = http_server.bind_unused_port
spawn http_server.not_nil!.listen # TODO: Remove .not_nil! when #6037 is fixed

expect_raises(Socket::Error, "Handshake got denied. Server did not verify WebSocket challenge.") do
HTTP::WebSocket::Protocol.new(address.address, port: address.port, path: "/")
run_server(http_server) do
expect_raises(Socket::Error, "Handshake got denied. Server did not verify WebSocket challenge.") do
HTTP::WebSocket::Protocol.new(address.address, port: address.port, path: "/")
end
end
ensure
# http_server.try &.close # TODO: Uncomment when #5958 is fixed
end
end

Expand Down

0 comments on commit 2a1e64b

Please sign in to comment.