Skip to content

Commit a99d21b

Browse files
authored
Added method to get port of XMLRPC::Server (#18)
Useful when you start the server with port=0 and let Webrick select the port.
1 parent 9048cde commit a99d21b

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

lib/xmlrpc/server.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,11 @@ def shutdown
594594
@server.shutdown
595595
end
596596

597+
# Get the port of the server, useful when started with port=0
598+
def port
599+
@server.config[:Port]
600+
end
601+
597602
end
598603

599604

test/test_server.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# coding: utf-8
2+
# frozen_string_literal: true
3+
4+
require 'test/unit'
5+
require 'stringio'
6+
require 'xmlrpc/client'
7+
require 'xmlrpc/server'
8+
9+
module TestXMLRPC
10+
class Test_XMLRPC_Server < Test::Unit::TestCase
11+
def test_port
12+
s = XMLRPC::Server.new(0, '127.0.0.1', 1, StringIO.new)
13+
refute_equal(0, s.port, 'Selected random port')
14+
15+
s.add_handler('test.add') { |a, b| a + b }
16+
srv_thread = Thread.new { s.serve }
17+
18+
c = XMLRPC::Client.new('127.0.0.1', '/', s.port)
19+
assert_equal(7, c.call('test.add', 3, 4))
20+
21+
s.shutdown
22+
srv_thread.kill
23+
end
24+
end
25+
end

0 commit comments

Comments
 (0)