Class: Typhoeus::HydraMock

Inherits:
Object
  • Object
show all
Defined in:
lib/typhoeus/hydra_mock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, method, options = {}) ⇒ HydraMock

Returns a new instance of HydraMock.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/typhoeus/hydra_mock.rb', line 5

def initialize(url, method, options = {})
  @url      = url
  @uri      = URI.parse(url) if url.kind_of?(String)
  @method   = method
  @requests = []
  @options = options
  if @options[:headers]
    @options[:headers] = Typhoeus::NormalizedHeaderHash.new(@options[:headers])
  end

  @current_response_index = 0
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



3
4
5
# File 'lib/typhoeus/hydra_mock.rb', line 3

def method
  @method
end

#requestsObject (readonly)

Returns the value of attribute requests.



3
4
5
# File 'lib/typhoeus/hydra_mock.rb', line 3

def requests
  @requests
end

#uriObject (readonly)

Returns the value of attribute uri.



3
4
5
# File 'lib/typhoeus/hydra_mock.rb', line 3

def uri
  @uri
end

#urlObject (readonly)

Returns the value of attribute url.



3
4
5
# File 'lib/typhoeus/hydra_mock.rb', line 3

def url
  @url
end

Instance Method Details

#add_request(request) ⇒ Object



34
35
36
# File 'lib/typhoeus/hydra_mock.rb', line 34

def add_request(request)
  @requests << request
end

#and_return(val) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/typhoeus/hydra_mock.rb', line 38

def and_return(val)
  if val.respond_to?(:each)
    @responses = val
  else
    @responses = [val]
  end

  # make sure to mark them as a mock.
  @responses.each { |r| r.mock = true }

  val
end

#bodyObject



18
19
20
# File 'lib/typhoeus/hydra_mock.rb', line 18

def body
  @options[:body]
end

#body?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/typhoeus/hydra_mock.rb', line 22

def body?
  @options.has_key?(:body)
end

#headersObject



26
27
28
# File 'lib/typhoeus/hydra_mock.rb', line 26

def headers
  @options[:headers]
end

#headers?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/typhoeus/hydra_mock.rb', line 30

def headers?
  @options.has_key?(:headers)
end

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/typhoeus/hydra_mock.rb', line 61

def matches?(request)
  if !method_matches?(request) or !url_matches?(request)
    return false
  end

  if body?
    return false unless body_matches?(request)
  end

  if headers?
    return false unless headers_match?(request)
  end

  true
end

#responseObject



51
52
53
54
55
56
57
58
59
# File 'lib/typhoeus/hydra_mock.rb', line 51

def response
  if @current_response_index == (@responses.length - 1)
    @responses.last
  else
    value = @responses[@current_response_index]
    @current_response_index += 1
    value
  end
end