summaryrefslogtreecommitdiffstats
path: root/spec/unit/network/http/rack/rest_spec.rb
blob: b9d835284121ce794676889cce6623fce2af2129 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../../../spec_helper'
require 'puppet/network/http/rack' if Puppet.features.rack?
require 'puppet/network/http/rack/rest'

describe "Puppet::Network::HTTP::RackREST" do
    confine "Rack is not available" => Puppet.features.rack?

    it "should include the Puppet::Network::HTTP::Handler module" do
        Puppet::Network::HTTP::RackREST.ancestors.should be_include(Puppet::Network::HTTP::Handler)
    end

    describe "when initializing" do
        it "should call the Handler's initialization hook with its provided arguments" do
            Puppet::Network::HTTP::RackREST.any_instance.expects(:initialize_for_puppet).with(:server => "my", :handler => "arguments")
            Puppet::Network::HTTP::RackREST.new(:server => "my", :handler => "arguments")
        end
    end

    describe "when serving a request" do
        before :all do
            @model_class = stub('indirected model class')
            Puppet::Indirector::Indirection.stubs(:model).with(:foo).returns(@model_class)
            @handler = Puppet::Network::HTTP::RackREST.new(:handler => :foo)
        end

        before :each do
            @response = Rack::Response.new()
        end

        def mk_req(uri, opts = {})
            env = Rack::MockRequest.env_for(uri, opts)
            Rack::Request.new(env)
        end

        describe "and using the HTTP Handler interface" do
            it "should return the HTTP_ACCEPT parameter as the accept header" do
                req = mk_req('/', 'HTTP_ACCEPT' => 'myaccept')
                @handler.accept_header(req).should == "myaccept"
            end

            it "should return the CONTENT_TYPE parameter as the content type header" do
                req = mk_req('/', 'CONTENT_TYPE' => 'mycontent')
                @handler.content_type_header(req).should == "mycontent"
            end

            it "should use the REQUEST_METHOD as the http method" do
                req = mk_req('/', :method => 'MYMETHOD')
                @handler.http_method(req).should == "MYMETHOD"
            end

            it "should return the request path as the path" do
                req = mk_req('/foo/bar')
                @handler.path(req).should == "/foo/bar"
            end

            it "should return the request body as the body" do
                req = mk_req('/foo/bar', :input => 'mybody')
                @handler.body(req).should == "mybody"
            end

            it "should set the response's content-type header when setting the content type" do
                @header = mock 'header'
                @response.expects(:header).returns @header
                @header.expects(:[]=).with('Content-Type', "mytype")

                @handler.set_content_type(@response, "mytype")
            end

            it "should set the status and write the body when setting the response for a request" do
                @response.expects(:status=).with(400)
                @response.expects(:write).with("mybody")

                @handler.set_response(@response, "mybody", 400)
            end

            describe "when result is a File" do
                before :each do
                    stat = stub 'stat', :size => 100
                    @file = stub 'file', :stat => stat, :path => "/tmp/path"
                    @file.stubs(:is_a?).with(File).returns(true)
                end

                it "should set the Content-Length header" do
                    @response.expects(:[]=).with("Content-Length", 100)

                    @handler.set_response(@response, @file, 200)
                end

                it "should return a RackFile adapter as body" do
                    @response.expects(:body=).with { |val| val.is_a?(Puppet::Network::HTTP::RackREST::RackFile) }

                    @handler.set_response(@response, @file, 200)
                end
            end
        end

        describe "and determining the request parameters" do
            it "should include the HTTP request parameters, with the keys as symbols" do
                req = mk_req('/?foo=baz&bar=xyzzy')
                result = @handler.params(req)
                result[:foo].should == "baz"
                result[:bar].should == "xyzzy"
            end

            it "should CGI-decode the HTTP parameters" do
                encoding = CGI.escape("foo bar")
                req = mk_req("/?foo=#{encoding}")
                result = @handler.params(req)
                result[:foo].should == "foo bar"
            end

            it "should convert the string 'true' to the boolean" do
                req = mk_req("/?foo=true")
                result = @handler.params(req)
                result[:foo].should be_true
            end

            it "should convert the string 'false' to the boolean" do
                req = mk_req("/?foo=false")
                result = @handler.params(req)
                result[:foo].should be_false
            end

            it "should convert integer arguments to Integers" do
                req = mk_req("/?foo=15")
                result = @handler.params(req)
                result[:foo].should == 15
            end

            it "should convert floating point arguments to Floats" do
                req = mk_req("/?foo=1.5")
                result = @handler.params(req)
                result[:foo].should == 1.5
            end

            it "should YAML-load and CGI-decode values that are YAML-encoded" do
                escaping = CGI.escape(YAML.dump(%w{one two}))
                req = mk_req("/?foo=#{escaping}")
                result = @handler.params(req)
                result[:foo].should == %w{one two}
            end

            it "should not allow the client to set the node via the query string" do
                req = mk_req("/?node=foo")
                @handler.params(req)[:node].should be_nil
            end

            it "should not allow the client to set the IP address via the query string" do
                req = mk_req("/?ip=foo")
                @handler.params(req)[:ip].should be_nil
            end

            it "should pass the client's ip address to model find" do
                req = mk_req("/", 'REMOTE_ADDR' => 'ipaddress')
                @handler.params(req)[:ip].should == "ipaddress"
            end

            it "should set 'authenticated' to false if no certificate is present" do
                req = mk_req('/')
                @handler.params(req)[:authenticated].should be_false
            end
        end

        describe "with pre-validated certificates" do

            it "should use the :ssl_client_header to determine the parameter when looking for the certificate" do
                Puppet.settings.stubs(:value).returns "eh"
                Puppet.settings.expects(:value).with(:ssl_client_header).returns "myheader"
                req = mk_req('/', "myheader" => "/CN=host.domain.com")
                @handler.params(req)
            end

            it "should retrieve the hostname by matching the certificate parameter" do
                Puppet.settings.stubs(:value).returns "eh"
                Puppet.settings.expects(:value).with(:ssl_client_header).returns "myheader"
                req = mk_req('/', "myheader" => "/CN=host.domain.com")
                @handler.params(req)[:node].should == "host.domain.com"
            end

            it "should use the :ssl_client_header to determine the parameter for checking whether the host certificate is valid" do
                Puppet.settings.stubs(:value).with(:ssl_client_header).returns "certheader"
                Puppet.settings.expects(:value).with(:ssl_client_verify_header).returns "myheader"
                req = mk_req('/', "myheader" => "SUCCESS", "certheader" => "/CN=host.domain.com")
                @handler.params(req)
            end

            it "should consider the host authenticated if the validity parameter contains 'SUCCESS'" do
                Puppet.settings.stubs(:value).with(:ssl_client_header).returns "certheader"
                Puppet.settings.stubs(:value).with(:ssl_client_verify_header).returns "myheader"
                req = mk_req('/', "myheader" => "SUCCESS", "certheader" => "/CN=host.domain.com")
                @handler.params(req)[:authenticated].should be_true
            end

            it "should consider the host unauthenticated if the validity parameter does not contain 'SUCCESS'" do
                Puppet.settings.stubs(:value).with(:ssl_client_header).returns "certheader"
                Puppet.settings.stubs(:value).with(:ssl_client_verify_header).returns "myheader"
                req = mk_req('/', "myheader" => "whatever", "certheader" => "/CN=host.domain.com")
                @handler.params(req)[:authenticated].should be_false
            end

            it "should consider the host unauthenticated if no certificate information is present" do
                Puppet.settings.stubs(:value).with(:ssl_client_header).returns "certheader"
                Puppet.settings.stubs(:value).with(:ssl_client_verify_header).returns "myheader"
                req = mk_req('/', "myheader" => nil, "certheader" => "/CN=host.domain.com")
                @handler.params(req)[:authenticated].should be_false
            end

            it "should resolve the node name with an ip address look-up if no certificate is present" do
                Puppet.settings.stubs(:value).returns "eh"
                Puppet.settings.expects(:value).with(:ssl_client_header).returns "myheader"
                req = mk_req('/', "myheader" => nil)
                @handler.expects(:resolve_node).returns("host.domain.com")
                @handler.params(req)[:node].should == "host.domain.com"
            end
        end
    end
end

describe Puppet::Network::HTTP::RackREST::RackFile do
    before(:each) do
        stat = stub 'stat', :size => 100
        @file = stub 'file', :stat => stat, :path => "/tmp/path"
        @rackfile = Puppet::Network::HTTP::RackREST::RackFile.new(@file)
    end

    it "should have an each method" do
        @rackfile.should be_respond_to(:each)
    end

    it "should yield file chunks by chunks" do
        @file.expects(:read).times(3).with(8192).returns("1", "2", nil)
        i = 1
        @rackfile.each do |chunk|
            chunk.to_i.should == i
            i += 1
        end
    end

    it "should have a close method" do
        @rackfile.should be_respond_to(:close)
    end

    it "should delegate close to File close" do
        @file.expects(:close)
        @rackfile.close
    end
end