Module: Philiprehberger::HttpMock

Defined in:
lib/philiprehberger/http_mock.rb,
lib/philiprehberger/http_mock/request.rb,
lib/philiprehberger/http_mock/version.rb,
lib/philiprehberger/http_mock/registry.rb,
lib/philiprehberger/http_mock/response.rb,
lib/philiprehberger/http_mock/stub_definition.rb

Defined Under Namespace

Classes: Error, Registry, Request, Response, StubDefinition, TimeoutError, UnmatchedRequestError, UnmatchedStubError

Constant Summary collapse

VERSION =
'0.3.1'

Class Method Summary collapse

Class Method Details

.last_requestRequest?

Get the most recently recorded request

Returns:

  • (Request, nil)

    the last recorded request or nil



63
64
65
# File 'lib/philiprehberger/http_mock.rb', line 63

def last_request
  registry.requests.last
end

.request(method, url, body: nil, headers: {}) ⇒ Response

Simulate an HTTP request against registered stubs

Parameters:

  • method (Symbol)

    the HTTP method

  • url (String)

    the request URL

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

    the request body

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

    the request headers

Returns:

  • (Response)

    the matched stub response

Raises:



43
44
45
46
47
48
49
50
51
# File 'lib/philiprehberger/http_mock.rb', line 43

def request(method, url, body: nil, headers: {})
  req = Request.new(method: method, url: url, body: body, headers: headers)
  registry.record(req)

  matched = registry.find_stub(req)
  raise UnmatchedRequestError, "No stub matched #{method.upcase} #{url}" unless matched

  matched.response_for(req)
end

.requestsArray<Request>

Get all recorded requests

Returns:

  • (Array<Request>)

    the recorded requests



56
57
58
# File 'lib/philiprehberger/http_mock.rb', line 56

def requests
  registry.requests.dup
end

.reset!void

This method returns an undefined value.

Clear all stubs and recorded requests



97
98
99
# File 'lib/philiprehberger/http_mock.rb', line 97

def reset!
  registry.reset!
end

.scope { ... } ⇒ Object

Execute a block with isolated stubs that are automatically cleaned up

Yields:

  • the block to execute with isolated stubs

Returns:

  • (Object)

    the block return value



105
106
107
108
109
110
111
# File 'lib/philiprehberger/http_mock.rb', line 105

def scope(&block)
  previous = @registry
  @registry = Registry.new
  block.call
ensure
  @registry = previous
end

.stub(method, url) ⇒ StubDefinition

Stub an HTTP request

Parameters:

  • method (Symbol)

    the HTTP method (:get, :post, :put, :patch, :delete, :head, :options)

  • url (String, Regexp)

    the URL or pattern to match

Returns:



30
31
32
33
# File 'lib/philiprehberger/http_mock.rb', line 30

def stub(method, url)
  stub_def = StubDefinition.new(method, url)
  registry.register(stub_def)
end

.stub_delete(url) ⇒ Object

Shorthand for stub(:delete, url)



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

def stub_delete(url) = stub(:delete, url)

.stub_get(url) ⇒ Object

Shorthand for stub(:get, url)



68
# File 'lib/philiprehberger/http_mock.rb', line 68

def stub_get(url) = stub(:get, url)

.stub_patch(url) ⇒ Object

Shorthand for stub(:patch, url)



77
# File 'lib/philiprehberger/http_mock.rb', line 77

def stub_patch(url) = stub(:patch, url)

.stub_post(url) ⇒ Object

Shorthand for stub(:post, url)



71
# File 'lib/philiprehberger/http_mock.rb', line 71

def stub_post(url) = stub(:post, url)

.stub_put(url) ⇒ Object

Shorthand for stub(:put, url)



74
# File 'lib/philiprehberger/http_mock.rb', line 74

def stub_put(url) = stub(:put, url)

.verify!void

This method returns an undefined value.

Verify that all registered stubs were called at least once

Raises:



86
87
88
89
90
91
92
# File 'lib/philiprehberger/http_mock.rb', line 86

def verify!
  uncalled = registry.stubs.reject(&:called?)
  return if uncalled.empty?

  descriptions = uncalled.map { |s| "#{s.method.upcase} #{s.url}" }
  raise UnmatchedStubError, "The following stubs were never called: #{descriptions.join(', ')}"
end