class VCR::Errors::UnhandledHTTPRequestError

Error raised when an HTTP request is made that VCR is unable to handle. @note VCR will raise this to force you to do something about the

HTTP request. The idea is that you want to handle _every_ HTTP
request in your test suite. The error message will give you
suggestions for how to deal with the request.

Constants

ALL_SUGGESTIONS

List of suggestions for how to configure VCR to handle the request.

Attributes

request[R]

The HTTP request.

Public Class Methods

new(request) click to toggle source

Constructs the error.

@param [VCR::Request] request the unhandled request.

Calls superclass method
# File lib/vcr/errors.rb, line 66
def initialize(request)
  @request = request
  super construct_message
end

Private Instance Methods

cassette_description() click to toggle source
# File lib/vcr/errors.rb, line 90
def cassette_description
  if cassette = VCR.current_cassette
    ["VCR is currently using the following cassette:",
     "  - #{cassette.file}",
     "  - :record => #{cassette.record_mode.inspect}",
     "  - :match_requests_on => #{cassette.match_requests_on.inspect}\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
construct_message() click to toggle source
# File lib/vcr/errors.rb, line 77
def construct_message
  ["", "", "=" * 80,
   "An HTTP request has been made that VCR does not know how to handle:",
   "  #{request_description}\n",
   cassette_description,
   formatted_suggestions,
   "=" * 80, "", ""].join("\n")
end
format_bullet_point(lines, index) click to toggle source
# File lib/vcr/errors.rb, line 120
def format_bullet_point(lines, index)
  lines.first.insert(0, "  * ")
  lines.last << " [#{index + 1}]."
  lines.join("\n    ")
end
format_foot_note(url, index) click to toggle source
# File lib/vcr/errors.rb, line 126
def format_foot_note(url, index)
  "[#{index + 1}] #{url % relish_version_slug}"
end
formatted_suggestions() click to toggle source
# File lib/vcr/errors.rb, line 105
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
match_requests_on_suggestion() click to toggle source
# File lib/vcr/errors.rb, line 228
def match_requests_on_suggestion
  num_remaining_interactions = VCR.current_cassette.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
no_cassette_suggestions() click to toggle source
# File lib/vcr/errors.rb, line 214
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
record_mode_suggestion() click to toggle source
# File lib/vcr/errors.rb, line 220
def record_mode_suggestion
  case VCR.current_cassette.record_mode
  when :none then [:deal_with_none]
  when :once then [:delete_cassette_for_once]
  else []
  end
end
relish_version_slug() click to toggle source
# File lib/vcr/errors.rb, line 73
def relish_version_slug
  @relish_version_slug ||= VCR.version.gsub(/\W/, '-')
end
request_description() click to toggle source
# File lib/vcr/errors.rb, line 86
def request_description
  "#{request.method.to_s.upcase} #{request.uri}"
end
suggestion_for(key) click to toggle source
# File lib/vcr/errors.rb, line 196
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
suggestions() click to toggle source
# File lib/vcr/errors.rb, line 203
def suggestions
  return no_cassette_suggestions unless cassette = VCR.current_cassette

  [:try_debug_logger, :use_new_episodes, :ignore_request].tap do |suggestions|
    suggestions.push(*record_mode_suggestion)
    suggestions << :allow_playback_repeats if cassette.http_interactions.has_used_interaction_matching?(request)
    suggestions.map! { |k| suggestion_for(k) }
    suggestions.push(*match_requests_on_suggestion)
  end
end