spec/shipcloud/request/base_spec.rb in shipcloud-0.6.0 vs spec/shipcloud/request/base_spec.rb in shipcloud-0.7.0
- old
+ new
@@ -1,27 +1,63 @@
require "spec_helper"
describe Shipcloud::Request::Base do
context "#perform" do
it "checks for an api key" do
- Shipcloud.stub(:api_key).and_return(nil)
+ info = Shipcloud::Request::Info.new(:get, "shipments", nil, {})
expect{
- Shipcloud::Request::Base.new(nil).perform
+ Shipcloud::Request::Base.new(info).perform
}.to raise_error Shipcloud::AuthenticationError
end
- it "performs an https request" do
- Shipcloud.stub(:api_key).and_return("some key")
- connection = stub
- validator = stub
- Shipcloud::Request::Connection.stub(:new).and_return(connection)
- Shipcloud::Request::Validator.stub(:new).and_return(validator)
+ it "performs an https request and returns a response hash" do
+ connection = double
+ expect(Shipcloud::Request::Connection).to receive(:new).and_return(connection)
+ expect(connection).to receive(:setup_https)
+ response = double(code: "200", body: { id: 1 }.to_json)
+ expect(connection).to receive(:request).and_return(response)
+ info = Shipcloud::Request::Info.new(:get, "shipments", "api_key", {})
- connection.should_receive(:setup_https)
- connection.should_receive(:request)
- validator.should_receive(:validated_data_for)
+ data = Shipcloud::Request::Base.new(info).perform
- Shipcloud::Request::Base.new(nil).perform
+ expect(data).to eq("id" => 1)
end
+
+ it "performs an https request and raises an Shipcloud::ClientError if the response "\
+ "has a 400 status code" do
+ connection = double
+ expect(Shipcloud::Request::Connection).to receive(:new).and_return(connection)
+ expect(connection).to receive(:setup_https)
+ response = double(code: "400", body: { id: 1 }.to_json)
+ expect(connection).to receive(:request).and_return(response)
+ info = Shipcloud::Request::Info.new(:get, "shipments", "api_key", {})
+
+ expect { Shipcloud::Request::Base.new(info).perform }.to raise_error(Shipcloud::ClientError)
+ end
+
+ it "performs an https request and raises an Shipcloud::ServerError if the response "\
+ "has a 500 status code" do
+ connection = double
+ expect(Shipcloud::Request::Connection).to receive(:new).and_return(connection)
+ expect(connection).to receive(:setup_https)
+ response = double(code: "500", body: { id: 1 }.to_json)
+ expect(connection).to receive(:request).and_return(response)
+ info = Shipcloud::Request::Info.new(:get, "shipments", "api_key", {})
+
+ expect { Shipcloud::Request::Base.new(info).perform }.to raise_error(Shipcloud::ServerError)
+ end
+
+ it "performs an https request and raises an Shipcloud::ShipcloudError if the body of the "\
+ "response is not in JSON" do
+ connection = double
+ expect(Shipcloud::Request::Connection).to receive(:new).and_return(connection)
+ expect(connection).to receive(:setup_https)
+ response = double(code: "200", body: "no json")
+ expect(connection).to receive(:request).and_return(response)
+ info = Shipcloud::Request::Info.new(:get, "shipments", "api_key", {})
+
+ expect { Shipcloud::Request::Base.new(info).perform }.
+ to raise_error(Shipcloud::ShipcloudError)
+ end
end
-end
\ No newline at end of file
+end