httpadapter 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,415 @@
1
+ # Copyright (C) 2010 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'spec_helper'
16
+
17
+ require 'httpadapter/adapters/net_http'
18
+
19
+ describe HTTPAdapter::NetHTTPRequestAdapter do
20
+ it 'should raise an error for converting from an invalid tuple' do
21
+ (lambda do
22
+ HTTPAdapter.specialize_request(
23
+ ['GET', '/', [], [42]], HTTPAdapter::NetHTTPRequestAdapter
24
+ )
25
+ end).should raise_error(TypeError)
26
+ end
27
+
28
+ it 'should raise an error for converting from an invalid request' do
29
+ (lambda do
30
+ HTTPAdapter.adapt_request(
31
+ 42, HTTPAdapter::NetHTTPRequestAdapter
32
+ )
33
+ end).should raise_error(TypeError)
34
+ end
35
+
36
+ it 'should raise an error for the bogus HTTP method' do
37
+ (lambda do
38
+ tuple = [
39
+ 'BOGUS',
40
+ 'http://www.example.com/path?query',
41
+ [
42
+ ['Accept', 'application/json'],
43
+ ['Content-Type', 'application/json; charset=utf-8'],
44
+ ['Content-Length', '27']
45
+ ],
46
+ ['{"three":3,', '"two":2,', '"one":1}']
47
+ ]
48
+ HTTPAdapter.specialize_request(
49
+ tuple, HTTPAdapter::NetHTTPRequestAdapter
50
+ )
51
+ end).should raise_error(ArgumentError)
52
+ end
53
+ end
54
+
55
+ describe HTTPAdapter::NetHTTPRequestAdapter,
56
+ 'converting from Net::HTTP::Get' do
57
+ before do
58
+ @request = Net::HTTP::Get.new('/path/to/resource')
59
+ @request['Accept'] = 'application/json'
60
+ @result = HTTPAdapter.adapt_request(
61
+ @request, HTTPAdapter::NetHTTPRequestAdapter
62
+ )
63
+ @method, @uri, @headers, @body = @result
64
+ end
65
+
66
+ it 'should convert the HTTP method properly' do
67
+ @method.should == 'GET'
68
+ end
69
+
70
+ it 'should convert the URI properly' do
71
+ @uri.should == '/path/to/resource'
72
+ end
73
+
74
+ it 'should convert the headers properly' do
75
+ accept = nil
76
+ @headers.each do |header, value|
77
+ header.should be_kind_of(String)
78
+ value.should be_kind_of(String)
79
+ accept = value if header == 'Accept'
80
+ end
81
+ accept.should == 'application/json'
82
+ end
83
+
84
+ it 'should convert the body properly' do
85
+ @body.each do |chunk|
86
+ chunk.should be_kind_of(String)
87
+ end
88
+ end
89
+ end
90
+
91
+ describe HTTPAdapter::NetHTTPRequestAdapter,
92
+ 'converting from Net::HTTP::Get with :uri option set' do
93
+ before do
94
+ @request = Net::HTTP::Get.new('/path/to/resource')
95
+ @request['Accept'] = 'application/json'
96
+ @result = HTTPAdapter.adapt_request(
97
+ HTTPAdapter::NetHTTPRequestAdapter.new(
98
+ @request,
99
+ :uri =>
100
+ Addressable::URI.parse('http://www.example.com/path/to/resource')
101
+ )
102
+ )
103
+ @method, @uri, @headers, @body = @result
104
+ end
105
+
106
+ it 'should convert the HTTP method properly' do
107
+ @method.should == 'GET'
108
+ end
109
+
110
+ it 'should convert the URI properly' do
111
+ @uri.should == 'http://www.example.com/path/to/resource'
112
+ end
113
+
114
+ it 'should convert the headers properly' do
115
+ accept = nil
116
+ @headers.each do |header, value|
117
+ header.should be_kind_of(String)
118
+ value.should be_kind_of(String)
119
+ accept = value if header == 'Accept'
120
+ end
121
+ accept.should == 'application/json'
122
+ end
123
+
124
+ it 'should convert the body properly' do
125
+ @body.each do |chunk|
126
+ chunk.should be_kind_of(String)
127
+ end
128
+ end
129
+ end
130
+
131
+ describe HTTPAdapter::NetHTTPRequestAdapter,
132
+ 'converting from Net::HTTP::Get with :uri option set' do
133
+ before do
134
+ @request = Net::HTTP::Get.new('/path/to/resource')
135
+ @request['Accept'] = 'application/json'
136
+ @result = HTTPAdapter.adapt_request(
137
+ HTTPAdapter::NetHTTPRequestAdapter.new(
138
+ @request,
139
+ :uri => 'http://www.example.com/path/to/resource'
140
+ )
141
+ )
142
+ @method, @uri, @headers, @body = @result
143
+ end
144
+
145
+ it 'should convert the HTTP method properly' do
146
+ @method.should == 'GET'
147
+ end
148
+
149
+ it 'should convert the URI properly' do
150
+ @uri.should == 'http://www.example.com/path/to/resource'
151
+ end
152
+
153
+ it 'should convert the headers properly' do
154
+ accept = nil
155
+ @headers.each do |header, value|
156
+ header.should be_kind_of(String)
157
+ value.should be_kind_of(String)
158
+ accept = value if header == 'Accept'
159
+ end
160
+ accept.should == 'application/json'
161
+ end
162
+
163
+ it 'should convert the body properly' do
164
+ @body.each do |chunk|
165
+ chunk.should be_kind_of(String)
166
+ end
167
+ end
168
+ end
169
+
170
+ describe HTTPAdapter::NetHTTPRequestAdapter,
171
+ 'converting from Net::HTTP::Get with :uri option set' do
172
+ before do
173
+ @request = Net::HTTP::Get.new('/path/to/resource')
174
+ @request['Accept'] = 'application/json'
175
+ @result = HTTPAdapter.adapt_request(
176
+ HTTPAdapter::NetHTTPRequestAdapter.new(
177
+ @request,
178
+ :host => 'www.example.com'
179
+ )
180
+ )
181
+ @method, @uri, @headers, @body = @result
182
+ end
183
+
184
+ it 'should convert the HTTP method properly' do
185
+ @method.should == 'GET'
186
+ end
187
+
188
+ it 'should convert the URI properly' do
189
+ @uri.should == 'http://www.example.com/path/to/resource'
190
+ end
191
+
192
+ it 'should convert the headers properly' do
193
+ accept = nil
194
+ @headers.each do |header, value|
195
+ header.should be_kind_of(String)
196
+ value.should be_kind_of(String)
197
+ accept = value if header == 'Accept'
198
+ end
199
+ accept.should == 'application/json'
200
+ end
201
+
202
+ it 'should convert the body properly' do
203
+ @body.each do |chunk|
204
+ chunk.should be_kind_of(String)
205
+ end
206
+ end
207
+ end
208
+
209
+ describe HTTPAdapter::NetHTTPRequestAdapter,
210
+ 'converting from Net::HTTP::Get with :uri option set' do
211
+ before do
212
+ @request = Net::HTTP::Get.new('/path/to/resource')
213
+ @request['Accept'] = 'application/json'
214
+ @result = HTTPAdapter.adapt_request(
215
+ HTTPAdapter::NetHTTPRequestAdapter.new(
216
+ @request,
217
+ :host => 'www.example.com',
218
+ :port => 8080
219
+ )
220
+ )
221
+ @method, @uri, @headers, @body = @result
222
+ end
223
+
224
+ it 'should convert the HTTP method properly' do
225
+ @method.should == 'GET'
226
+ end
227
+
228
+ it 'should convert the URI properly' do
229
+ @uri.should == 'http://www.example.com:8080/path/to/resource'
230
+ end
231
+
232
+ it 'should convert the headers properly' do
233
+ accept = nil
234
+ @headers.each do |header, value|
235
+ header.should be_kind_of(String)
236
+ value.should be_kind_of(String)
237
+ accept = value if header == 'Accept'
238
+ end
239
+ accept.should == 'application/json'
240
+ end
241
+
242
+ it 'should convert the body properly' do
243
+ @body.each do |chunk|
244
+ chunk.should be_kind_of(String)
245
+ end
246
+ end
247
+ end
248
+
249
+ describe HTTPAdapter::NetHTTPRequestAdapter,
250
+ 'converting from Net::HTTP::Get with :uri option set' do
251
+ before do
252
+ @request = Net::HTTP::Get.new('/path/to/resource')
253
+ @request['Accept'] = 'application/json'
254
+ @result = HTTPAdapter.adapt_request(
255
+ HTTPAdapter::NetHTTPRequestAdapter.new(
256
+ @request,
257
+ :scheme => 'https',
258
+ :host => 'www.example.com',
259
+ :port => 443
260
+ )
261
+ )
262
+ @method, @uri, @headers, @body = @result
263
+ end
264
+
265
+ it 'should convert the HTTP method properly' do
266
+ @method.should == 'GET'
267
+ end
268
+
269
+ it 'should convert the URI properly' do
270
+ @uri.should == 'https://www.example.com/path/to/resource'
271
+ end
272
+
273
+ it 'should convert the headers properly' do
274
+ accept = nil
275
+ @headers.each do |header, value|
276
+ header.should be_kind_of(String)
277
+ value.should be_kind_of(String)
278
+ accept = value if header == 'Accept'
279
+ end
280
+ accept.should == 'application/json'
281
+ end
282
+
283
+ it 'should convert the body properly' do
284
+ @body.each do |chunk|
285
+ chunk.should be_kind_of(String)
286
+ end
287
+ end
288
+ end
289
+
290
+ describe HTTPAdapter::NetHTTPRequestAdapter,
291
+ 'converting from a GET tuple' do
292
+ before do
293
+ @tuple = [
294
+ 'GET',
295
+ 'http://www.example.com/path?query',
296
+ [
297
+ ['Accept', 'application/json']
298
+ ],
299
+ []
300
+ ]
301
+ @request = HTTPAdapter.specialize_request(
302
+ @tuple, HTTPAdapter::NetHTTPRequestAdapter
303
+ )
304
+ end
305
+
306
+ it 'should convert the HTTP method properly' do
307
+ @request.method.should == 'GET'
308
+ end
309
+
310
+ it 'should convert the URI properly' do
311
+ @request.path.should == '/path?query'
312
+ end
313
+
314
+ it 'should convert the headers properly' do
315
+ accept = nil
316
+ @request.canonical_each do |header, value|
317
+ header.should be_kind_of(String)
318
+ value.should be_kind_of(String)
319
+ accept = value if header == 'Accept'
320
+ end
321
+ accept.should == 'application/json'
322
+ end
323
+
324
+ it 'should convert the body properly' do
325
+ @request.body.should == ''
326
+ end
327
+ end
328
+
329
+ describe HTTPAdapter::NetHTTPRequestAdapter,
330
+ 'converting from Net::HTTP::Post' do
331
+ before do
332
+ @request = Net::HTTP::Post.new('/path/to/resource')
333
+ @request['Accept'] = 'application/json'
334
+ @request['Content-Type'] = 'application/json; charset=utf-8'
335
+ @request.body = '{"three":3,"two":2,"one":1}'
336
+ @result = HTTPAdapter.adapt_request(
337
+ @request, HTTPAdapter::NetHTTPRequestAdapter
338
+ )
339
+ @method, @uri, @headers, @body = @result
340
+ end
341
+
342
+ it 'should convert the HTTP method properly' do
343
+ @method.should == 'POST'
344
+ end
345
+
346
+ it 'should convert the URI properly' do
347
+ @uri.should == '/path/to/resource'
348
+ end
349
+
350
+ it 'should convert the headers properly' do
351
+ accept = nil
352
+ content_type = nil
353
+ @headers.each do |header, value|
354
+ header.should be_kind_of(String)
355
+ value.should be_kind_of(String)
356
+ accept = value if header == 'Accept'
357
+ content_type = value if header == 'Content-Type'
358
+ end
359
+ accept.should == 'application/json'
360
+ content_type.should == 'application/json; charset=utf-8'
361
+ end
362
+
363
+ it 'should convert the body properly' do
364
+ merged_body = ""
365
+ @body.each do |chunk|
366
+ chunk.should be_kind_of(String)
367
+ merged_body += chunk
368
+ end
369
+ merged_body.should == '{"three":3,"two":2,"one":1}'
370
+ end
371
+ end
372
+
373
+ describe HTTPAdapter::NetHTTPRequestAdapter,
374
+ 'converting from a POST tuple' do
375
+ before do
376
+ @tuple = [
377
+ 'POST',
378
+ 'http://www.example.com/path?query',
379
+ [
380
+ ['Accept', 'application/json'],
381
+ ['Content-Type', 'application/json; charset=utf-8'],
382
+ ['Content-Length', '27']
383
+ ],
384
+ ['{"three":3,', '"two":2,', '"one":1}']
385
+ ]
386
+ @request = HTTPAdapter.specialize_request(
387
+ @tuple, HTTPAdapter::NetHTTPRequestAdapter
388
+ )
389
+ end
390
+
391
+ it 'should convert the HTTP method properly' do
392
+ @request.method.should == 'POST'
393
+ end
394
+
395
+ it 'should convert the URI properly' do
396
+ @request.path.should == '/path?query'
397
+ end
398
+
399
+ it 'should convert the headers properly' do
400
+ accept = nil
401
+ content_type = nil
402
+ @request.canonical_each do |header, value|
403
+ header.should be_kind_of(String)
404
+ value.should be_kind_of(String)
405
+ accept = value if header == 'Accept'
406
+ content_type = value if header == 'Content-Type'
407
+ end
408
+ accept.should == 'application/json'
409
+ content_type.should == 'application/json; charset=utf-8'
410
+ end
411
+
412
+ it 'should convert the body properly' do
413
+ @request.body.should == '{"three":3,"two":2,"one":1}'
414
+ end
415
+ end
@@ -0,0 +1,170 @@
1
+ # Copyright (C) 2010 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'spec_helper'
16
+
17
+ require 'httpadapter/adapters/net_http'
18
+
19
+ describe HTTPAdapter::NetHTTPResponseAdapter do
20
+ it 'should raise an error for converting from an invalid tuple' do
21
+ (lambda do
22
+ HTTPAdapter.specialize_response(
23
+ [200, [], [42]], HTTPAdapter::NetHTTPResponseAdapter
24
+ )
25
+ end).should raise_error(TypeError)
26
+ end
27
+
28
+ it 'should raise an error for converting from an invalid response' do
29
+ (lambda do
30
+ HTTPAdapter.adapt_response(
31
+ 42, HTTPAdapter::NetHTTPResponseAdapter
32
+ )
33
+ end).should raise_error(TypeError)
34
+ end
35
+
36
+ it 'should raise an error for the bogus HTTP status code' do
37
+ (lambda do
38
+ tuple = [
39
+ 'BOGUS',
40
+ [
41
+ ['Content-Type', 'application/json; charset=utf-8'],
42
+ ['Content-Length', '27']
43
+ ],
44
+ ['{"three":3,', '"two":2,', '"one":1}']
45
+ ]
46
+ HTTPAdapter.specialize_response(
47
+ tuple, HTTPAdapter::NetHTTPResponseAdapter
48
+ )
49
+ end).should raise_error(ArgumentError)
50
+ end
51
+ end
52
+
53
+ describe HTTPAdapter::NetHTTPResponseAdapter,
54
+ 'converting from Net::HTTPOK' do
55
+ before do
56
+ @response = Net::HTTPOK.new('1.1', '200', 'OK')
57
+ @response['Content-Type'] = 'application/json; charset=utf-8'
58
+ @response['Content-Length'] = '27'
59
+
60
+ # Ugh
61
+ @response.instance_variable_set('@read', true)
62
+ @response.instance_variable_set('@body', '{"three":3,"two":2,"one":1}')
63
+
64
+ @result = HTTPAdapter.adapt_response(
65
+ @response, HTTPAdapter::NetHTTPResponseAdapter
66
+ )
67
+ @status, @headers, @body = @result
68
+ end
69
+
70
+ it 'should convert the HTTP status properly' do
71
+ @status.should == 200
72
+ end
73
+
74
+ it 'should convert the headers properly' do
75
+ content_type = nil
76
+ @headers.each do |header, value|
77
+ header.should be_kind_of(String)
78
+ value.should be_kind_of(String)
79
+ content_type = value if header == 'Content-Type'
80
+ end
81
+ content_type.should == 'application/json; charset=utf-8'
82
+ end
83
+
84
+ it 'should convert the body properly' do
85
+ merged_body = ""
86
+ @body.each do |chunk|
87
+ merged_body += chunk
88
+ chunk.should be_kind_of(String)
89
+ end
90
+ merged_body.should == '{"three":3,"two":2,"one":1}'
91
+ end
92
+ end
93
+
94
+ describe HTTPAdapter::NetHTTPResponseAdapter,
95
+ 'converting from a 200 tuple' do
96
+ before do
97
+ @tuple = [
98
+ 200,
99
+ [
100
+ ['Content-Type', 'application/json; charset=utf-8'],
101
+ ['Content-Length', '27']
102
+ ],
103
+ ['{"three":3,"two":2,"one":1}']
104
+ ]
105
+ @response = HTTPAdapter.specialize_response(
106
+ @tuple, HTTPAdapter::NetHTTPResponseAdapter
107
+ )
108
+ end
109
+
110
+ it 'should be the correct type' do
111
+ @response.should be_kind_of(Net::HTTPOK)
112
+ end
113
+
114
+ it 'should convert the HTTP status properly' do
115
+ @response.code.to_i.should == 200
116
+ end
117
+
118
+ it 'should convert the headers properly' do
119
+ content_type = nil
120
+ @response.canonical_each do |header, value|
121
+ header.should be_kind_of(String)
122
+ value.should be_kind_of(String)
123
+ content_type = value if header == 'Content-Type'
124
+ end
125
+ content_type.should == 'application/json; charset=utf-8'
126
+ end
127
+
128
+ it 'should convert the body properly' do
129
+ @response.body.should == '{"three":3,"two":2,"one":1}'
130
+ end
131
+ end
132
+
133
+ describe HTTPAdapter::NetHTTPResponseAdapter,
134
+ 'converting from a 200 tuple' do
135
+ before do
136
+ @tuple = [
137
+ 500,
138
+ [
139
+ ['Content-Type', 'text/html'],
140
+ ['Content-Length', '28']
141
+ ],
142
+ ['<html><body>', '42', '</body></html>']
143
+ ]
144
+ @response = HTTPAdapter.specialize_response(
145
+ @tuple, HTTPAdapter::NetHTTPResponseAdapter
146
+ )
147
+ end
148
+
149
+ it 'should be the correct type' do
150
+ @response.should be_kind_of(Net::HTTPInternalServerError)
151
+ end
152
+
153
+ it 'should convert the HTTP status properly' do
154
+ @response.code.to_i.should == 500
155
+ end
156
+
157
+ it 'should convert the headers properly' do
158
+ content_type = nil
159
+ @response.canonical_each do |header, value|
160
+ header.should be_kind_of(String)
161
+ value.should be_kind_of(String)
162
+ content_type = value if header == 'Content-Type'
163
+ end
164
+ content_type.should == 'text/html'
165
+ end
166
+
167
+ it 'should convert the body properly' do
168
+ @response.body.should == '<html><body>42</body></html>'
169
+ end
170
+ end