Mongrel2::

WebSocketFrameFactory

class
Superclass
Mongrel2::RequestFactory
Included Modules
Mongrel2::Constants

A factory for generating WebSocket request objects for testing.

Constants

DEFAULT_FACTORY_CONFIG

The defaults used by the websocket request factory

DEFAULT_HANDSHAKE_BODY
DEFAULT_OPCODE

The default WebSocket opcode

DEFAULT_TESTING_HEADERS

Default headers

DEFAULT_TESTING_HOST

The default host

DEFAULT_TESTING_PORT
DEFAULT_TESTING_ROUTE

Public Class Methods

anchor
new( config={} )

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

Public Instance Methods

anchor
binary( uri, payload='', *flags )

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
anchor
close( uri, payload='', *flags )

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
anchor
continuation( uri, payload='', *flags )

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
anchor
create( uri, data, *flags )

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
anchor
handshake( uri, *subprotocols )

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
anchor
ping( uri, payload='', *flags )

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
anchor
pong( uri, payload='', *flags )

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
anchor
text( uri, payload='', *flags )

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

Protected Instance Methods

anchor
make_merged_headers( uri, flags, userheaders )

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