diff --git a/lib/cypress_on_rails/vcr/insert_eject_middleware.rb b/lib/cypress_on_rails/vcr/insert_eject_middleware.rb index 9d16c74..0c405bb 100644 --- a/lib/cypress_on_rails/vcr/insert_eject_middleware.rb +++ b/lib/cypress_on_rails/vcr/insert_eject_middleware.rb @@ -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) @@ -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 diff --git a/spec/cypress_on_rails/vcr/insert_eject_middleware_spec.rb b/spec/cypress_on_rails/vcr/insert_eject_middleware_spec.rb index ba3f762..284e34e 100644 --- a/spec/cypress_on_rails/vcr/insert_eject_middleware_spec.rb +++ b/spec/cypress_on_rails/vcr/insert_eject_middleware_spec.rb @@ -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 @@ -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