summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/request.rb
blob: fc3ed44e646917313d5a4355e3aca1c3b88a1810 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/indirector/request'

describe Puppet::Indirector::Request do
    describe "when initializing" do
        it "should require an indirection name, a key, and a method" do
            lambda { Puppet::Indirector::Request.new }.should raise_error(ArgumentError)
        end

        it "should use provided value as the key if it is a string" do
            Puppet::Indirector::Request.new(:ind, :method, "mykey").key.should == "mykey"
        end

        it "should use provided value as the key if it is a symbol" do
            Puppet::Indirector::Request.new(:ind, :method, :mykey).key.should == :mykey
        end

        it "should use the name of the provided instance as its key if an instance is provided as the key instead of a string" do
            instance = mock 'instance', :name => "mykey"
            request = Puppet::Indirector::Request.new(:ind, :method, instance)
            request.key.should == "mykey"
            request.instance.should equal(instance)
        end

        it "should support options specified as a hash" do
            lambda { Puppet::Indirector::Request.new(:ind, :method, :key, :one => :two) }.should_not raise_error(ArgumentError)
        end

        it "should support nil options" do
            lambda { Puppet::Indirector::Request.new(:ind, :method, :key, nil) }.should_not raise_error(ArgumentError)
        end

        it "should support unspecified options" do
            lambda { Puppet::Indirector::Request.new(:ind, :method, :key) }.should_not raise_error(ArgumentError)
        end

        it "should fail if options are specified as anything other than nil or a hash" do
            lambda { Puppet::Indirector::Request.new(:ind, :method, :key, [:one, :two]) }.should raise_error(ArgumentError)
        end

        it "should use an empty options hash if nil was provided" do
            Puppet::Indirector::Request.new(:ind, :method, :key, nil).options.should == {}
        end

        it "should default to a nil node" do
            Puppet::Indirector::Request.new(:ind, :method, :key, nil).node.should be_nil
        end

        it "should set its node attribute if provided in the options" do
            Puppet::Indirector::Request.new(:ind, :method, :key, :node => "foo.com").node.should == "foo.com"
        end

        it "should default to a nil ip" do
            Puppet::Indirector::Request.new(:ind, :method, :key, nil).ip.should be_nil
        end

        it "should set its ip attribute if provided in the options" do
            Puppet::Indirector::Request.new(:ind, :method, :key, :ip => "192.168.0.1").ip.should == "192.168.0.1"
        end

        it "should default to being unauthenticated" do
            Puppet::Indirector::Request.new(:ind, :method, :key, nil).should_not be_authenticated
        end

        it "should set be marked authenticated if configured in the options" do
            Puppet::Indirector::Request.new(:ind, :method, :key, :authenticated => "eh").should be_authenticated
        end

        it "should keep its options as a hash even if a node is specified" do
            Puppet::Indirector::Request.new(:ind, :method, :key, :node => "eh").options.should be_instance_of(Hash)
        end

        it "should keep its options as a hash even if another option is specified" do
            Puppet::Indirector::Request.new(:ind, :method, :key, :foo => "bar").options.should be_instance_of(Hash)
        end

        describe "and the request key is a URI" do
            describe "and the URI is a 'file' URI" do
                before do
                    @request = Puppet::Indirector::Request.new(:ind, :method, "file:///my/file with spaces")
                end

                it "should set the request key to the unescaped full file path" do
                    @request.key.should == "/my/file with spaces"
                end

                it "should not set the protocol" do
                    @request.protocol.should be_nil
                end

                it "should not set the port" do
                    @request.port.should be_nil
                end

                it "should not set the server" do
                    @request.server.should be_nil
                end
            end

            it "should set the protocol to the URI scheme" do
                Puppet::Indirector::Request.new(:ind, :method, "http://host/stuff").protocol.should == "http"
            end

            it "should set the server if a server is provided" do
                Puppet::Indirector::Request.new(:ind, :method, "http://host/stuff").server.should == "host"
            end

            it "should set the server and port if both are provided" do
                Puppet::Indirector::Request.new(:ind, :method, "http://host:543/stuff").port.should == 543
            end

            it "should default to the masterport if the URI scheme is 'puppet'" do
                Puppet.settings.expects(:value).with(:masterport).returns "321"
                Puppet::Indirector::Request.new(:ind, :method, "puppet://host/stuff").port.should == 321
            end

            it "should use the provided port if the URI scheme is not 'puppet'" do
                Puppet::Indirector::Request.new(:ind, :method, "http://host/stuff").port.should == 80
            end

            it "should set the request key to the unescaped unqualified path from the URI" do
                Puppet::Indirector::Request.new(:ind, :method, "http:///stuff with spaces").key.should == "stuff with spaces"
            end

            it "should set the :uri attribute to the full URI" do
                Puppet::Indirector::Request.new(:ind, :method, "http:///stuff").uri.should == "http:///stuff"
            end
        end

        it "should allow indication that it should not read a cached instance" do
            Puppet::Indirector::Request.new(:ind, :method, :key, :ignore_cache => true).should be_ignore_cache
        end

        it "should default to not ignoring the cache" do
            Puppet::Indirector::Request.new(:ind, :method, :key).should_not be_ignore_cache
        end

        it "should allow indication that it should not not read an instance from the terminus" do
            Puppet::Indirector::Request.new(:ind, :method, :key, :ignore_terminus => true).should be_ignore_terminus
        end

        it "should default to not ignoring the terminus" do
            Puppet::Indirector::Request.new(:ind, :method, :key).should_not be_ignore_terminus
        end
    end

    it "should look use the Indirection class to return the appropriate indirection" do
        ind = mock 'indirection'
        Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns ind
        request = Puppet::Indirector::Request.new(:myind, :method, :key)

        request.indirection.should equal(ind)
    end

    it "should have a method for determining if the request is plural or singular" do
        Puppet::Indirector::Request.new(:myind, :method, :key).should respond_to(:plural?)
    end

    it "should be considered plural if the method is 'search'" do
        Puppet::Indirector::Request.new(:myind, :search, :key).should be_plural
    end

    it "should not be considered plural if the method is not 'search'" do
        Puppet::Indirector::Request.new(:myind, :find, :key).should_not be_plural
    end

    it "should use its uri, if it has one, as its string representation" do
        Puppet::Indirector::Request.new(:myind, :find, "foo://bar/baz").to_s.should == "foo://bar/baz"
    end

    it "should use its indirection name and key, if it has no uri, as its string representation" do
        Puppet::Indirector::Request.new(:myind, :find, "key") == "/myind/key"
    end

    it "should be able to return the URI-escaped key" do
        Puppet::Indirector::Request.new(:myind, :find, "my key").escaped_key.should == URI.escape("my key")
    end
end