Module: RSpec::OpenAPI::ExchangeRecorder
- Defined in:
- lib/rspec/openapi/exchange_recorder.rb
Overview
Records every request/response exchange issued in an example so that
the exchange matching request_pattern can be looked up afterwards.
Defined Under Namespace
Modules: VerbTracking
Constant Summary collapse
- THREAD_KEY =
:rspec_openapi_exchanges- VERBS =
[:get, :post, :put, :patch, :delete, :head, :options].freeze
Class Method Summary collapse
- .capture_from_context(context) ⇒ Object
-
.fetch(pattern) ⇒ Array(ActionDispatch::Request, ActionDispatch::TestResponse)
Returns the exchange matching a
"METHOD /path/template"pattern. - .pattern_for(example) ⇒ Object
- .reset!(example) ⇒ Object
Class Method Details
.capture_from_context(context) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/rspec/openapi/exchange_recorder.rb', line 35 def capture_from_context(context) exchanges = Thread.current[THREAD_KEY] return unless exchanges exchanges << build_exchange(context) end |
.fetch(pattern) ⇒ Array(ActionDispatch::Request, ActionDispatch::TestResponse)
Returns the exchange matching a "METHOD /path/template" pattern.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rspec/openapi/exchange_recorder.rb', line 46 def fetch(pattern) method, path_template = parse_pattern(pattern) path_matcher = path_template_to_regexp(path_template) found = recorded.reverse_each.find do |request, _response| request.request_method.to_s.upcase == method && request.path.match?(path_matcher) end return found if found raise ArgumentError, (pattern) end |
.pattern_for(example) ⇒ Object
31 32 33 |
# File 'lib/rspec/openapi/exchange_recorder.rb', line 31 def pattern_for(example) SharedExtractor.(example.)[:request_pattern] end |
.reset!(example) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/rspec/openapi/exchange_recorder.rb', line 22 def reset!(example) if pattern_for(example) example.example_group.prepend(VerbTracking) Thread.current[THREAD_KEY] = [] else Thread.current[THREAD_KEY] = nil end end |