spec/acceptance/rest/base_spec.rb in ably-0.1.5 vs spec/acceptance/rest/base_spec.rb in ably-0.1.6

- old
+ new

@@ -12,78 +12,95 @@ let(:client_options) { {} } let(:client) do Ably::Rest::Client.new(client_options.merge(api_key: 'appid.keyuid:keysecret')) end - skip '#protocol should default to :msgpack' - context 'transport' do let(:now) { Time.now - 1000 } let(:body_value) { [as_since_epoch(now)] } before do stub_request(:get, "#{client.endpoint}/time"). with(:headers => { 'Accept' => mime }). to_return(:status => 200, :body => request_body, :headers => { 'Content-Type' => mime }) end - context 'when protocol is set as :json' do - let(:client_options) { { protocol: :json } } - let(:mime) { 'application/json' } - let(:request_body) { body_value.to_json } - - it 'uses JSON', webmock: true do - expect(client.protocol).to eql(:json) - expect(client.time).to be_within(1).of(now) - end - - skip 'uses JSON against Ably service for Auth' - skip 'uses JSON against Ably service for Messages' - end - - context 'when protocol is set as :msgpack' do - let(:client_options) { { protocol: :msgpack } } + context 'when protocol is not defined it defaults to :msgpack' do + let(:client_options) { { } } let(:mime) { 'application/x-msgpack' } let(:request_body) { body_value.to_msgpack } it 'uses MsgPack', webmock: true do expect(client.protocol).to eql(:msgpack) expect(client.time).to be_within(1).of(now) end + end - skip 'uses MsgPack against Ably service for Auth' - skip 'uses MsgPack against Ably service for Messages' + options = [ + { protocol: :json }, + { use_binary_protocol: false } + ].each do |client_option| + + context "when option #{client_option} is used" do + let(:client_options) { client_option } + let(:mime) { 'application/json' } + let(:request_body) { body_value.to_json } + + it 'uses JSON', webmock: true do + expect(client.protocol).to eql(:json) + expect(client.time).to be_within(1).of(now) + end + end end + + options = [ + { protocol: :json }, + { use_binary_protocol: false } + ].each do |client_option| + + context "when option #{client_option} is used" do + let(:client_options) { { protocol: :msgpack } } + let(:mime) { 'application/x-msgpack' } + let(:request_body) { body_value.to_msgpack } + + it 'uses MsgPack', webmock: true do + expect(client.protocol).to eql(:msgpack) + expect(client.time).to be_within(1).of(now) + end + end + end end end describe "invalid requests in middleware" do it "should raise an InvalidRequest exception with a valid message" do invalid_client = Ably::Rest::Client.new(api_key: 'appid.keyuid:keysecret', environment: environment) expect { invalid_client.channel('test').publish('foo', 'choo') }.to raise_error do |error| - expect(error).to be_a(Ably::Exceptions::InvalidRequest) + expect(error).to be_a(Ably::Exceptions::InvalidToken) expect(error.message).to match(/invalid credentials/) expect(error.code).to eql(40100) expect(error.status).to eql(401) end end describe "server error with JSON response", webmock: true do let(:error_response) { '{ "error": { "statusCode": 500, "code": 50000, "message": "Internal error" } }' } before do - stub_request(:get, "#{client.endpoint}/time").to_return(:status => 500, :body => error_response, :headers => { 'Content-Type' => 'application/json' }) + stub_request(:get, "#{client.endpoint}/time"). + to_return(:status => 500, :body => error_response, :headers => { 'Content-Type' => 'application/json' }) end it "should raise a ServerError exception" do expect { client.time }.to raise_error(Ably::Exceptions::ServerError, /Internal error/) end end describe "server error", webmock: true do before do - stub_request(:get, "#{client.endpoint}/time").to_return(:status => 500) + stub_request(:get, "#{client.endpoint}/time"). + to_return(:status => 500, :headers => { 'Content-Type' => 'application/json' }) end it "should raise a ServerError exception" do expect { client.time }.to raise_error(Ably::Exceptions::ServerError, /Unknown/) end @@ -108,13 +125,13 @@ end stub_request(:post, "#{client.endpoint}/channels/#{channel}/publish").to_return do @publish_attempts += 1 if [1, 3].include?(@publish_attempts) - { status: 201, :body => '[]' } + { status: 201, :body => '[]', :headers => { 'Content-Type' => 'application/json' } } else - raise Ably::Exceptions::InvalidRequest.new('Authentication failure', status: 401, code: 40140) + raise Ably::Exceptions::InvalidRequest.new('Authentication failure', 401, 40140) end end end context 'when auth#token_renewable?' do @@ -183,10 +200,10 @@ end let(:token_request_1) { client.auth.create_token_request(token_request_options.merge(client_id: SecureRandom.hex)) } let(:token_request_2) { client.auth.create_token_request(token_request_options.merge(client_id: SecureRandom.hex)) } context 'when expired' do - let(:token_request_options) { { key_id: key_id, key_secret: key_secret, ttl: Ably::Token::TOKEN_EXPIRY_BUFFER } } + let(:token_request_options) { { key_id: key_id, key_secret: key_secret, ttl: Ably::Models::Token::TOKEN_EXPIRY_BUFFER } } it 'creates a new token automatically when the old token expires' do expect { client.channel('channel_name').publish('event', 'message') }.to change { client.auth.current_token } expect(client.auth.current_token.client_id).to eql(token_request_1[:client_id])