require 'spec_helper' describe NightcrawlerSwift::Command do subject do NightcrawlerSwift::Command.new end before do NightcrawlerSwift.configure allow(NightcrawlerSwift).to receive(:connection).and_return(connection) allow(connection).to receive(:token_id).and_return(token) allow(connection).to receive(:expires_at).and_return(expires_at) allow(RestClient::Resource).to receive(:new).and_return(restclient) end let(:connection) { NightcrawlerSwift::Connection.new } let(:restclient) { double(:restclient, put: response, get: response, head: response, delete: response) } let(:response) { double(:response) } let(:url) { "http://url-com" } let(:token) { "token" } let(:expires_at) { (DateTime.now + 60).to_time } shared_examples "command with configured gateway" do it "creates a new gateway with the url" do expect(NightcrawlerSwift::Gateway).to receive(:new).with(url).and_call_original execute_http end end shared_examples "standard http method" do let :execute_http do subject.send http_verb, url, body: 'ignore_me', headers: { content_type: :json }, params: { b: 2 } end it_behaves_like "command with configured gateway" it "ignores body" do execute_http expect(restclient).to have_received(http_verb).with(kind_of(Hash)) end it "sends headers with token" do execute_http expect(restclient).to have_received(http_verb).with(hash_including({ content_type: :json, "X-Storage-Token" => token })) end it "sends params" do execute_http expect(restclient).to have_received(http_verb).with(hash_including(params: { b: 2 })) end it "returns RestClient response" do expect(execute_http).to eql(response) end end describe "#connection" do context "when disconnected" do before do allow(NightcrawlerSwift.connection).to receive(:connected?).and_return(false) end it "connects and then return the connection" do expect(NightcrawlerSwift.connection).to receive(:connect!) expect(subject.connection).to eql(connection) end end context "when connected" do before do allow(NightcrawlerSwift.connection).to receive(:connected?).and_return(true) end it "returns the connection" do expect(NightcrawlerSwift.connection).to_not receive(:connect!) expect(subject.connection).to eql(connection) end end end describe "#options" do it "returns NightcrawlerSwift.options" do expect(subject.options).to eql(NightcrawlerSwift.options) end end describe "#get" do let(:http_verb) { :get } it_behaves_like "standard http method" end describe "#head" do let(:http_verb) { :head } it_behaves_like "standard http method" end describe "#delete" do let(:http_verb) { :delete } it_behaves_like "standard http method" end describe "#put" do let :execute_http do subject.send :put, url, body: 'content', headers: { a: 1 }, params: { b: 2 } end it_behaves_like "command with configured gateway" it "returns RestClient response" do expect(execute_http).to eql(response) end it "sends body" do execute_http expect(restclient).to have_received(:put).with('content', kind_of(Hash)) end it "sends headers with token" do execute_http expect(restclient).to have_received(:put).with(anything, hash_including({ a: 1, "X-Storage-Token" => token })) end it "sends params" do execute_http expect(restclient).to have_received(:put).with(anything, hash_including(params: { b: 2 })) end end end