1+ #
2+ # Author:: Adam Jacob (<adam@opscode.com>)
3+ # Copyright:: Copyright (c) 2008 OpsCode, Inc.
4+ # License:: Apache License, Version 2.0
5+ #
6+ # Licensed under the Apache License, Version 2.0 (the "License");
7+ # you may not use this file except in compliance with the License.
8+ # You may obtain a copy of the License at
9+ #
10+ # http://www.apache.org/licenses/LICENSE-2.0
11+ #
12+ # Unless required by applicable law or agreed to in writing, software
13+ # distributed under the License is distributed on an "AS IS" BASIS,
14+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ # See the License for the specific language governing permissions and
16+ # limitations under the License.
17+ #
18+
19+ require File . expand_path ( File . join ( File . dirname ( __FILE__ ) , ".." , ".." , "spec_helper" ) )
20+
21+ describe Chef ::Provider ::HttpRequest , "initialize" do
22+ before ( :each ) do
23+ @node = mock ( "Chef::Node" , :null_object => true )
24+ @new_resource = mock ( "Chef::Resource::HttpRequest" ,
25+ :null_object => true ,
26+ :name => "adam" ,
27+ :url => "http://www.opscode.com" ,
28+ :message => "is cool"
29+ )
30+ @provider = Chef ::Provider ::HttpRequest . new ( @node , @new_resource )
31+ end
32+
33+ it "should return a Chef::Provider::HttpRequest" do
34+ @provider . should be_a_kind_of ( Chef ::Provider ::HttpRequest )
35+ end
36+ end
37+
38+ describe Chef ::Provider ::HttpRequest , "load_current_resource" do
39+ before ( :each ) do
40+ @node = mock ( "Chef::Node" , :null_object => true )
41+ @new_resource = mock ( "Chef::Resource::HttpRequest" ,
42+ :null_object => true ,
43+ :name => "adam" ,
44+ :url => "http://www.opscode.com" ,
45+ :message => "is cool"
46+ )
47+ @provider = Chef ::Provider ::HttpRequest . new ( @node , @new_resource )
48+ end
49+
50+ it "should set up a Chef::REST client" do
51+ Chef ::REST . should_receive ( :new ) . with ( @new_resource . url )
52+ @provider . load_current_resource
53+ end
54+ end
55+
56+ describe Chef ::Provider ::HttpRequest , "action_get" do
57+ before ( :each ) do
58+ @node = mock ( "Chef::Node" , :null_object => true )
59+ @new_resource = mock ( "Chef::Resource::HttpRequest" ,
60+ :null_object => true ,
61+ :name => "adam" ,
62+ :url => "http://www.opscode.com" ,
63+ :message => "is cool"
64+ )
65+ @rest = mock ( "Chef::REST" ,
66+ :null_object => true ,
67+ :create_url => "http://www.opscode.com" ,
68+ :run_request => "you made it!"
69+ )
70+ @provider = Chef ::Provider ::HttpRequest . new ( @node , @new_resource )
71+ @provider . rest = @rest
72+ end
73+
74+ it "should create the url with a message argument" do
75+ @rest . should_receive ( :create_url ) . with ( "#{ @new_resource . url } ?message=#{ @new_resource . message } " )
76+ @provider . action_get
77+ end
78+
79+ it "should run a GET request" do
80+ @rest . should_receive ( :run_request ) . with ( :GET , @rest . create_url , false , 10 , false )
81+ @provider . action_get
82+ end
83+
84+ it "should update the resource" do
85+ @new_resource . should_receive ( :updated= ) . with ( true )
86+ @provider . action_get
87+ end
88+ end
89+
90+ describe Chef ::Provider ::HttpRequest , "action_put" do
91+ before ( :each ) do
92+ @node = mock ( "Chef::Node" , :null_object => true )
93+ @new_resource = mock ( "Chef::Resource::HttpRequest" ,
94+ :null_object => true ,
95+ :name => "adam" ,
96+ :url => "http://www.opscode.com" ,
97+ :message => "is cool"
98+ )
99+ @rest = mock ( "Chef::REST" ,
100+ :null_object => true ,
101+ :create_url => "http://www.opscode.com" ,
102+ :run_request => "you made it!"
103+ )
104+ @provider = Chef ::Provider ::HttpRequest . new ( @node , @new_resource )
105+ @provider . rest = @rest
106+ end
107+
108+ it "should create the url" do
109+ @rest . should_receive ( :create_url ) . with ( "#{ @new_resource . url } " )
110+ @provider . action_put
111+ end
112+
113+ it "should run a PUT request with the message as the payload" do
114+ @rest . should_receive ( :run_request ) . with ( :PUT , @rest . create_url , @new_resource . message , 10 , false )
115+ @provider . action_put
116+ end
117+
118+ it "should update the resource" do
119+ @new_resource . should_receive ( :updated= ) . with ( true )
120+ @provider . action_put
121+ end
122+ end
123+
124+ describe Chef ::Provider ::HttpRequest , "action_post" do
125+ before ( :each ) do
126+ @node = mock ( "Chef::Node" , :null_object => true )
127+ @new_resource = mock ( "Chef::Resource::HttpRequest" ,
128+ :null_object => true ,
129+ :name => "adam" ,
130+ :url => "http://www.opscode.com" ,
131+ :message => "is cool"
132+ )
133+ @rest = mock ( "Chef::REST" ,
134+ :null_object => true ,
135+ :create_url => "http://www.opscode.com" ,
136+ :run_request => "you made it!"
137+ )
138+ @provider = Chef ::Provider ::HttpRequest . new ( @node , @new_resource )
139+ @provider . rest = @rest
140+ end
141+
142+ it "should create the url" do
143+ @rest . should_receive ( :create_url ) . with ( "#{ @new_resource . url } " )
144+ @provider . action_post
145+ end
146+
147+ it "should run a PUT request with the message as the payload" do
148+ @rest . should_receive ( :run_request ) . with ( :POST , @rest . create_url , @new_resource . message , 10 , false )
149+ @provider . action_post
150+ end
151+
152+ it "should update the resource" do
153+ @new_resource . should_receive ( :updated= ) . with ( true )
154+ @provider . action_post
155+ end
156+ end
157+
158+ describe Chef ::Provider ::HttpRequest , "action_delete" do
159+ before ( :each ) do
160+ @node = mock ( "Chef::Node" , :null_object => true )
161+ @new_resource = mock ( "Chef::Resource::HttpRequest" ,
162+ :null_object => true ,
163+ :name => "adam" ,
164+ :url => "http://www.opscode.com" ,
165+ :message => "is cool"
166+ )
167+ @rest = mock ( "Chef::REST" ,
168+ :null_object => true ,
169+ :create_url => "http://www.opscode.com" ,
170+ :run_request => "you made it!"
171+ )
172+ @provider = Chef ::Provider ::HttpRequest . new ( @node , @new_resource )
173+ @provider . rest = @rest
174+ end
175+
176+ it "should create the url" do
177+ @rest . should_receive ( :create_url ) . with ( "#{ @new_resource . url } " )
178+ @provider . action_delete
179+ end
180+
181+ it "should run a DELETE request" do
182+ @rest . should_receive ( :run_request ) . with ( :DELETE , @rest . create_url , false , 10 , false )
183+ @provider . action_delete
184+ end
185+
186+ it "should update the resource" do
187+ @new_resource . should_receive ( :updated= ) . with ( true )
188+ @provider . action_delete
189+ end
190+ end
0 commit comments