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.
101 102 103 104 105 |
# File 'lib/lowdown/mock.rb', line 101 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.
90 91 92 |
# File 'lib/lowdown/mock.rb', line 90 def pool_keep_alive @pool_keep_alive end |
#pool_size ⇒ Fixnum
Returns the number of workers in a pool.
95 96 97 |
# File 'lib/lowdown/mock.rb', line 95 def pool_size @pool_size end |
#requests ⇒ Array<Request> (readonly)
Returns a list of requests that have been made in order.
79 80 81 |
# File 'lib/lowdown/mock.rb', line 79 def requests @requests end |
#responses ⇒ Array<Response> (readonly)
Returns a list of stubbed responses to return in order.
84 85 86 |
# File 'lib/lowdown/mock.rb', line 84 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.
133 134 135 |
# File 'lib/lowdown/mock.rb', line 133 def ssl_context @ssl_context end |
#uri ⇒ URI (readonly)
Returns the details to connect to the APN service.
129 130 131 |
# File 'lib/lowdown/mock.rb', line 129 def uri @uri end |
Class Method Details
.pool(size:, args:) ⇒ Object
137 138 139 140 141 |
# File 'lib/lowdown/mock.rb', line 137 def self.pool(size:, args:) connection = new(*args) connection.pool_size = size connection end |
Instance Method Details
#alive? ⇒ Boolean
147 148 149 |
# File 'lib/lowdown/mock.rb', line 147 def alive? true end |
#async ⇒ Object
143 144 145 |
# File 'lib/lowdown/mock.rb', line 143 def async self end |
#connect ⇒ void
This method returns an undefined value.
Changes #connected? to return true.
180 181 182 |
# File 'lib/lowdown/mock.rb', line 180 def connect @connected = true end |
#connected? ⇒ Boolean
194 195 196 |
# File 'lib/lowdown/mock.rb', line 194 def connected? !!@connected end |
#disconnect ⇒ void
This method returns an undefined value.
Changes #connected? to return false.
188 189 190 |
# File 'lib/lowdown/mock.rb', line 188 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.
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/lowdown/mock.rb', line 163 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.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/lowdown/mock.rb', line 110 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 |