summaryrefslogtreecommitdiff
path: root/chef/spec/unit/rest_spec.rb
blob: ba892944fca5af913c07f39b3655d36471811f0f (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
require 'uri'
require 'net/https'

describe Chef::REST, "initialize method" do
  it "should create a new Chef::REST" do
    Chef::REST.new("url").should be_kind_of(Chef::REST)
  end
end

describe Chef::REST, "get_rest method" do
  it "should create a url from the path and base url" do
    URI.should_receive(:parse).with("url/monkey")
    r = Chef::REST.new("url")
    r.stub!(:run_request)
    r.get_rest("monkey")
  end
  
  it "should call run_request :GET with the composed url object" do
    URI.stub!(:parse).and_return(true)
    r = Chef::REST.new("url")
    r.should_receive(:run_request).with(:GET, true, false, 10, false).and_return(true)
    r.get_rest("monkey")
  end
end

describe Chef::REST, "delete_rest method" do
  it "should create a url from the path and base url" do
    URI.should_receive(:parse).with("url/monkey")
    r = Chef::REST.new("url")
    r.stub!(:run_request)
    r.delete_rest("monkey")
  end
  
  it "should call run_request :DELETE with the composed url object" do
    URI.stub!(:parse).and_return(true)
    r = Chef::REST.new("url")
    r.should_receive(:run_request).with(:DELETE, true).and_return(true)
    r.delete_rest("monkey")
  end
end

describe Chef::REST, "post_rest method" do
  it "should create a url from the path and base url" do
    URI.should_receive(:parse).with("url/monkey")
    r = Chef::REST.new("url")
    r.stub!(:run_request)
    r.post_rest("monkey", "data")
  end
  
  it "should call run_request :POST with the composed url object and data" do
    URI.stub!(:parse).and_return(true)
    r = Chef::REST.new("url")
    r.should_receive(:run_request).with(:POST, true, "data").and_return(true)
    r.post_rest("monkey", "data")
  end
end

describe Chef::REST, "put_rest method" do
  it "should create a url from the path and base url" do
    URI.should_receive(:parse).with("url/monkey")
    r = Chef::REST.new("url")
    r.stub!(:run_request)
    r.put_rest("monkey", "data")
  end
  
  it "should call run_request :PUT with the composed url object and data" do
    URI.stub!(:parse).and_return(true)
    r = Chef::REST.new("url")
    r.should_receive(:run_request).with(:PUT, true, "data").and_return(true)
    r.put_rest("monkey", "data")
  end
end

describe Chef::REST, "run_request method" do
  before(:each) do
    @r = Chef::REST.new("url")
    @url_mock = mock("URI", :null_object => true)
    @url_mock.stub!(:host).and_return("one")
    @url_mock.stub!(:port).and_return("80")
    @url_mock.stub!(:path).and_return("/")
    @url_mock.stub!(:query).and_return("foo=bar")
    @url_mock.stub!(:scheme).and_return("https")
    @url_mock.stub!(:to_s).and_return("https://one:80/?foo=bar")
    @http_response_mock = mock("Net::HTTPSuccess", :null_object => true)
    @http_response_mock.stub!(:kind_of?).with(Net::HTTPSuccess).and_return(true)
    @http_response_mock.stub!(:body).and_return("ninja")
    @http_response_mock.stub!(:error!).and_return(true)
    @http_response_mock.stub!(:header).and_return({ 'Content-Length' => "5" })
    @http_mock = mock("Net::HTTP", :null_object => true)
    @http_mock.stub!(:verify_mode=).and_return(true)
    @http_mock.stub!(:read_timeout=).and_return(true)
    @http_mock.stub!(:use_ssl=).with(true).and_return(true)
    @data_mock = mock("Data", :null_object => true)
    @data_mock.stub!(:to_json).and_return('{ "one": "two" }')
    @request_mock = mock("Request", :null_object => true)
    @request_mock.stub!(:body=).and_return(true)
    @request_mock.stub!(:method).and_return(true)
    @request_mock.stub!(:path).and_return(true)
    @http_mock.stub!(:request).and_return(@http_response_mock)
    @tf_mock = mock(Tempfile, { :print => true, :close => true, :write => true })
    Tempfile.stub!(:new).with("chef-rest").and_return(@tf_mock)
  end
  
  def do_run_request(method=:GET, data=false, limit=10, raw=false)
    Net::HTTP.stub!(:new).and_return(@http_mock)
    @r.run_request(method, @url_mock, data, limit, raw)
  end
  
  it "should raise an exception if the redirect limit is 0" do
    lambda { @r.run_request(:GET, "/", false, 0)}.should raise_error(ArgumentError)
  end
  
  it "should use SSL if the url starts with https" do
    @url_mock.should_receive(:scheme).and_return("https")
    @http_mock.should_receive(:use_ssl=).with(true).and_return(true)
    do_run_request
  end
  
  it "should set the OpenSSL Verify Mode to verify_none if requested" do
    @http_mock.should_receive(:verify_mode=).and_return(true)
    do_run_request
  end
  
  it "should set a read timeout based on the rest_timeout config option" do
    Chef::Config[:rest_timeout] = 10
    @http_mock.should_receive(:read_timeout=).with(10).and_return(true)
    do_run_request
  end
  
  it "should set the cookie for this request if one exists for the given host:port" do
    @r.cookies = { "#{@url_mock.host}:#{@url_mock.port}" => "cookie monster" }
    Net::HTTP::Get.should_receive(:new).with("/?foo=bar", 
      { 'Accept' => 'application/json', 'Cookie' => 'cookie monster' }
    ).and_return(@request_mock)
    do_run_request
  end
  
  it "should build a new HTTP GET request" do
    Net::HTTP::Get.should_receive(:new).with("/?foo=bar", 
      { 'Accept' => 'application/json' }
    ).and_return(@request_mock)
    do_run_request
  end
  
  it "should build a new HTTP POST request" do
    Net::HTTP::Post.should_receive(:new).with("/", 
      { 'Accept' => 'application/json', "Content-Type" => 'application/json' }
    ).and_return(@request_mock)
    do_run_request(:POST, @data_mock)
  end
  
  it "should build a new HTTP PUT request" do
    Net::HTTP::Put.should_receive(:new).with("/", 
      { 'Accept' => 'application/json', "Content-Type" => 'application/json' }
    ).and_return(@request_mock)
    do_run_request(:PUT, @data_mock)
  end
  
  it "should build a new HTTP DELETE request" do
    Net::HTTP::Delete.should_receive(:new).with("/?foo=bar", 
      { 'Accept' => 'application/json' }
    ).and_return(@request_mock)
    do_run_request(:DELETE)
  end
  
  it "should raise an error if the method is not GET/PUT/POST/DELETE" do
    lambda { do_run_request(:MONKEY) }.should raise_error(ArgumentError)
  end
  
  it "should run an http request" do
    @http_mock.should_receive(:request).and_return(@http_response_mock)
    do_run_request
  end
  
  it "should return the body of the response on success" do
    do_run_request.should eql("ninja")
  end
  
  it "should inflate the body as to an object if JSON is returned" do
    @http_response_mock.stub!(:[]).with('content-type').and_return("application/json")
    JSON.should_receive(:parse).with("ninja").and_return(true)
    do_run_request
  end
  
  it "should call run_request again on a Redirect response" do
    @http_response_mock.stub!(:kind_of?).with(Net::HTTPSuccess).and_return(false)
    @http_response_mock.stub!(:kind_of?).with(Net::HTTPFound).and_return(true)
    @http_response_mock.stub!(:[]).with('location').and_return(@url_mock.path)
    lambda { do_run_request(method=:GET, data=false, limit=1) }.should raise_error(ArgumentError)
  end
  
  it "should raise an exception on an unsuccessful request" do
    @http_response_mock.stub!(:kind_of?).with(Net::HTTPSuccess).and_return(false)
    @http_response_mock.stub!(:kind_of?).with(Net::HTTPFound).and_return(false)
    @http_response_mock.should_receive(:error!)
    do_run_request
  end
  
  it "should build a new HTTP GET request without the application/json accept header for raw reqs" do
    Net::HTTP::Get.should_receive(:new).with("/?foo=bar", {}).and_return(@request_mock)
    do_run_request(:GET, false, 10, true)
  end
  
  it "should create a tempfile for the output of a raw request" do
    @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
    Tempfile.should_receive(:new).with("chef-rest").and_return(@tf_mock)
    do_run_request(:GET, false, 10, true).should eql(@tf_mock)    
  end
  
  it "should read the body of the response in chunks on a raw request" do
    @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
    @http_response_mock.should_receive(:read_body).and_return(true)
    do_run_request(:GET, false, 10, true)
  end
  
  it "should populate the tempfile with the value of the raw request" do
    @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
    @http_response_mock.stub!(:read_body).and_yield("ninja")
    @tf_mock.should_receive(:write, "ninja").once.and_return(true)
    do_run_request(:GET, false, 10, true)
  end
  
  it "should close the tempfile if we're doing a raw request" do
    @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
    @tf_mock.should_receive(:close).once.and_return(true)
    do_run_request(:GET, false, 10, true)
  end
  
  it "should not raise a divide by zero exception if the size is 0" do
    @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
    @http_response_mock.stub!(:header).and_return({ 'Content-Length' => "5" })
    @http_response_mock.stub!(:read_body).and_yield('')
    lambda { do_run_request(:GET, false, 10, true) }.should_not raise_error(ZeroDivisionError)
  end
  
  it "should not raise a divide by zero exception if the Content-Length is 0" do
    @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
    @http_response_mock.stub!(:header).and_return({ 'Content-Length' => "0" })
    @http_response_mock.stub!(:read_body).and_yield("ninja")
    lambda { do_run_request(:GET, false, 10, true) }.should_not raise_error(ZeroDivisionError)
  end
  
  it "should call read_body without a block if the request is not raw" do
    @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
    @http_response_mock.should_receive(:read_body)
    do_run_request(:GET, false, 10, false)
  end

end