Sha256: d59e0176cec131ffd440a1c77652b8efd80e1a748a384c14b380251319a1e29d

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 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
    after do Thread.current[:x_user_id] = nil 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
    %i(get put post delete).each do |method|
      describe "#{method}" do
        before do
          stub_request(method, "http://example.com/failblog").
            to_raise(Faraday::Error::ConnectionFailed.new('Connection Failed'))
        end

        let(:response){Request.new.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.1.0 spec/plaza/request_spec.rb