Class: Mongrel2::RequestFactory
- Inherits:
-
Object
- Object
- Mongrel2::RequestFactory
- Extended by:
- Loggability
- Defined in:
- lib/mongrel2/testing.rb
Overview
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
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_TEST_UUID =
Default testing UUID (sender_id)
'BD17D85C-4730-4BF2-999D-9D2B2E0FCCF9'
- DEFAULT_CONN_ID =
Default connection ID
0
- TEST_SEND_SPEC =
0mq socket specifications for Handlers
'tcp://127.0.0.1:9998'
- TEST_RECV_SPEC =
'tcp://127.0.0.1:9997'
- DEFAULT_TESTING_URL =
The testing URL to use by default
URI( 'http://localhost:8080/a_handler' )
- DEFAULT_TESTING_HOST =
DEFAULT_TESTING_URL.host
- DEFAULT_TESTING_PORT =
DEFAULT_TESTING_URL.port
- DEFAULT_TESTING_ROUTE =
DEFAULT_TESTING_URL.path
- DEFAULT_TESTING_HEADERS =
The default set of headers used for HTTP requests
Mongrel2::Table.new( 'x-forwarded-for' => '127.0.0.1', 'accept-language' => 'en-US,en;q=0.8', 'accept-encoding' => 'gzip,deflate,sdch', 'connection' => 'keep-alive', 'accept-charset' => 'UTF-8,*;q=0.5', 'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) ' + 'AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 ' + 'Safari/535.1', 'url-scheme' => 'http', 'VERSION' => 'HTTP/1.1', )
- DEFAULT_FACTORY_CONFIG =
The defaults used by the HTTP request factory
{ :sender_id => DEFAULT_TEST_UUID, :conn_id => DEFAULT_CONN_ID, :host => DEFAULT_TESTING_HOST, :port => DEFAULT_TESTING_PORT, :route => DEFAULT_TESTING_ROUTE, :headers => DEFAULT_TESTING_HEADERS, }
Instance Attribute Summary collapse
-
#conn_id ⇒ Object
Returns the value of attribute conn_id.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#route ⇒ Object
Returns the value of attribute route.
-
#sender_id ⇒ Object
Returns the value of attribute sender_id.
Class Method Summary collapse
-
.default_factory_config ⇒ Object
Return the default configuration for the receiving factory class.
-
.default_headers ⇒ Object
Return the default testing headers hash for the receiving class.
Instance Method Summary collapse
-
#delete(uri, headers = {}) ⇒ Object
Create a new DELETE Mongrel2::Request for the specified
uri
with the givenheaders
. -
#get(uri, headers = {}) ⇒ Object
Create a new GET Mongrel2::Request for the specified
uri
andheaders
. -
#head(uri, headers = {}) ⇒ Object
Create a new HEAD Mongrel2::Request for the specified
uri
andheaders
. -
#initialize(config = {}) ⇒ RequestFactory
constructor
Create a new RequestFactory with the given
config
, which will be merged with DEFAULT_FACTORY_CONFIG. -
#options(uri, headers = {}) ⇒ Object
Create a new OPTIONS Mongrel2::Request with the specified
uri
andheaders
. -
#post(uri, body = '', headers = {}) ⇒ Object
Create a new POST Mongrel2::Request for the specified
uri
with the givenbody
andheaders
. -
#put(uri, body = '', headers = {}) ⇒ Object
Create a new PUT Mongrel2::Request for the specified
uri
with the givenbody
andheaders
.
Constructor Details
#initialize(config = {}) ⇒ RequestFactory
Create a new RequestFactory with the given config
, which will be merged with DEFAULT_FACTORY_CONFIG.
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/mongrel2/testing.rb', line 160 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] @conn_id = config[:conn_id] @host = config[:host] @port = config[:port] @route = config[:route] @headers = Mongrel2::Table.new( config[:headers] ) end |
Instance Attribute Details
#conn_id ⇒ Object
Returns the value of attribute conn_id.
177 178 179 |
# File 'lib/mongrel2/testing.rb', line 177 def conn_id @conn_id end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
178 179 180 |
# File 'lib/mongrel2/testing.rb', line 178 def headers @headers end |
#host ⇒ Object
Returns the value of attribute host.
177 178 179 |
# File 'lib/mongrel2/testing.rb', line 177 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
177 178 179 |
# File 'lib/mongrel2/testing.rb', line 177 def port @port end |
#route ⇒ Object
Returns the value of attribute route.
177 178 179 |
# File 'lib/mongrel2/testing.rb', line 177 def route @route end |
#sender_id ⇒ Object
Returns the value of attribute sender_id.
177 178 179 |
# File 'lib/mongrel2/testing.rb', line 177 def sender_id @sender_id end |
Class Method Details
.default_factory_config ⇒ Object
Return the default configuration for the receiving factory class.
149 150 151 |
# File 'lib/mongrel2/testing.rb', line 149 def self::default_factory_config return const_get( :DEFAULT_FACTORY_CONFIG ) end |
.default_headers ⇒ Object
Return the default testing headers hash for the receiving class.
143 144 145 |
# File 'lib/mongrel2/testing.rb', line 143 def self::default_headers return const_get( :DEFAULT_TESTING_HEADERS ) end |
Instance Method Details
#delete(uri, headers = {}) ⇒ Object
Create a new DELETE Mongrel2::Request for the specified uri
with the given headers
.
252 253 254 255 256 257 258 259 260 |
# File 'lib/mongrel2/testing.rb', line 252 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 |
#get(uri, headers = {}) ⇒ Object
Create a new GET Mongrel2::Request for the specified uri
and headers
.
197 198 199 200 201 202 203 204 205 |
# File 'lib/mongrel2/testing.rb', line 197 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 |
#head(uri, headers = {}) ⇒ Object
Create a new HEAD Mongrel2::Request for the specified uri
and headers
.
209 210 211 212 213 214 215 216 217 |
# File 'lib/mongrel2/testing.rb', line 209 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 |
#options(uri, headers = {}) ⇒ Object
Create a new OPTIONS Mongrel2::Request with the specified uri
and headers
.
185 186 187 188 189 190 191 192 193 |
# File 'lib/mongrel2/testing.rb', line 185 def ( 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 |
#post(uri, body = '', headers = {}) ⇒ Object
Create a new POST Mongrel2::Request for the specified uri
with the given body
and headers
.
222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/mongrel2/testing.rb', line 222 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 |
#put(uri, body = '', headers = {}) ⇒ Object
Create a new PUT Mongrel2::Request for the specified uri
with the given body
and headers
.
237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/mongrel2/testing.rb', line 237 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 |