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

Ensure server port numbers are numeric and ensure they are stored as … #55

Merged
merged 1 commit into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions lib/webrick/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def initialize(config={}, default=Config::General)
@listeners = []
@shutdown_pipe = @config[:ShutdownPipe]
unless @config[:DoNotListen]
raise ArgumentError, "Port must an integer" unless @config[:Port].to_s == @config[:Port].to_i.to_s

@config[:Port] = @config[:Port].to_i
if @config[:Listen]
warn(":Listen option is deprecated; use GenericServer#listen", uplevel: 1)
end
Expand Down
28 changes: 28 additions & 0 deletions test/webrick/test_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,32 @@ def test_shutdown_pipe
pipe.last.puts('')
assert_join_threads([server_thread])
end

def test_port_numbers
config = {
:BindAddress => '0.0.0.0',
:Logger => WEBrick::Log.new([], WEBrick::BasicLog::WARN),
}

ports = [0, "0"]

ports.each do |port|
config[:Port]= port
server = WEBrick::GenericServer.new(config)
server_thread = Thread.start { server.start }
client_thread = Thread.start {
sleep 0.1 until server.status == :Running || !server_thread.status
server_port = server.listeners[0].addr[1]
server.stop
assert_equal server.config[:Port], server_port
sleep 0.1 until server.status == :Stop || !server_thread.status
}
assert_join_threads([client_thread, server_thread])
end

assert_raise(ArgumentError) do
config[:Port]= "FOO"
WEBrick::GenericServer.new(config)
end
end
end