Skip to content

Commit

Permalink
Fix HTTP::Server specs to use free ports in expectations (#6530)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored and sdogruyol committed Aug 12, 2018
1 parent f09121e commit 89a7718
Showing 1 changed file with 20 additions and 10 deletions.
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

0 comments on commit 89a7718

Please sign in to comment.