Skip to content

Commit

Permalink
throws error when valid response is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
parroty committed Oct 6, 2013
1 parent 5ac67b7 commit 62ab033
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/exvcr/exception.ex
@@ -1 +1,2 @@
defexception ExVCRError, message: "error"
defexception ExVCR.InvalidRequestError, message: "error"
defexception ExVCR.FileNotFoundError, message: "error"
2 changes: 1 addition & 1 deletion lib/exvcr/handler.ex
Expand Up @@ -16,7 +16,7 @@ defmodule ExVCR.Handler do
defp verify_response(response, recorder, target_url) do
options = Options.get(recorder.options)
if response == nil and options[:custom] == true do
raise ExVCRError.new(message: "response for \"#{target_url}\" was not found in the custom cassette")
raise ExVCR.InvalidRequestError.new(message: "response for \"#{target_url}\" was not found in the custom cassette")
else
response
end
Expand Down
5 changes: 4 additions & 1 deletion lib/exvcr/json.ex
Expand Up @@ -17,9 +17,12 @@ defmodule ExVCR.JSON do

def load(fixture, options) do
file_name = get_file_path(fixture, options)
custom_option = options[:custom]
case File.exists?(file_name) do
true -> File.read!(file_name) |> JSEX.decode! |> Enum.map(&from_string/1)
_ -> []
false when custom_option ->
raise ExVCR.FileNotFoundError.new(message: "cassette file \"#{file_name}\" not found")
_ -> []
end
end

Expand Down
22 changes: 15 additions & 7 deletions test/exvcr_test.exs
Expand Up @@ -59,27 +59,35 @@ defmodule ExVCR.MockTest do
end
end

test "custom response with valid response file" do
test "custom with valid response" do
use_cassette "response_mocking", custom: true do
assert HTTPotion.get("http://example.com", []).body =~ %r/Custom Response/
end
end

test "custom response without valid response file" do
assert_raise ExVCRError, fn ->
test "custom response with regexp url" do
use_cassette "response_mocking_regex", custom: true do
HTTPotion.get("http://example.com/something/abc", []).body =~ %r/Custom Response/
end
end

test "custom without valid response throws error" do
assert_raise ExVCR.InvalidRequestError, fn ->
use_cassette "response_mocking", custom: true do
HTTPotion.get("http://example.com/invalid", [])
end
end
end

test "custom response with regexp url" do
use_cassette "response_mocking_regex", custom: true do
HTTPotion.get("http://example.com/something/abc", []).body =~ %r/Custom Response/
test "custom without valid response file throws error" do
assert_raise ExVCR.FileNotFoundError, fn ->
use_cassette "invalid_file_response", custom: true do
HTTPotion.get("http://example.com", [])
end
end
:meck.unload(:ibrowse) # temporary fix
end


defp assert_response(response, function // nil) do
assert response.success?(:extra)
assert response.headers[:Connection] == "keep-alive"
Expand Down

0 comments on commit 62ab033

Please sign in to comment.