Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HTTP::Server specs to use free ports in expectations #6530

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions spec/std/http/server/server_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "spec"
require "../../spec_helper"
require "http/server"
require "http/client/response"
require "tempfile"
Expand Down Expand Up @@ -53,6 +53,12 @@ private def unix_request(path)
end
end

private def unused_port
TCPServer.open(0) do |server|
server.local_address.port
end
end

module HTTP
class Server
describe Response do
Expand Down Expand Up @@ -304,8 +310,9 @@ module HTTP
server = Server.new { }

begin
address = server.bind URI.parse("tcp://127.0.0.1:8081")
address.should eq Socket::IPAddress.new("127.0.0.1", 8081)
port = unused_port
address = server.bind URI.parse("tcp://127.0.0.1:#{port}")
address.should eq Socket::IPAddress.new("127.0.0.1", port)
ensure
server.close
end
Expand All @@ -315,8 +322,9 @@ module HTTP
server = Server.new { }

begin
address = server.bind "tcp://127.0.0.1:8081"
address.should eq Socket::IPAddress.new("127.0.0.1", 8081)
port = unused_port
address = server.bind "tcp://127.0.0.1:#{port}"
address.should eq Socket::IPAddress.new("127.0.0.1", port)
ensure
server.close
end
Expand All @@ -326,8 +334,9 @@ module HTTP
server = Server.new { }

begin
address = server.bind "tcp://127.0.0.1:8081"
address.should eq Socket::IPAddress.new("127.0.0.1", 8081)
port = unused_port
address = server.bind "tcp://127.0.0.1:#{port}"
address.should eq Socket::IPAddress.new("127.0.0.1", port)
ensure
server.close
end
Expand All @@ -340,12 +349,13 @@ module HTTP
certificate = datapath("openssl", "openssl.crt")

begin
port = unused_port
expect_raises(ArgumentError, "missing CA certificate") do
server.bind "ssl://127.0.0.1:8081?key=#{private_key}&cert=#{certificate}&verify_mode=force-peer"
server.bind "ssl://127.0.0.1:#{port}?key=#{private_key}&cert=#{certificate}&verify_mode=force-peer"
end

address = server.bind "ssl://127.0.0.1:8081?key=#{private_key}&cert=#{certificate}&ca=#{certificate}"
address.should eq Socket::IPAddress.new("127.0.0.1", 8081)
address = server.bind "ssl://127.0.0.1:#{port}?key=#{private_key}&cert=#{certificate}&ca=#{certificate}"
address.should eq Socket::IPAddress.new("127.0.0.1", port)
ensure
server.close
end
Expand Down