Module: RSpec::OpenAPI::ExchangeRecorder

Defined in:
lib/rspec/openapi/exchange_recorder.rb

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

Class Method Details

.capture_from_context(context) ⇒ Object



32
33
34
35
36
37
# File 'lib/rspec/openapi/exchange_recorder.rb', line 32

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.

Parameters:

  • pattern (String)

    e.g. "DELETE /resources/id"

Returns:

  • (Array(ActionDispatch::Request, ActionDispatch::TestResponse))

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rspec/openapi/exchange_recorder.rb', line 43

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, no_match_message(pattern)
end

.pattern_for(example) ⇒ Object



28
29
30
# File 'lib/rspec/openapi/exchange_recorder.rb', line 28

def pattern_for(example)
  SharedExtractor.(example.)[:request_pattern]
end

.reset!(example) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/rspec/openapi/exchange_recorder.rb', line 19

def reset!(example)
  if pattern_for(example)
    example.example_group.prepend(VerbTracking)
    Thread.current[THREAD_KEY] = []
  else
    Thread.current[THREAD_KEY] = nil
  end
end