A factory for generating Mongrel2::Request
objects for testing.
Usage:
require 'mongrel2/testing' describe "MyHandler" do before( :all ) do @factory = Mongrel2::RequestFactory. new( sender_id: 'my-handler', route: '/api/v1', headers: {accept: 'application/json'} ) end before( :each ) do @app = MyHandler.new( 'my-handler', 'tcp://0.0.0.0:5556', 'tcp://0.0.0.0:5555' ) end it "handles a JSON request for GET /" do request = @factory.get( '/api/v1' ) response = @app.dispatch_request( request ) #... end end
Default connection ID
The defaults used by the HTTP request factory
The default set of headers used for HTTP requests
The testing URL to use by default
Default testing UUID (sender_id)
0mq socket specifications for Handlers
Return the default configuration for the receiving factory class.
# File lib/mongrel2/testing.rb, line 148
def self::default_factory_config
return const_get( :DEFAULT_FACTORY_CONFIG )
end
Return the default testing headers hash for the receiving class.
# File lib/mongrel2/testing.rb, line 142
def self::default_headers
return const_get( :DEFAULT_TESTING_HEADERS )
end
Create a new RequestFactory
with the given config
, which will be merged with DEFAULT_FACTORY_CONFIG
.
# File lib/mongrel2/testing.rb, line 159
def initialize( config={} )
config[:headers] = self.class.default_headers.merge( config[:headers] ) if config[:headers]
config = self.class.default_factory_config.merge( config )
@sender_id = config[:sender_id]
@host = config[:host]
@port = config[:port]
@route = config[:route]
@headers = Mongrel2::Table.new( config[:headers] )
@conn_id = 0
end
Create a new DELETE Mongrel2::Request
for the specified uri
with the given headers
.
# File lib/mongrel2/testing.rb, line 251
def delete( uri, headers={} )
raise "Request doesn't route through %p" % [ self.route ] unless
uri.start_with?( self.route )
headers = self.make_merged_headers( :DELETE, uri, headers )
rclass = Mongrel2::Request.subclass_for_method( :DELETE )
return rclass.new( self.sender_id, self.conn_id.to_s, uri.to_s, headers )
end
Create a new GET Mongrel2::Request
for the specified uri
and headers
.
# File lib/mongrel2/testing.rb, line 196
def get( uri, headers={} )
raise "Request doesn't route through %p" % [ self.route ] unless
uri.start_with?( self.route )
headers = self.make_merged_headers( :GET, uri, headers )
rclass = Mongrel2::Request.subclass_for_method( :GET )
return rclass.new( self.sender_id, self.conn_id.to_s, uri.to_s, headers )
end
Create a new HEAD Mongrel2::Request
for the specified uri
and headers
.
# File lib/mongrel2/testing.rb, line 208
def head( uri, headers={} )
raise "Request doesn't route through %p" % [ self.route ] unless
uri.start_with?( self.route )
headers = self.make_merged_headers( :HEAD, uri, headers )
rclass = Mongrel2::Request.subclass_for_method( :HEAD )
return rclass.new( self.sender_id, self.conn_id.to_s, uri.to_s, headers )
end
Create a new OPTIONS Mongrel2::Request
with the specified uri
and headers
.
# File lib/mongrel2/testing.rb, line 184
def options( uri, headers={} )
raise "Request doesn't route through %p" % [ self.route ] unless
uri.start_with?( self.route )
headers = self.make_merged_headers( :OPTIONS, uri, headers )
rclass = Mongrel2::Request.subclass_for_method( :OPTIONS )
return rclass.new( self.sender_id, self.conn_id.to_s, uri.to_s, headers )
end
Create a new POST Mongrel2::Request
for the specified uri
with the given body
and headers
.
# File lib/mongrel2/testing.rb, line 221
def post( uri, body='', headers={} )
raise "Request doesn't route through %p" % [ self.route ] unless
uri.start_with?( self.route )
headers = self.make_merged_headers( :POST, uri, headers )
rclass = Mongrel2::Request.subclass_for_method( :POST )
req = rclass.new( self.sender_id, self.conn_id.to_s, uri.to_s, headers, body )
return req
end
Create a new PUT Mongrel2::Request
for the specified uri
with the given body
and headers
.
# File lib/mongrel2/testing.rb, line 236
def put( uri, body='', headers={} )
raise "Request doesn't route through %p" % [ self.route ] unless
uri.start_with?( self.route )
headers = self.make_merged_headers( :PUT, uri, headers )
rclass = Mongrel2::Request.subclass_for_method( :PUT )
req = rclass.new( self.sender_id, self.conn_id.to_s, uri.to_s, headers, body )
return req
end
Merge the factory's headers with userheaders
, and then merge in the special headers that Mongrel2
adds that are based on the uri
and other server attributes.
# File lib/mongrel2/testing.rb, line 269
def make_merged_headers( verb, uri, userheaders )
headers = self.headers.merge( userheaders )
uri = URI( uri )
# Add mongrel headers
headers.uri = uri.to_s
headers.path = uri.path
headers['METHOD'] = verb.to_s
headers.host = "%s:%d" % [ self.host, self.port ]
headers.query = uri.query if uri.query
headers.pattern = self.route
return headers
end