Sha256: a75c027f97aec892c26fad8f5e0a6722fd5e146702e064f1a56c4ad7b0965230

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

require 'spec_helper'
include Plaza

describe Request do

  context 'when Thread.current[:x_user_id] is set' do

    before do Thread.current[:x_user_id] = 4242 end

    %i(get post put delete).each do |method|
      it "##{method} should add X-User-Id to headers" do
        stub_request(method, "http://example.com/foobar").
          with(:headers => {
            'Accept'=>'application/json',
            'X-User-Id'=>'4242'}).
          to_return(:status => 200, :body => "", :headers => {})

        Request.new.send(method, '/foobar')
      end
    end

  end

  context 'when Thread.current[:x_user_id] is not set' do

    %i(get post put delete).each do |method|
      it "##{method} should now add X-User-Id to headers" do
        stub_request(method, "http://example.com/foobar").
          with(:headers => {
            'Accept'=>'application/json',
            'User-Agent'=>'Faraday v0.9.0'}).
          to_return(:status => 200, :body => "", :headers => {})

        Request.new.send(method, '/foobar')
      end
    end

  end

  context "when trucker service is down" do
    let(:request){
      exception = Faraday::Adapter::Test::Stubs.new do |stub|
        %i(get put post delete).each do |method|
          stub.send(method, '/failblog') {raise Faraday::Error::ConnectionFailed.new('Connection Failed')}
        end
      end
      request = Request.new do |conn|
        conn.adapter :test, exception
      end
      request
    }
    %i(get put post delete).each do |method|
      describe "#{method}" do
        let(:response){request.send(method, '/failblog')}

        it 'response code should be 503' do
          expect{response}.to raise_error(Plaza::ConnectionError)
        end

        it 'should have error message' do
          expect{response}.to raise_error { |error|
            error.should be_a(Plaza::ConnectionError)
            error.message.should == "Service is not available."
          }
        end
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
plaza-0.0.4 spec/plaza/request_spec.rb