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
|
# Walk array/hash to determine maximum depth. A scalar (anything but an
# Array or Hash) has depth 0.
def count_structure_levels(obj)
if obj.respond_to?(:keys)
# empty hash also has depth 0.
max_depth = 0
obj.keys.each do |key|
child_levels = 1 + count_structure_levels(obj[key])
max_depth = [max_depth, child_levels].max
end
max_depth
elsif obj.is_a?(Array)
# empty array also has depth 0.
max_depth = 0
obj.each do |child|
child_levels = 1 + count_structure_levels(child)
max_depth = [max_depth, child_levels].max
end
max_depth
else
0
end
end
Then /^I should get a '(.+)' exception$/ do |exception|
self.exception.message.to_s.should == exception
end
Then /^I should not get an exception$/ do
self.exception.should == nil
end
Then /^the response code should be '(.+)'$/ do |response_code|
case response_code.to_i
when 200
self.api_response.code.should == 200
when 400
self.exception.to_s.should match(/(Bad Request|400)/)
when 404
Then "I should get a 'RestClient::ResourceNotFound' exception"
end
end
Then /^the inflated responses key '(.+)' should be the integer '(\d+)'$/ do |key, int|
inflated_response[key].should == int.to_i
end
Then /^the inflated responses key '(.+)' should match '(.+)'$/ do |key, regex|
puts self.inflated_response.inspect if ENV['DEBUG']
self.inflated_response[key].should =~ /#{regex}/m
end
Then /^the inflated responses key '(.+)' should be literally '(.+)'$/ do |key, literal|
puts self.inflated_response.inspect if ENV['DEBUG']
to_check = case literal
when "true"
true
when "false"
false
end
self.inflated_response[key].should == to_check
end
Then /^the inflated response should match '(.+)' as json$/ do |regex|
puts self.inflated_response.inspect if ENV["DEBUG"]
Chef::JSON.to_json(self.inflated_response).should =~ /#{regex}/m
end
Then /^the inflated responses key '(.+)' should match '(.+)' as json$/ do |key, regex|
puts self.inflated_response.inspect if ENV["DEBUG"]
Chef::JSON.to_json(self.inflated_response[key]).should =~ /#{regex}/m
end
Then /^the inflated responses key '(.+)' item '(\d+)' should be '(.+)'$/ do |key, index, to_equal|
inflated_response[key][index.to_i].should == to_equal
end
Then /^the inflated responses key '(.+)' item '(\d+)' should be a kind of '(.+)'$/ do |key, index, constant|
inflated_response[key][index.to_i].should be_a_kind_of(eval(constant))
end
Then /^the inflated responses key '(.+)' item '(\d+)' key '(.+)' should be '(.+)'$/ do |key, index, sub_key, to_equal|
inflated_response[key][index.to_i][sub_key].should == to_equal
end
Then /^the inflated responses key '(.+)' sub-key '(.+)' should be an empty hash$/ do |key, sub_key|
inflated_response[key][sub_key].should == {}
end
Then /^the inflated responses key '(.+)' should be '(\d+)' items long$/ do |key, length|
inflated_response[key].length.should == length.to_i
end
Then /^the inflated responses key '(.+)' should not exist$/ do |key|
self.inflated_response.has_key?(key).should == false
end
Then /^the inflated responses key '(.+)' should exist$/ do |key|
self.inflated_response.has_key?(key).should == true
end
Then /^the inflated responses key '(.+)'.to_s should be '(.+)'$/ do |key, expected_value|
self.inflated_response[key].to_s.should == expected_value
end
Then /^the inflated response should be an empty array$/ do
self.inflated_response.should == []
end
Then /^the inflated response should be an empty hash$/ do
self.inflated_response.should == {}
end
Then /^the inflated response should include '(.+)'$/ do |entry|
if inflated_response.size == 1
inflated_response.should match(/#{entry}/)
else
inflated_response.detect { |n| n =~ /#{entry}/ }.should_not be_empty
end
end
Then /^the inflated response should be '(.+)' items long$/ do |length|
if length.respond_to?(:keys)
self.inflated_response.keys.length.should == length.to_i
else
self.inflated_response.length.should == length.to_i
end
end
Then /^the '(.+)' header should match '(.+)'$/ do |header, regex|
self.api_response.headers[header].should =~ /#{regex}/
end
Then /^the inflated responses key '(.+)' should include '(.+)'$/ do |key, regex|
if self.inflated_response[key].size == 1
self.inflated_response[key].first.should match(/#{regex}/)
else
self.inflated_response[key].detect { |n| n =~ /#{regex}/ }.should_not be_empty
end
end
Then /^the inflated response should match the '(.+)'$/ do |stash_name|
stash[stash_name].each do |k,v|
self.inflated_response[k.to_s].should == v
end
end
Then /^the inflated response should be the '(.+)'$/ do |stash_key|
self.inflated_response.should == stash[stash_key]
end
Then /^the stringified response should be the stringified '(.+)'$/ do |stash_key|
self.api_response.to_s.should == stash[stash_key].to_s
end
Then /^the inflated response should be a kind of '(.+)'$/ do |thing|
self.inflated_response.should be_a_kind_of(eval(thing))
end
Then /^the inflated response should respond to '(.+)' with '(.+)'$/ do |method, to_match|
to_match = Chef::JSON.from_json(to_match) if to_match =~ /^\[|\{/
to_match = true if to_match == 'true'
to_match = false if to_match == 'false'
self.inflated_response.to_hash[method].should == to_match
end
Then /^the inflated response should respond to '(.+)' and match '(.+)'$/ do |method, to_match|
self.inflated_response.to_hash[method].should == to_match
end
Then /^the inflated response should respond to '(.+)' and match '(.+)' as json$/ do |method, regex|
Chef::JSON.to_json(self.inflated_response.to_hash[method]).should =~ /#{regex}/m
end
#And the 'deep_array' component has depth of '50' levels
Then /^the '(.+)' component has depth of '(.+)' levels$/ do |method, levels|
count_structure_levels(self.inflated_response.to_hash[method]).should == levels.to_i
end
Then /^the fields in the inflated response should match the '(.+)'$/ do |stash_name|
self.inflated_response.each do |k,v|
unless k =~ /^_/ || k == 'couchrest-type'
stash[stash_name][k.to_sym].should == v
end
end
end
Then /^the data_bag named '(.+)' should not have an item named '(.+)'$/ do |data_bag, item|
exists = true
begin
Chef::DataBagItem.load(data_bag, item, @couchdb)
rescue
exists = false
end
exists.should == false
end
|