Class: Lowdown::Mock::Connection
- Inherits:
-
Object
- Object
- Lowdown::Mock::Connection
- 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
-
#pool_keep_alive ⇒ Boolean
readonly
Whether or not the connection should be opened on initialization.
-
#pool_size ⇒ Fixnum
The number of workers in a pool.
-
#requests ⇒ Array<Request>
readonly
A list of requests that have been made in order.
-
#responses ⇒ Array<Response>
readonly
A list of stubbed responses to return in order.
Real API: Instance Attribute Summary collapse
-
#ssl_context ⇒ OpenSSL::SSL::SSLContext
readonly
A SSL context, configured with the certificate/key pair, which is used to connect to the APN service.
-
#uri ⇒ URI
readonly
The details to connect to the APN service.
Mock API: Instance Method Summary collapse
-
#initialize(uri = nil, ssl_context = nil, connect = true) ⇒ Connection
constructor
A new instance of Connection.
-
#requests_as_notifications ⇒ Array<Notification>
Returns the recorded requests as Notification objects.
Celluloid API collapse
Real API: Instance Method Summary collapse
-
#connect ⇒ void
Changes #connected? to return
true
. - #connected? ⇒ Boolean
-
#disconnect ⇒ void
Changes #connected? to return
false
. -
#post(path:, headers:, body:, delegate:, context: nil) ⇒ void
Yields stubbed #responses or if none are available defaults to success responses.
Constructor Details
#initialize(uri = nil, ssl_context = nil, connect = true) ⇒ Connection
Returns a new instance of Connection.
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_alive ⇒ Boolean (readonly)
Returns 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_size ⇒ Fixnum
Returns the number of workers in a pool.
101 102 103 |
# File 'lib/lowdown/mock.rb', line 101 def pool_size @pool_size end |
#requests ⇒ Array<Request> (readonly)
Returns a list of requests that have been made in order.
85 86 87 |
# File 'lib/lowdown/mock.rb', line 85 def requests @requests end |
#responses ⇒ Array<Response> (readonly)
Returns a list of stubbed responses to return in order.
90 91 92 |
# File 'lib/lowdown/mock.rb', line 90 def responses @responses end |
#ssl_context ⇒ OpenSSL::SSL::SSLContext (readonly)
Returns 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 |
#uri ⇒ URI (readonly)
Returns 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
153 154 155 |
# File 'lib/lowdown/mock.rb', line 153 def alive? true end |
#async ⇒ Object
149 150 151 |
# File 'lib/lowdown/mock.rb', line 149 def async self end |
#connect ⇒ void
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
200 201 202 |
# File 'lib/lowdown/mock.rb', line 200 def connected? !!@connected end |
#disconnect ⇒ void
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.
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_notifications ⇒ 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 |