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.4.0'
Class Method Summary collapse
-
.last_request ⇒ Request?
Get the most recently recorded request.
-
.request(method, url, body: nil, headers: {}) ⇒ Response
Simulate an HTTP request against registered stubs.
-
.requests ⇒ Array<Request>
Get all recorded requests.
-
.requests_for(method, url) ⇒ Array<Request>
Get all recorded requests matching a method and URL.
-
.reset! ⇒ void
Clear all stubs and recorded requests.
-
.scope { ... } ⇒ Object
Execute a block with isolated stubs that are automatically cleaned up.
-
.stub(method, url) ⇒ StubDefinition
Stub an HTTP request.
-
.stub_delete(url) ⇒ Object
Shorthand for stub(:delete, url).
-
.stub_get(url) ⇒ Object
Shorthand for stub(:get, url).
-
.stub_patch(url) ⇒ Object
Shorthand for stub(:patch, url).
-
.stub_post(url) ⇒ Object
Shorthand for stub(:post, url).
-
.stub_put(url) ⇒ Object
Shorthand for stub(:put, url).
-
.verify! ⇒ void
Verify that all registered stubs were called at least once.
Class Method Details
.last_request ⇒ Request?
Get the most recently recorded request
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
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 |
.requests ⇒ Array<Request>
Get all recorded requests
56 57 58 |
# File 'lib/philiprehberger/http_mock.rb', line 56 def requests registry.requests.dup end |
.requests_for(method, url) ⇒ Array<Request>
Get all recorded requests matching a method and URL
The method comparison normalizes case (‘:GET` matches `:get`). The URL comparison is exact against the recorded `request.url` value, so it mirrors how the stub registry stores incoming requests. Returns an empty Array when no requests match.
77 78 79 80 81 82 |
# File 'lib/philiprehberger/http_mock.rb', line 77 def requests_for(method, url) target_method = method.to_s.downcase.to_sym registry.requests.select do |req| req.method.to_s.downcase.to_sym == target_method && req.url == url end end |
.reset! ⇒ void
This method returns an undefined value.
Clear all stubs and recorded requests
114 115 116 |
# File 'lib/philiprehberger/http_mock.rb', line 114 def reset! registry.reset! end |
.scope { ... } ⇒ Object
Execute a block with isolated stubs that are automatically cleaned up
122 123 124 125 126 127 128 |
# File 'lib/philiprehberger/http_mock.rb', line 122 def scope(&block) previous = @registry @registry = Registry.new block.call ensure @registry = previous end |
.stub(method, url) ⇒ StubDefinition
Stub an HTTP request
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)
97 |
# File 'lib/philiprehberger/http_mock.rb', line 97 def stub_delete(url) = stub(:delete, url) |
.stub_get(url) ⇒ Object
Shorthand for stub(:get, url)
85 |
# File 'lib/philiprehberger/http_mock.rb', line 85 def stub_get(url) = stub(:get, url) |
.stub_patch(url) ⇒ Object
Shorthand for stub(:patch, url)
94 |
# File 'lib/philiprehberger/http_mock.rb', line 94 def stub_patch(url) = stub(:patch, url) |
.stub_post(url) ⇒ Object
Shorthand for stub(:post, url)
88 |
# File 'lib/philiprehberger/http_mock.rb', line 88 def stub_post(url) = stub(:post, url) |
.stub_put(url) ⇒ Object
Shorthand for stub(:put, url)
91 |
# File 'lib/philiprehberger/http_mock.rb', line 91 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
103 104 105 106 107 108 109 |
# File 'lib/philiprehberger/http_mock.rb', line 103 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 |