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.



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

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.



188
189
190
191
192
193
194
# File 'lib/vanilli/client.rb', line 188

def dump
  begin
    puts RestClient.get("http://localhost:#{@port}/_vanilli/dump")
  rescue => e
    raise e.response
  end
end

#expect(expectation) ⇒ Object

Registers the specified Stub as an expectation on the vanilli server.



147
148
149
150
# File 'lib/vanilli/client.rb', line 147

def expect(expectation)
  expectation.expect = true
  stub(expectation)
end

#get_capture(capture_id) ⇒ Object

Pulls back details of the last request that was logged against the specified capture id.



181
182
183
# File 'lib/vanilli/client.rb', line 181

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.



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

def get_captures(capture_id)
  JSON.parse(RestClient.get "http://localhost:#{@port}/_vanilli/captures/#{capture_id}")
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.



130
131
132
# File 'lib/vanilli/client.rb', line 130

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.



112
113
114
# File 'lib/vanilli/client.rb', line 112

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.



118
119
120
# File 'lib/vanilli/client.rb', line 118

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.



124
125
126
# File 'lib/vanilli/client.rb', line 124

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.



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vanilli/client.rb', line 97

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.



135
136
137
138
139
140
141
142
143
# File 'lib/vanilli/client.rb', line 135

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.



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

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