class VCR::Errors::UnhandledHTTPRequestError
suggestions for how to deal with the request.
request in your test suite. The error message will give you
HTTP request. The idea is that you want to handle every HTTP
@note VCR will raise this to force you to do something about the
Error raised when an HTTP request is made that VCR is unable to handle.
def cassettes_description
def cassettes_description if current_cassettes.size > 0 [cassettes_list << "\n", "Under the current configuration VCR can not find a suitable HTTP interaction", "to replay and is prevented from recording new requests. There are a few ways", "you can deal with this:\n"].join("\n") else ["There is currently no cassette in use. There are a few ways", "you can configure VCR to handle this request:\n"].join("\n") end end
def cassettes_list
def cassettes_list lines = [] lines << if current_cassettes.size == 1 "VCR is currently using the following cassette:" else "VCR are currently using the following cassettes:" end lines = current_cassettes.inject(lines) do |memo, cassette| memo.concat([ " - #{cassette.file}", " - :record => #{cassette.record_mode.inspect}", " - :match_requests_on => #{cassette.match_requests_on.inspect}" ]) end lines.join("\n") end
def construct_message
def construct_message ["", "", "=" * 80, "An HTTP request has been made that VCR does not know how to handle:", "#{request_description}\n", cassettes_description, formatted_suggestions, "=" * 80, "", ""].join("\n") end
def current_cassettes
def current_cassettes @cassettes ||= VCR.cassettes.to_a.reverse end
def current_matchers
def current_matchers if current_cassettes.size > 0 current_cassettes.inject([]) do |memo, cassette| memo | cassette.match_requests_on end else VCR.configuration.default_cassette_options[:match_requests_on] end end
def documentation_version_slug
def documentation_version_slug @documentation_version_slug ||= VCR.version.gsub(/\W/, '-') end
def format_bullet_point(lines, index)
def format_bullet_point(lines, index) lines.first.insert(0, " * ") lines.last << " [#{index + 1}]." lines.join("\n ") end
def format_foot_note(url, index)
def format_foot_note(url, index) "[#{index + 1}] #{url % documentation_version_slug}" end
def formatted_headers
def formatted_headers request.headers.flat_map do |header, values| values.map do |val| " #{header}: #{val.inspect}" end end.join("\n") end
def formatted_suggestions
def formatted_suggestions formatted_points, formatted_foot_notes = [], [] suggestions.each_with_index do |suggestion, index| bullet_point, foot_note = suggestion.first, suggestion.last formatted_points << format_bullet_point(bullet_point, index) formatted_foot_notes << format_foot_note(foot_note, index) end [ formatted_points.join("\n"), formatted_foot_notes.join("\n") ].join("\n\n") end
def has_used_interaction_matching?
def has_used_interaction_matching? current_cassettes.any?{|c| c.http_interactions.has_used_interaction_matching?(request) } end
def initialize(request)
-
request
(VCR::Request
) -- the unhandled request.
def initialize(request) @request = request super construct_message end
def match_request_on_body?
def match_request_on_body? current_matchers.include?(:body) end
def match_request_on_headers?
def match_request_on_headers? current_matchers.include?(:headers) end
def match_requests_on_suggestion
def match_requests_on_suggestion num_remaining_interactions = current_cassettes.inject(0) { |sum, c| sum + c.http_interactions.remaining_unused_interaction_count } return [] if num_remaining_interactions.zero? interaction_description = if num_remaining_interactions == 1 "1 HTTP interaction that has" else "#{num_remaining_interactions} HTTP interactions that have" end description_lines, link = suggestion_for(:match_requests_on) description_lines[0] = description_lines[0] % interaction_description [[description_lines, link]] end
def no_cassette_suggestions
def no_cassette_suggestions [:try_debug_logger, :use_a_cassette, :allow_http_connections_when_no_cassette, :ignore_request].map do |key| suggestion_for(key) end end
def none_suggestion
def none_suggestion if current_cassettes.any? {|c| !File.exist?(c.file) } [:none_without_file] else [:deal_with_none] end end
def record_mode_suggestion
def record_mode_suggestion record_modes = current_cassettes.map(&:record_mode) if record_modes.all?{|r| r == :none } none_suggestion elsif record_modes.all?{|r| r == :once } [:delete_cassette_for_once] else [] end end
def request_description
def request_description lines = [] lines << " #{request.method.to_s.upcase} #{request.uri}" if match_request_on_headers? lines << " Headers:\n#{formatted_headers}" end if match_request_on_body? lines << " Body: #{request.body}" end lines.join("\n") end
def suggestion_for(key)
def suggestion_for(key) bullet_point_lines, url = ALL_SUGGESTIONS[key] bullet_point_lines = bullet_point_lines.map(&:dup) url = url.dup [bullet_point_lines, url] end
def suggestions
def suggestions return no_cassette_suggestions if current_cassettes.size == 0 [:try_debug_logger, :use_new_episodes, :ignore_request].tap do |suggestions| suggestions.push(*record_mode_suggestion) suggestions << :allow_playback_repeats if has_used_interaction_matching? suggestions.map! { |k| suggestion_for(k) } suggestions.push(*match_requests_on_suggestion) end end