File: connection_spec.rb

package info (click to toggle)
ruby-fog-core 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 676 kB
  • sloc: ruby: 4,719; makefile: 5
file content (105 lines) | stat: -rw-r--r-- 3,219 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require "spec_helper"

describe Fog::Core::Connection do
  it "raises ArgumentError when no arguments given" do
    assert_raises(ArgumentError) do
      Fog::Core::Connection.new
    end
  end

  [:request, :reset].each do |method|
    it "responds to #{method}" do
      connection = Fog::Core::Connection.new("http://example.com")
      assert connection.respond_to?(method)
    end
  end

  it "adds custom user-agents to Fog requests" do
    Fog::VERSION = 'Version'
    Fog::Core::Connection.add_user_agent("my-app/1.2")

    connection = Fog::Core::Connection.new("http://example.com")
    assert_equal "my-app/1.2 fog/Version fog-core/#{Fog::Core::VERSION}",
                 connection.instance_variable_get(:@excon).data[:headers]["User-Agent"]

    Fog.send(:remove_const, :VERSION)
  end

  it "doesn't error when persistence is enabled" do
    Fog::Core::Connection.new("http://example.com", true)
  end

  it "doesn't error when persistence is enabled and debug_response is disabled" do
    options = {
      :debug_response => false
    }
    Fog::Core::Connection.new("http://example.com", true, options)
  end

  describe ":path_prefix" do
    it "does not emit a warning when provided this argument in the initializer" do
      $stderr = StringIO.new

      Fog::Core::Connection.new("http://example.com", false, :path_prefix => "foo")

      assert_empty($stderr.string)
    end

    it "raises when the 'path' arg is present and this arg is supplied" do
      assert_raises(ArgumentError) do
        Fog::Core::Connection.new("http://example.com", false, :path_prefix => "foo", :path => "bar")
      end
    end
  end

  describe "#request" do
    describe "default behavior" do
      it "supplies the 'path' arg directly to Excon" do
        spy = Object.new
        spy.instance_eval do
          def params
            @params
          end
          def new(_, params)
            @params = params
          end
        end

        Object.stub_const("Excon", spy) do
          Fog::Core::Connection.new("http://example.com", false, :path => "bar")
          assert_equal("bar", spy.params[:path])
        end
      end
    end

    describe "with path_prefix supplied to the initializer" do
      let(:spy) do
        Object.new.tap do |spy|
          spy.instance_eval do
            def new(*_args); self; end
            def params; @params; end
            def request(params)
              @params = params
            end
          end
        end
      end

      it "uses the initializer-supplied :path_prefix arg with #request :arg to formulate a path to send to Excon.request" do
        Object.stub_const("Excon", spy) do
          c = Fog::Core::Connection.new("http://example.com", false, :path_prefix => "foo")
          c.request(:path => "bar")
          assert_equal("foo/bar", spy.params[:path])
        end
      end

      it "does not introduce consecutive '/'s into the path if 'path' starts with a '/'" do
        Object.stub_const("Excon", spy) do
          c = Fog::Core::Connection.new("http://example.com", false, :path_prefix => "foo")
          c.request(:path => "/bar")
          assert_equal("foo/bar", spy.params[:path])
        end
      end
    end
  end
end