class Lowdown::Mock::Connection
A mock object that can be used instead of a real Connection
object.
Constants
- Request
Represents a recorded request.
Attributes
@return [Boolean]
whether or not the connection should be opened on initialization. In a pool this basically equals the `keep_alive` Client option.
@return [Fixnum]
the number of workers in a pool.
@return [Array<Request>]
a list of requests that have been made in order.
@return [Array<Response>]
a list of stubbed responses to return in order.
@return (see Lowdown::Connection#ssl_context
)
@return (see Lowdown::Connection#uri
)
Public Class Methods
@param (see Lowdown::Connection#initialize)
# File lib/lowdown/mock.rb, line 107 def initialize(uri = nil, ssl_context = nil, connect = true) @uri, @ssl_context, @pool_keep_alive = uri, ssl_context, connect @responses = [] @requests = [] end
@!group Celluloid API
# File lib/lowdown/mock.rb, line 143 def self.pool(size:, args:) connection = new(*args) connection.pool_size = size connection end
Public Instance Methods
# File lib/lowdown/mock.rb, line 153 def alive? true end
# File lib/lowdown/mock.rb, line 149 def async self end
Changes {#connected?} to return `true`.
@return [void]
# File lib/lowdown/mock.rb, line 186 def connect @connected = true end
@return (see Lowdown::Connection#connected?
)
# File lib/lowdown/mock.rb, line 200 def connected? !!@connected end
Changes {#connected?} to return `false`.
@return [void]
# File lib/lowdown/mock.rb, line 194 def disconnect @connected = false end
Yields stubbed {#responses} or if none are available defaults to success responses. It does this on a different thread, just like the real API does.
To make the connection simulate being closed from the other end, specify the `test-close-connection` header.
@param (see Lowdown::Connection#post
) @yield (see Lowdown::Connection#post
) @yieldparam (see Lowdown::Connection#post
) @return (see Lowdown::Connection#post
)
# File lib/lowdown/mock.rb, line 169 def post(path:, headers:, body:, delegate:, context: nil) raise "First open the connection." unless @connected unless headers["test-close-connection"] response = @responses.shift || Response.new(":status" => "200", "apns-id" => headers["apns-id"]) end @requests << Request.new(path, headers, body, response, delegate, context) raise EOFError, "Stubbed EOF" if headers["test-close-connection"] delegate.handle_apns_response(response, context: context) end
@return [Array<Notification>]
returns the recorded requests as Notification objects.
# File lib/lowdown/mock.rb, line 116 def requests_as_notifications @requests.map do |request| headers = request.headers hash = { :token => File.basename(request.path), :id => request.response.id, :payload => JSON.parse(request.body), :topic => headers["apns-topic"], } hash[:expiration] = Time.at(headers["apns-expiration"].to_i) if headers["apns-expiration"] hash[:priority] = headers["apns-priority"].to_i if headers["apns-priority"] Notification.new(hash) end end