@@ -29,13 +29,13 @@ def self.included(mod)
2929end
3030
3131# Launches an instance of a given API server. The server
32- # will launch on the default settings of localhost port 9000 .
32+ # will launch on the specified port.
3333#
3434# @param api [Class] The API class to launch
3535# @param port [Integer] The port to run the server on
3636# @param options [Hash] The options hash to provide to the server
3737# @return [Goliath::Server] The executed server
38- def server ( api , port = 9000 , options = { } , &blk )
38+ def server ( api , port , options = { } , &blk )
3939op = OptionParser . new
4040
4141s = Goliath ::Server . new
@@ -44,7 +44,7 @@ def server(api, port = 9000, options ={}, &blk)
4444s . app = Goliath ::Rack ::Builder . build ( api , s . api )
4545s . api . options_parser ( op , options )
4646s . options = options
47- s . port = port
47+ s . port = @test_server_port = port
4848s . start ( &blk )
4949s
5050end
@@ -64,7 +64,7 @@ def stop
6464# @param blk [Proc] The code to execute after the server is launched.
6565# @note This will not return until stop is called.
6666def with_api ( api , options = { } , &blk )
67- server ( api , 9000 , options , &blk )
67+ server ( api , options . delete ( :port ) || 9900 , options , &blk )
6868end
6969
7070# Helper method to setup common callbacks for various request methods.
@@ -90,8 +90,7 @@ def hookup_request_callbacks(req, errback, &blk)
9090# @param errback [Proc] An error handler to attach
9191# @param blk [Proc] The callback block to execute
9292def head_request ( request_data = { } , errback = nil , &blk )
93- path = request_data . delete ( :path ) || ''
94- req = EM ::HttpRequest . new ( "http://localhost:9000#{ path } " ) . head ( request_data )
93+ req = test_request ( request_data ) . head ( request_data )
9594hookup_request_callbacks ( req , errback , &blk )
9695end
9796
@@ -101,8 +100,7 @@ def head_request(request_data ={}, errback = nil, &blk)
101100# @param errback [Proc] An error handler to attach
102101# @param blk [Proc] The callback block to execute
103102def get_request ( request_data = { } , errback = nil , &blk )
104- path = request_data . delete ( :path ) || ''
105- req = EM ::HttpRequest . new ( "http://localhost:9000#{ path } " ) . get ( request_data )
103+ req = test_request ( request_data ) . get ( request_data )
106104hookup_request_callbacks ( req , errback , &blk )
107105end
108106
@@ -112,8 +110,7 @@ def get_request(request_data ={}, errback = nil, &blk)
112110# @param errback [Proc] An error handler to attach
113111# @param blk [Proc] The callback block to execute
114112def post_request ( request_data = { } , errback = nil , &blk )
115- path = request_data . delete ( :path ) || ''
116- req = EM ::HttpRequest . new ( "http://localhost:9000#{ path } " ) . post ( request_data )
113+ req = test_request ( request_data ) . post ( request_data )
117114hookup_request_callbacks ( req , errback , &blk )
118115end
119116
@@ -123,8 +120,7 @@ def post_request(request_data ={}, errback = nil, &blk)
123120# @param errback [Proc] An error handler to attach
124121# @param blk [Proc] The callback block to execute
125122def put_request ( request_data = { } , errback = nil , &blk )
126- path = request_data . delete ( :path ) || ''
127- req = EM ::HttpRequest . new ( "http://localhost:9000#{ path } " ) . put ( request_data )
123+ req = test_request ( request_data ) . put ( request_data )
128124hookup_request_callbacks ( req , errback , &blk )
129125end
130126
@@ -134,9 +130,13 @@ def put_request(request_data ={}, errback = nil, &blk)
134130# @param errback [Proc] An error handler to attach
135131# @param blk [Proc] The callback block to execute
136132def delete_request ( request_data = { } , errback = nil , &blk )
137- path = request_data . delete ( :path ) || ''
138- req = EM ::HttpRequest . new ( "http://localhost:9000#{ path } " ) . delete ( request_data )
133+ req = test_request ( request_data ) . delete ( request_data )
139134hookup_request_callbacks ( req , errback , &blk )
140135end
136+
137+ def test_request ( request_data )
138+ path = request_data . delete ( :path ) || ''
139+ EM ::HttpRequest . new ( "http://localhost:#{ @test_server_port } #{ path } " )
140+ end
141141end
142142end
0 commit comments