Class: Philiprehberger::HttpMock::StubDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/http_mock/stub_definition.rb

Overview

Defines a request stub with matching criteria and response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, url) ⇒ StubDefinition

Returns a new instance of StubDefinition.

Parameters:

  • method (Symbol)

    the HTTP method to match

  • url (String, Regexp)

    the URL or pattern to match



21
22
23
24
25
26
27
28
29
30
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 21

def initialize(method, url)
  @method = method
  @url = url
  @constraints = {}
  @response = Response.new
  @response_sequence = nil
  @response_callback = nil
  @raise_exception = nil
  @call_count = 0
end

Instance Attribute Details

#call_countInteger (readonly)

Returns number of times this stub has been matched.

Returns:

  • (Integer)

    number of times this stub has been matched



17
18
19
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 17

def call_count
  @call_count
end

#constraintsHash (readonly)

Returns additional matching criteria.

Returns:

  • (Hash)

    additional matching criteria



14
15
16
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 14

def constraints
  @constraints
end

#methodSymbol (readonly)

Returns the HTTP method.

Returns:

  • (Symbol)

    the HTTP method



8
9
10
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 8

def method
  @method
end

#urlString, Regexp (readonly)

Returns the URL pattern.

Returns:

  • (String, Regexp)

    the URL pattern



11
12
13
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 11

def url
  @url
end

Instance Method Details

#called?Boolean

Whether this stub has been called at least once

Returns:

  • (Boolean)


126
127
128
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 126

def called?
  @call_count.positive?
end

#matches?(request) ⇒ Boolean

Check if a request matches this stub

Parameters:

  • request (Request)

    the request to check

Returns:

  • (Boolean)

    true if the request matches



134
135
136
137
138
139
140
141
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 134

def matches?(request)
  return false unless request.method == @method
  return false unless url_matches?(request.url)
  return false unless body_matches?(request.body)
  return false unless headers_match?(request.headers)

  true
end

#response_for(request) ⇒ Response

Return the response for this stub, advancing sequence if applicable

Parameters:

  • request (Request)

    the matched request

Returns:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 103

def response_for(request)
  @call_count += 1

  if @raise_exception
    raise @raise_exception.is_a?(Class) ? @raise_exception.new : @raise_exception
  elsif @response_callback
    result = @response_callback.call(request)
    Response.new(
      status: result.fetch(:status, 200),
      body: result.fetch(:body, ''),
      headers: result.fetch(:headers, {})
    )
  elsif @response_sequence
    index = [@call_count - 1, @response_sequence.length - 1].min
    @response_sequence[index]
  else
    @response
  end
end

#to_raise(exception) ⇒ self

Configure the stub to raise an exception instead of returning a response

Parameters:

  • exception (Exception, Class)

    an exception instance or class to raise

Returns:

  • (self)


67
68
69
70
71
72
73
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 67

def to_raise(exception)
  @raise_exception = exception
  @response = nil
  @response_sequence = nil
  @response_callback = nil
  self
end

#to_return(status: 200, body: '', headers: {}) {|Request| ... } ⇒ self

Set the response to return when this stub matches

Parameters:

  • status (Integer) (defaults to: 200)

    the HTTP status code

  • body (String) (defaults to: '')

    the response body

  • headers (Hash) (defaults to: {})

    the response headers

Yields:

  • (Request)

    optional block for dynamic response generation

Returns:

  • (self)


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 50

def to_return(status: 200, body: '', headers: {}, &block)
  if block
    @response_callback = block
    @response = nil
    @response_sequence = nil
  else
    @response = Response.new(status: status, body: body, headers: headers)
    @response_callback = nil
    @response_sequence = nil
  end
  self
end

#to_return_in_sequence(responses) ⇒ self

Set a sequence of responses to cycle through

Parameters:

  • responses (Array<Hash>)

    array of response hashes with :status, :body, :headers keys

Returns:

  • (self)


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 86

def to_return_in_sequence(responses)
  @response_sequence = responses.map do |resp|
    Response.new(
      status: resp.fetch(:status, 200),
      body: resp.fetch(:body, ''),
      headers: resp.fetch(:headers, {})
    )
  end
  @response = nil
  @response_callback = nil
  self
end

#to_timeoutself

Configure the stub to raise a TimeoutError

Returns:

  • (self)


78
79
80
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 78

def to_timeout
  to_raise(TimeoutError.new("Request timed out: #{@method.upcase} #{@url}"))
end

#with(body: nil, headers: nil) ⇒ self

Set additional matching constraints

Parameters:

  • body (String, Hash, nil) (defaults to: nil)

    the expected request body

  • headers (Hash, nil) (defaults to: nil)

    the expected request headers

Returns:

  • (self)


37
38
39
40
41
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 37

def with(body: nil, headers: nil)
  @constraints[:body] = body if body
  @constraints[:headers] = headers if headers
  self
end