Class: VanilliClient
- Inherits:
-
Object
- Object
- VanilliClient
- Defined in:
- lib/vanilli/client.rb
Overview
Provides ruby bindings for the vanilli client. API is a “rubified” (i.e. snake-case for camel-case) version of the default javascript API provided with vanilli.
Defined Under Namespace
Classes: Stub
Instance Method Summary collapse
-
#clear ⇒ Object
Clears the vanilli server of all stubs.
-
#dump ⇒ Object
Verifies that all vanilli expectations have been met.
-
#expect(expectation) ⇒ Object
Registers the specified Stub as an expectation on the vanilli server.
-
#get_capture(capture_id) ⇒ Object
Pulls back details of the last request that was logged against the specified capture id.
-
#get_captures(capture_id) ⇒ Object
Pulls back details of all requests that were logged against the specified capture id.
-
#initialize(port) ⇒ VanilliClient
constructor
A new instance of VanilliClient.
-
#on_delete(url, query: nil, headers: nil, priority: nil) ⇒ Object
Creates a Stub for a DELETE request that will be matched against the specified criteria.
-
#on_get(url, query: nil, headers: nil, priority: nil) ⇒ Object
Creates a Stub for a GET request that will be matched against the specified criteria.
-
#on_head(url, query: nil, headers: nil, priority: nil) ⇒ Object
Creates a Stub for a HEAD request that will be matched against the specified criteria.
-
#on_post(url, query: nil, headers: nil, priority: nil, content_type: nil, body: nil) ⇒ Object
Creates a Stub for a POST request that will be matched against the specified criteria.
-
#on_put(url, query: nil, headers: nil, priority: nil, content_type: nil, body: nil) ⇒ Object
Creates a Stub for a PUT request that will be matched against the specified criteria.
-
#on_request(method, url, content_type: nil, body: nil, query: nil, headers: nil, priority: nil) ⇒ Object
Create a new Stub that will be matched against the specified criteria.
-
#stub(*stubs) ⇒ Object
Registers the specified Stub(s) with the vanilli server.
-
#verify ⇒ Object
Verifies that all vanilli expectations have been met.
Constructor Details
#initialize(port) ⇒ VanilliClient
Returns a new instance of VanilliClient.
42 43 44 |
# File 'lib/vanilli/client.rb', line 42 def initialize(port) @port = port end |
Instance Method Details
#clear ⇒ Object
Clears the vanilli server of all stubs.
168 169 170 171 172 |
# File 'lib/vanilli/client.rb', line 168 def clear RestClient.delete "http://localhost:#{@port}/_vanilli/stubs" rescue => e raise e.response end |
#dump ⇒ Object
Verifies that all vanilli expectations have been met. If not, an error is thrown.
206 207 208 209 210 |
# File 'lib/vanilli/client.rb', line 206 def dump puts RestClient.get("http://localhost:#{@port}/_vanilli/dump") rescue => e raise e.response end |
#expect(expectation) ⇒ Object
Registers the specified Stub as an expectation on the vanilli server.
160 161 162 163 164 165 |
# File 'lib/vanilli/client.rb', line 160 def expect(expectation) expectation.expect = true expectation.ensure_times_exists stub(expectation) end |
#get_capture(capture_id) ⇒ Object
Pulls back details of the last request that was logged against the specified capture id.
200 201 202 |
# File 'lib/vanilli/client.rb', line 200 def get_capture(capture_id) get_captures(capture_id).last end |
#get_captures(capture_id) ⇒ Object
Pulls back details of all requests that were logged against the specified capture id.
188 189 190 191 192 193 194 195 196 |
# File 'lib/vanilli/client.rb', line 188 def get_captures(capture_id) return JSON.parse(RestClient.get "http://localhost:#{@port}/_vanilli/captures/#{capture_id}") rescue RestClient::ResourceNotFound return [] rescue => e raise e.response end |
#on_delete(url, query: nil, headers: nil, priority: nil) ⇒ Object
Creates a Stub for a DELETE request that will be matched against the specified criteria.
137 138 139 |
# File 'lib/vanilli/client.rb', line 137 def on_delete(url, query: nil, headers: nil, priority: nil) on_request('DELETE', url, query: query, headers: headers, priority: priority) end |
#on_get(url, query: nil, headers: nil, priority: nil) ⇒ Object
Creates a Stub for a GET request that will be matched against the specified criteria.
119 120 121 |
# File 'lib/vanilli/client.rb', line 119 def on_get(url, query: nil, headers: nil, priority: nil) on_request('GET', url, query: query, headers: headers, priority: priority) end |
#on_head(url, query: nil, headers: nil, priority: nil) ⇒ Object
Creates a Stub for a HEAD request that will be matched against the specified criteria.
143 144 145 |
# File 'lib/vanilli/client.rb', line 143 def on_head(url, query: nil, headers: nil, priority: nil) on_request('HEAD', url, query: query, headers: headers, priority: priority) end |
#on_post(url, query: nil, headers: nil, priority: nil, content_type: nil, body: nil) ⇒ Object
Creates a Stub for a POST request that will be matched against the specified criteria.
125 126 127 |
# File 'lib/vanilli/client.rb', line 125 def on_post(url, query: nil, headers: nil, priority: nil, content_type: nil, body: nil) on_request('POST', url, query: query, headers: headers, priority: priority, content_type: content_type, body: body) end |
#on_put(url, query: nil, headers: nil, priority: nil, content_type: nil, body: nil) ⇒ Object
Creates a Stub for a PUT request that will be matched against the specified criteria.
131 132 133 |
# File 'lib/vanilli/client.rb', line 131 def on_put(url, query: nil, headers: nil, priority: nil, content_type: nil, body: nil) on_request('PUT', url, query: query, headers: headers, priority: priority, content_type: content_type, body: body) end |
#on_request(method, url, content_type: nil, body: nil, query: nil, headers: nil, priority: nil) ⇒ Object
Create a new Stub that will be matched against the specified criteria.
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/vanilli/client.rb', line 104 def on_request(method, url, content_type: nil, body: nil, query: nil, headers: nil, priority: nil) fail 'Url is missing.' unless url fail_body_with_no_contenttype(body, content_type) Stub.new(criteria: stringify_non_json_content(method: method, url: unwrap_regex(url), contentType: content_type, body: body, query: map_over_hash(query, :unwrap_regex), headers: map_over_hash(headers, :unwrap_regex)), priority: priority) end |
#stub(*stubs) ⇒ Object
Registers the specified Stub(s) with the vanilli server.
148 149 150 151 152 153 154 155 156 |
# File 'lib/vanilli/client.rb', line 148 def stub(*stubs) stubs.each do |stub| begin RestClient.post "http://localhost:#{@port}/_vanilli/stubs", stub.to_json, content_type: :json, accept: :json rescue => e raise e.response end end end |
#verify ⇒ Object
Verifies that all vanilli expectations have been met. If not, an error is thrown.
176 177 178 179 180 181 182 183 184 |
# File 'lib/vanilli/client.rb', line 176 def verify begin res = JSON.parse(RestClient.get "http://localhost:#{@port}/_vanilli/verify") rescue => e raise e.response end fail 'VERIFICATION FAILED: ' + res['errors'].join('\n') if res['errors'].length > 0 end |