Class: MockRestService

Inherits:
Object
  • Object
show all
Defined in:
features/support/mock_rest_service.rb

Overview

Mocking Service for testing rest calls

Constant Summary

STANDARD_HEADERS =
{ 'Accept' => '*/*', 'User-Agent' => 'Ruby' }
STANDARD =
'http'
SECURE =
'https'

Instance Method Summary (collapse)

Constructor Details

- (MockRestService) initialize(host, port, protocol = STANDARD)

Returns a new instance of MockRestService



11
12
13
14
15
16
# File 'features/support/mock_rest_service.rb', line 11

def initialize(host, port, protocol = STANDARD)
  @protocol = protocol
  @host = host
  @port = port
  @messages = {}
end

Instance Method Details

- (Object) auth_string(user, password)



40
41
42
# File 'features/support/mock_rest_service.rb', line 40

def auth_string(user, password)
  "#{user}:#{password}@" unless user.nil? || password.nil?
end

- (Object) merge_headers(headers)



36
37
38
# File 'features/support/mock_rest_service.rb', line 36

def merge_headers(headers)
  STANDARD_HEADERS.merge(headers)
end

- (Object) store_get_query(path, headers = {}, user = nil, password = nil)



44
45
46
47
48
49
50
51
52
# File 'features/support/mock_rest_service.rb', line 44

def store_get_query(path, headers = {}, user = nil, password = nil)
  new_headers = STANDARD_HEADERS.merge(headers)
  message = path.split('?').last
  auth_string = "#{user}:#{password}@" unless user.nil? || password.nil?
  WebMock.stub_request(:get,
                       "#{@protocol}://#{auth_string}#{@host}:#{@port}#{path}")
    .with(headers: new_headers)
    .to_return({ body: "#{message}", status: 200 }, headers: {})
end

- (Object) store_msg(type, path, message, headers = {}, user = nil, password = nil, body = nil)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'features/support/mock_rest_service.rb', line 18

def store_msg(type, path, message, headers = {},
              user = nil, password = nil, body = nil)
  url = "#{@protocol}://#{auth_string(user, password)}#{@host}:#{@port}#{path}"
  case type.downcase
  when 'get', 'delete'
    WebMock.stub_request(type.downcase.to_sym, url)
      .with(headers: merge_headers(headers))
      .to_return({ status: 200, body: message }, headers: {})
  when 'put', 'post'
    WebMock.stub_request(type.downcase.to_sym, url)
      .with(body: body,
            headers: merge_headers(headers))
      .to_return({ status: 200, body: message }, headers: {})
  else
    fail "Unsupported type: #{type}"
  end
end