A factory for generating WebSocket
request objects for testing.
The defaults used by the websocket request factory
The default WebSocket
opcode
Default headers
The default host
Create a new factory using the specified config
.
# File lib/mongrel2/testing.rb, line 336
def initialize( config={} )
config[:headers] = DEFAULT_TESTING_HEADERS.merge( config[:headers] ) if config[:headers]
config = 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 binary frame.
# File lib/mongrel2/testing.rb, line 402
def binary( uri, payload='', *flags )
flags << :binary
return self.create( uri, payload, flags )
end
Create a close frame.
# File lib/mongrel2/testing.rb, line 409
def close( uri, payload='', *flags )
flags << :close << :fin
return self.create( uri, payload, flags )
end
Create a continuation frame.
# File lib/mongrel2/testing.rb, line 388
def continuation( uri, payload='', *flags )
flags << :continuation
return self.create( uri, payload, flags )
end
Create a new request with the specified uri
, data
, and flags
.
# File lib/mongrel2/testing.rb, line 374
def create( uri, data, *flags )
raise "Request doesn't route through %p" % [ self.route ] unless
uri.start_with?( self.route )
headers = if flags.last.is_a?( Hash ) then flags.pop else {} end
flagheader = make_flags_header( flags )
headers = self.make_merged_headers( uri, flagheader, headers )
rclass = Mongrel2::Request.subclass_for_method( :WEBSOCKET )
return rclass.new( self.sender_id, self.conn_id.to_s, self.route, headers, data )
end
Create an initial websocket handshake request and return it.
# File lib/mongrel2/testing.rb, line 354
def handshake( uri, *subprotocols )
raise "Request doesn't route through %p" % [ self.route ] unless
uri.start_with?( self.route )
headers = if subprotocols.last.is_a?( Hash ) then subprotocols.pop else {} end
headers = self.make_merged_headers( uri, 0, headers )
headers.delete( :flags )
unless subprotocols.empty?
protos = subprotocols.map( &:to_s ).join( ', ' )
headers.sec_websocket_protocol = protos
end
rclass = Mongrel2::Request.subclass_for_method( :WEBSOCKET_HANDSHAKE )
return rclass.new( self.sender_id, self.conn_id.to_s, self.route, headers, DEFAULT_HANDSHAKE_BODY.dup )
end
Create a ping frame.
# File lib/mongrel2/testing.rb, line 416
def ping( uri, payload='', *flags )
flags << :ping << :fin
return self.create( uri, payload, flags )
end
Create a pong frame.
# File lib/mongrel2/testing.rb, line 423
def pong( uri, payload='', *flags )
flags << :pong << :fin
return self.create( uri, payload, flags )
end
Create a text frame.
# File lib/mongrel2/testing.rb, line 395
def text( uri, payload='', *flags )
flags << :text
return self.create( uri, payload, flags )
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 437
def make_merged_headers( uri, flags, userheaders )
headers = self.headers.merge( userheaders )
uri = URI( uri )
# Add mongrel headers
headers.uri = uri.to_s
headers.path = uri.path
headers.host = "%s:%d" % [ self.host, self.port ]
headers.query = uri.query if uri.query
headers.pattern = self.route
headers.origin = "http://#{headers.host}"
headers.flags = "0x%02x" % [ flags ]
return headers
end