Class: VanilliClient

Inherits:
Object
  • Object
show all
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

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

#clearObject

Clears the vanilli server of all stubs.



162
163
164
165
166
# File 'lib/vanilli/client.rb', line 162

def clear
  RestClient.delete "http://localhost:#{@port}/_vanilli/stubs"
rescue => e
  raise e.response
end

#dumpObject

Verifies that all vanilli expectations have been met. If not, an error is thrown.



200
201
202
203
204
# File 'lib/vanilli/client.rb', line 200

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.



154
155
156
157
158
159
# File 'lib/vanilli/client.rb', line 154

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.



194
195
196
# File 'lib/vanilli/client.rb', line 194

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.



182
183
184
185
186
187
188
189
190
# File 'lib/vanilli/client.rb', line 182

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_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.



142
143
144
145
146
147
148
149
150
# File 'lib/vanilli/client.rb', line 142

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

#verifyObject

Verifies that all vanilli expectations have been met. If not, an error is thrown.



170
171
172
173
174
175
176
177
178
# File 'lib/vanilli/client.rb', line 170

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