Skip to content

Commit

Permalink
Update rescue logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MUTOgen committed Oct 18, 2024
1 parent 218e3a4 commit bd1dc92
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/cypress_on_rails/vcr/insert_eject_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def handle_insert(req)
vcr.insert_cassette(cassette_name, options)
[201, { 'Content-Type' => 'application/json' }, [{ 'message': 'OK' }.to_json]]
rescue LoadError, ArgumentError => e
[501, { 'Content-Type' => 'application/json' }, [{ 'message': e.message }.to_json]]
[500, { 'Content-Type' => 'application/json' }, [{ 'message': e.message }.to_json]]
end

def parse_request_body(req)
Expand All @@ -57,7 +57,7 @@ def handle_eject
do_first_call
[201, { 'Content-Type' => 'application/json' }, [{ 'message': 'OK' }.to_json]]
rescue LoadError, ArgumentError => e
[501, { 'Content-Type' => 'application/json' }, [{ 'message': e.message }.to_json]]
[500, { 'Content-Type' => 'application/json' }, [{ 'message': e.message }.to_json]]
end

def do_first_call
Expand Down
46 changes: 46 additions & 0 deletions spec/cypress_on_rails/vcr/insert_eject_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ def rack_input(json_value)
expect(vcr).to have_received(:insert_cassette).with('cas1', persist_with: :file_system)
end
end

context 'when an error occurs' do
it 'returns a 500 error with the error message' do
env['rack.input'] = rack_input(['cas1'])
allow(vcr).to receive(:insert_cassette).and_raise(ArgumentError.new('Invalid cassette name'))

expect(response).to eq([
500,
{ 'Content-Type' => 'application/json' },
['{"message":"Invalid cassette name"}']
])
end

it 'returns a 500 error when LoadError occurs' do
env['rack.input'] = rack_input(['cas1'])
allow(vcr).to receive(:insert_cassette).and_raise(LoadError.new('Cannot load VCR'))

expect(response).to eq([
500,
{ 'Content-Type' => 'application/json' },
['{"message":"Cannot load VCR"}']
])
end
end
end

describe '/__e2e__/vcr/eject' do
Expand All @@ -93,6 +117,28 @@ def rack_input(json_value)
expect(vcr).to have_received(:eject_cassette)
end
end

context 'when an error occurs' do
it 'returns a 500 error with the error message' do
allow(vcr).to receive(:eject_cassette).and_raise(ArgumentError.new('No cassette to eject'))

expect(response).to eq([
500,
{ 'Content-Type' => 'application/json' },
['{"message":"No cassette to eject"}']
])
end

it 'returns a 500 error when LoadError occurs' do
allow(vcr).to receive(:eject_cassette).and_raise(LoadError.new('Cannot load VCR'))

expect(response).to eq([
500,
{ 'Content-Type' => 'application/json' },
['{"message":"Cannot load VCR"}']
])
end
end
end

describe '"Other paths"' do
Expand Down

0 comments on commit bd1dc92

Please sign in to comment.