/
http_backend_implementation_examples.rb
266 lines (221 loc) · 9.63 KB
/
http_backend_implementation_examples.rb
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
shared_examples_for "HTTP backend" do
describe "HEAD requests" do
before :each do
setup_http_mock(:head, @backend.path("/riak/","foo").to_s, :body => "")
end
it "should return only the headers when the request succeeds" do
response = @backend.head(200, "/riak/","foo")
response.should_not have_key(:body)
response[:headers].should be_kind_of(Hash)
response[:code].should == 200
end
it "should raise a FailedRequest exception when the request fails" do
lambda { @backend.head(301, "/riak/", "foo") }.should raise_error(Riak::FailedRequest)
end
it "should raise an error if an invalid resource path is given" do
lambda { @backend.head(200, "/riak/") }.should raise_error(ArgumentError)
end
it "should not raise a FailedRequest if one of the expected response codes matches" do
lambda { @backend.head([200, 301], "/riak/", "foo") }.should_not raise_error(Riak::FailedRequest)
end
end
describe "GET requests" do
before :each do
setup_http_mock(:get, @backend.path("/riak/","foo").to_s, :body => "Success!")
end
it "should return the response body and headers when the request succeeds" do
response = @backend.get(200, "/riak/","foo")
response[:body].should == "Success!"
response[:headers].should be_kind_of(Hash)
response[:code].should == 200
end
it "should raise a FailedRequest exception when the request fails" do
lambda { @backend.get(304, "/riak/","foo") }.should raise_error(Riak::FailedRequest)
end
it "should not raise a FailedRequest if one of the expected response codes matches" do
lambda { @backend.get([200, 301], "/riak/","foo") }.should_not raise_error(Riak::FailedRequest)
end
it "should yield successive chunks of the response to the given block but not return the entire body" do
chunks = ""
response = @backend.get(200, "/riak/","foo") do |chunk|
chunks << chunk
end
chunks.should == "Success!"
response.should_not have_key(:body)
response[:headers].should be_kind_of(Hash)
response[:code].should == 200
end
it "should raise an error if an invalid resource path is given" do
lambda { @backend.get(200, "/riak/") }.should raise_error(ArgumentError)
end
end
describe "DELETE requests" do
before :each do
setup_http_mock(:delete, @backend.path("/riak/","foo").to_s, :body => "Success!")
end
it "should return the response body and headers when the request succeeds" do
response = @backend.delete(200, "/riak/","foo")
response[:body].should == "Success!"
response[:headers].should be_kind_of(Hash)
end
it "should raise a FailedRequest exception when the request fails" do
lambda { @backend.delete(304, "/riak/","foo") }.should raise_error(Riak::FailedRequest)
end
it "should not raise a FailedRequest if one of the expected response codes matches" do
lambda { @backend.delete([200, 301], "/riak/","foo") }.should_not raise_error(Riak::FailedRequest)
end
it "should yield successive chunks of the response to the given block but not return the entire body" do
chunks = ""
response = @backend.delete(200, "/riak/","foo") do |chunk|
chunks << chunk
end
chunks.should == "Success!"
response.should_not have_key(:body)
response[:headers].should be_kind_of(Hash)
response[:code].should == 200
end
it "should raise an error if an invalid resource path is given" do
lambda { @backend.delete(200, "/riak/") }.should raise_error(ArgumentError)
end
end
describe "PUT requests" do
before :each do
setup_http_mock(:put, @backend.path("/riak/","foo").to_s, :body => "Success!")
end
it "should return the response body, headers, and code when the request succeeds" do
response = @backend.put(200, "/riak/","foo", "This is the body.")
response[:body].should == "Success!"
response[:headers].should be_kind_of(Hash)
response[:code].should == 200
end
it "should raise a FailedRequest exception when the request fails" do
lambda { @backend.put(204, "/riak/","foo", "This is the body.") }.should raise_error(Riak::FailedRequest)
end
it "should not raise a FailedRequest if one of the expected response codes matches" do
lambda { @backend.put([200, 204], "/riak/","foo", "This is the body.") }.should_not raise_error(Riak::FailedRequest)
end
it "should yield successive chunks of the response to the given block but not return the entire body" do
chunks = ""
response = @backend.put(200, "/riak/","foo", "This is the body.") do |chunk|
chunks << chunk
end
chunks.should == "Success!"
response.should_not have_key(:body)
response[:headers].should be_kind_of(Hash)
response[:code].should == 200
end
it "should raise an error if an invalid resource path is given" do
lambda { @backend.put(200, "/riak/") }.should raise_error(ArgumentError)
end
it "should raise an error if no body data is given" do
lambda { @backend.put(200, "/riak/","foo") }.should raise_error(ArgumentError)
end
it "should raise an error if the body is not a string" do
lambda { @backend.put(200, "/riak/","foo", 123) }.should raise_error(ArgumentError)
end
end
describe "POST requests" do
before :each do
setup_http_mock(:post, @backend.path("/riak/","foo").to_s, :body => "Success!")
end
it "should return the response body, headers, and code when the request succeeds" do
response = @backend.post(200, "/riak/","foo", "This is the body.")
response[:body].should == "Success!"
response[:headers].should be_kind_of(Hash)
response[:code].should == 200
end
it "should raise a FailedRequest exception when the request fails" do
lambda { @backend.post(204, "/riak/", "foo", "This is the body.") }.should raise_error(Riak::FailedRequest)
end
it "should not raise a FailedRequest if one of the expected response codes matches" do
lambda { @backend.post([200, 204], "/riak/", "foo", "This is the body.") }.should_not raise_error(Riak::FailedRequest)
end
it "should yield successive chunks of the response to the given block but not return the entire body" do
chunks = ""
response = @backend.post(200, "/riak/", "foo", "This is the body.") do |chunk|
chunks << chunk
end
chunks.should == "Success!"
response.should_not have_key(:body)
response[:headers].should be_kind_of(Hash)
response[:code].should == 200
end
it "should raise an error if an invalid resource path is given" do
lambda { @backend.post(200, "/riak/") }.should raise_error(ArgumentError)
end
it "should raise an error if no body data is given" do
lambda { @backend.post(200, "/riak/", "foo") }.should raise_error(ArgumentError)
end
it "should raise an error if the body is not a string" do
lambda { @backend.post(200, "/riak/", "foo", 123) }.should raise_error(ArgumentError)
end
end
describe "Responses with no body" do
[204, 205, 304].each do |code|
[:get, :post, :put, :delete].each do |method|
it "should not return a body on #{method.to_s.upcase} for #{code}" do
setup_http_mock(method, @backend.path("/riak/","foo").to_s, :status => code)
response = if method == :post || method == :put
@backend.send(method, code,"/riak/","foo", "This is the body")
else
@backend.send(method, code, "/riak/", "foo")
end
response.should_not have_key(:body)
end
end
end
end
describe "SSL" do
it "should be supported" do
@client.http_port = $mock_server.port + 1 unless @client.http_backend == :NetHTTP
@client.ssl = true
setup_http_mock(:get, @backend.path("/riak/","ssl").to_s, :body => "Success!")
response = @backend.get(200, "/riak/","ssl")
response[:code].should == 200
end
end
describe "HTTP Basic Authentication", :basic_auth => true do
it "should add the http basic auth header" do
@client.basic_auth = "ripple:rocks"
if @client.http_backend == :NetHTTP
setup_http_mock(:get, "http://ripple:rocks@127.0.0.1:8098/riak/auth", :body => 'Success!')
else
@_mock_set = "Basic #{Base64::encode64("ripple:rocks").strip}"
$mock_server.attach do |env|
$mock_server.satisfied = env['HTTP_AUTHORIZATION'] == @_mock_set
[200, {}, Array('Success!')]
end
end
response = @backend.get(200, "/riak/", "auth")
response[:code].should == 200
end
end
describe "Invalid responses" do
def bad_request(method)
if method == :post || method == :put
@backend.send(method, 200, "/riak/","foo", "body")
else
@backend.send(method, 200, "/riak/","foo")
end
end
[:get, :post, :put, :delete].each do |method|
context method.to_s do
before(:each) do
setup_http_mock(method, @backend.path("/riak/","foo").to_s, :body => "Failure!", :status => 400, 'Content-Type' => 'text/plain' )
end
it "raises an HTTPFailedRequest exeption" do
lambda { bad_request(method) }.should raise_error(Riak::HTTPFailedRequest)
end
it "should normalize the response header keys to lower case" do
begin
bad_request(method)
rescue Riak::HTTPFailedRequest => fr
fr.headers.keys.should =~ fr.headers.keys.collect(&:downcase)
else
fail "No exception raised!"
end
end
end
end
end
end