class Lowdown::Mock::Connection

A mock object that can be used instead of a real Connection object.

Constants

Request

Represents a recorded request.

Attributes

pool_keep_alive[R]

@return [Boolean]

whether or not the connection should be opened on initialization. In a pool this basically equals the
`keep_alive` Client option.
pool_size[RW]

@return [Fixnum]

the number of workers in a pool.
requests[R]

@return [Array<Request>]

a list of requests that have been made in order.
responses[R]

@return [Array<Response>]

a list of stubbed responses to return in order.
ssl_context[R]
uri[R]

@return (see Lowdown::Connection#uri)

Public Class Methods

new(uri = nil, ssl_context = nil, connect = true) click to toggle source

@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
pool(size:, args:) click to toggle source

@!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

alive?() click to toggle source
# File lib/lowdown/mock.rb, line 153
def alive?
  true
end
async() click to toggle source
# File lib/lowdown/mock.rb, line 149
def async
  self
end
connect() click to toggle source

Changes {#connected?} to return `true`.

@return [void]

# File lib/lowdown/mock.rb, line 186
def connect
  @connected = true
end
connected?() click to toggle source

@return (see Lowdown::Connection#connected?)

# File lib/lowdown/mock.rb, line 200
def connected?
  !!@connected
end
disconnect() click to toggle source

Changes {#connected?} to return `false`.

@return [void]

# File lib/lowdown/mock.rb, line 194
def disconnect
  @connected = false
end
post(path:, headers:, body:, delegate:, context: nil) click to toggle source

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
requests_as_notifications() click to toggle source

@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