Class: Lowdown::Mock::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/lowdown/mock.rb

Overview

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

Defined Under Namespace

Classes: Request

Mock API: Instance Attribute Summary collapse

Real API: Instance Attribute Summary collapse

Mock API: Instance Method Summary collapse

Celluloid API collapse

Real API: Instance Method Summary collapse

Constructor Details

#initialize(uri = nil, ssl_context = nil, connect = true) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • uri (URI, String) (defaults to: nil)

    the details to connect to the APN service.

  • ssl_context (OpenSSL::SSL::SSLContext) (defaults to: nil)

    a SSL context, configured with the certificate/key pair, which is used to connect to the APN service.

  • connect (Boolean) (defaults to: true)

    whether or not to immediately connect on initialization.

  • socket_maker (lambda)

    a lambda takes uri and returns duck type of TCPSocket e.g.:



107
108
109
110
111
# 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

Instance Attribute Details

#pool_keep_aliveBoolean (readonly)

Returns whether or not the connection should be opened on initialization. In a pool this basically equals the keep_alive Client option.

Returns:

  • (Boolean)

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



96
97
98
# File 'lib/lowdown/mock.rb', line 96

def pool_keep_alive
  @pool_keep_alive
end

#pool_sizeFixnum

Returns the number of workers in a pool.

Returns:

  • (Fixnum)

    the number of workers in a pool.



101
102
103
# File 'lib/lowdown/mock.rb', line 101

def pool_size
  @pool_size
end

#requestsArray<Request> (readonly)

Returns a list of requests that have been made in order.

Returns:

  • (Array<Request>)

    a list of requests that have been made in order.



85
86
87
# File 'lib/lowdown/mock.rb', line 85

def requests
  @requests
end

#responsesArray<Response> (readonly)

Returns a list of stubbed responses to return in order.

Returns:

  • (Array<Response>)

    a list of stubbed responses to return in order.



90
91
92
# File 'lib/lowdown/mock.rb', line 90

def responses
  @responses
end

#ssl_contextOpenSSL::SSL::SSLContext (readonly)

Returns a SSL context, configured with the certificate/key pair, which is used to connect to the APN service.

Returns:

  • (OpenSSL::SSL::SSLContext)

    a SSL context, configured with the certificate/key pair, which is used to connect to the APN service.



139
140
141
# File 'lib/lowdown/mock.rb', line 139

def ssl_context
  @ssl_context
end

#uriURI (readonly)

Returns the details to connect to the APN service.

Returns:

  • (URI)

    the details to connect to the APN service.



135
136
137
# File 'lib/lowdown/mock.rb', line 135

def uri
  @uri
end

Class Method Details

.pool(size:, args:) ⇒ Object



143
144
145
146
147
# File 'lib/lowdown/mock.rb', line 143

def self.pool(size:, args:)
  connection = new(*args)
  connection.pool_size = size
  connection
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/lowdown/mock.rb', line 153

def alive?
  true
end

#asyncObject



149
150
151
# File 'lib/lowdown/mock.rb', line 149

def async
  self
end

#connectvoid

This method returns an undefined value.

Changes #connected? to return true.



186
187
188
# File 'lib/lowdown/mock.rb', line 186

def connect
  @connected = true
end

#connected?Boolean

Returns:

  • (Boolean)
  • (Boolean)

    whether or not the Connection is open.



200
201
202
# File 'lib/lowdown/mock.rb', line 200

def connected?
  !!@connected
end

#disconnectvoid

This method returns an undefined value.

Changes #connected? to return false.



194
195
196
# File 'lib/lowdown/mock.rb', line 194

def disconnect
  @connected = false
end

#post(path:, headers:, body:, delegate:, context: nil) ⇒ void

This method returns an undefined value.

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.

Parameters:

  • path (String)

    the request path, which should be /3/device/<device-token>.

  • headers (Hash)

    the additional headers for the request. By default it sends :method, :path, and content-length.

  • body (String)

    the (JSON) encoded payload data to send to the service.

  • delegate (DelegateProtocol)

    an object that implements the delegate protocol.

  • context (Object, nil) (defaults to: nil)

    any object that you want to be passed to the delegate once the response is back.

Raises:

  • (EOFError)


169
170
171
172
173
174
175
176
177
178
179
180
# 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_notificationsArray<Notification>

Returns the recorded requests as Notification objects.

Returns:

  • (Array<Notification>)

    returns the recorded requests as Notification objects.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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