diff options
Diffstat (limited to 'spec/lib')
| -rw-r--r-- | spec/lib/tom_tom/matrix_spec.rb | 67 | ||||
| -rw-r--r-- | spec/lib/tom_tom_spec.rb | 12 |
2 files changed, 75 insertions, 4 deletions
diff --git a/spec/lib/tom_tom/matrix_spec.rb b/spec/lib/tom_tom/matrix_spec.rb index 605f1d254..f914cf7ff 100644 --- a/spec/lib/tom_tom/matrix_spec.rb +++ b/spec/lib/tom_tom/matrix_spec.rb @@ -149,6 +149,73 @@ RSpec.describe TomTom::Matrix do end end + describe "#check_for_error_response" do + it "raises a MatrixRemoteError when an 'error' key is present in the response" do + response = double( + 'response', + status: 200, + body: JSON.dump({ + 'formatVersion' => '0.0.1', + 'error' => { + 'description' => 'Output format: csv is unsupported.' + } + }) + ) + + expect { + matrix.check_for_error_response(response) + }.to raise_error( + TomTom::Errors::MatrixRemoteError, + "status: #{response.status}, message: Output format: csv is unsupported." + ) + end + + it "raises a MatrixRemoteError when response status is not 200" do + response = double( + 'response', + status: 403, + body: '<h1>Developer Inactive</h1>' + ) + + expect { + matrix.check_for_error_response(response) + }.to raise_error( + TomTom::Errors::MatrixRemoteError, + "status: #{response.status}, body: <h1>Developer Inactive</h1>" + ) + end + + it "doesn't raise an error when response status is 200" do + response = double( + 'response', + status: 200, + body: JSON.dump({ + 'formatVersion' => '0.0.1', + 'matrix' => [] + }) + ) + + expect { + matrix.check_for_error_response(response) + }.not_to raise_error + end + + it "doesn't raise errors with a normal response" do + response = double( + 'response', + status: 200, + body: JSON.dump({ + 'formatVersion' => '0.0.1', + 'matrix' => [] + }) + ) + + expect { + matrix.check_for_error_response(response) + }.to_not raise_error + end + end + describe "#extract_costs_to_way_costs!" do it "puts distance & time costs in way_costs" do way_costs = [ diff --git a/spec/lib/tom_tom_spec.rb b/spec/lib/tom_tom_spec.rb index 71584e242..4571609c3 100644 --- a/spec/lib/tom_tom_spec.rb +++ b/spec/lib/tom_tom_spec.rb @@ -1,15 +1,19 @@ RSpec.describe TomTom do describe ".enabled?" do it "returns true when API key is set" do - TomTom.instance_variable_set(:@api_key, 'fake key') - + dummy_key = ['a'..'z','A'..'Z',0..9].map(&:to_a).flatten.sample(32).join + allow(TomTom).to receive(:api_key).and_return dummy_key expect(TomTom.enabled?).to be true end it "returns false without an API key" do - TomTom.instance_variable_set(:@api_key, '') + allow(TomTom).to receive(:api_key).and_return '' + expect(TomTom.enabled?).to be_falsy + end - expect(TomTom.enabled?).to be false + it "returns false when API key is malformed" do + allow(TomTom).to receive(:api_key).and_return 'it will not work' + expect(TomTom.enabled?).to be_falsy end end end |
