httpadapter 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,228 @@
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/rack'
18
+
19
+ describe HTTPAdapter::RackRequestAdapter 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::RackRequestAdapter
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::RackRequestAdapter
32
+ )
33
+ end).should raise_error(TypeError)
34
+ end
35
+ end
36
+
37
+ describe HTTPAdapter::RackRequestAdapter,
38
+ 'converting from Rack::Request for GET' do
39
+ before do
40
+ @body_io = StringIO.new
41
+ @env = {
42
+ # PEP333 variables
43
+ 'REQUEST_METHOD' => 'GET',
44
+ 'SERVER_NAME' => 'www.example.com',
45
+ 'SERVER_PORT' => '80',
46
+ 'SCRIPT_NAME' => '',
47
+ 'PATH_INFO' => '/path/to/resource',
48
+ 'QUERY_STRING' => '',
49
+
50
+ # Rack-specific variables
51
+ 'rack.version' => Rack::VERSION,
52
+ 'rack.input' => @body_io,
53
+ 'rack.errors' => STDERR,
54
+ 'rack.multithread' => true,
55
+ 'rack.multiprocess' => true,
56
+ 'rack.run_once' => false,
57
+ 'rack.url_scheme' => 'http',
58
+
59
+ # HTTP headers
60
+ 'HTTP_ACCEPT' => 'application/json'
61
+ }
62
+ @request = Rack::Request.new(@env)
63
+ @result = HTTPAdapter.adapt_request(
64
+ @request, HTTPAdapter::RackRequestAdapter
65
+ )
66
+ @method, @uri, @headers, @body = @result
67
+ end
68
+
69
+ it 'should convert the HTTP method properly' do
70
+ @method.should == 'GET'
71
+ end
72
+
73
+ it 'should convert the URI properly' do
74
+ @uri.should == 'http://www.example.com/path/to/resource'
75
+ end
76
+
77
+ it 'should convert the headers properly' do
78
+ accept = nil
79
+ @headers.each do |header, value|
80
+ header.should be_kind_of(String)
81
+ value.should be_kind_of(String)
82
+ accept = value if header == 'Accept'
83
+ end
84
+ accept.should == 'application/json'
85
+ end
86
+
87
+ it 'should convert the body properly' do
88
+ @body.each do |chunk|
89
+ chunk.should be_kind_of(String)
90
+ end
91
+ end
92
+ end
93
+
94
+ describe HTTPAdapter::RackRequestAdapter,
95
+ 'converting from a GET tuple' do
96
+ before do
97
+ @tuple = [
98
+ 'GET',
99
+ 'http://www.example.com/path?query',
100
+ [
101
+ ['Accept', 'application/json']
102
+ ],
103
+ []
104
+ ]
105
+ @request = HTTPAdapter.specialize_request(
106
+ @tuple, HTTPAdapter::RackRequestAdapter
107
+ )
108
+ end
109
+
110
+ it 'should convert the HTTP method properly' do
111
+ @request.request_method.should == 'GET'
112
+ end
113
+
114
+ it 'should convert the URI properly' do
115
+ @request.url.should == 'http://www.example.com/path?query'
116
+ end
117
+
118
+ it 'should convert the headers properly' do
119
+ @request.env['HTTP_ACCEPT'].should == 'application/json'
120
+ end
121
+
122
+ it 'should convert the body properly' do
123
+ @request.body.read.should == ''
124
+ end
125
+ end
126
+
127
+ describe HTTPAdapter::RackRequestAdapter,
128
+ 'converting from Rack::Request for POST' do
129
+ before do
130
+ @body_io = StringIO.new('{"three":3,"two":2,"one":1}')
131
+ @env = {
132
+ # PEP333 variables
133
+ 'REQUEST_METHOD' => 'POST',
134
+ 'SERVER_NAME' => 'www.example.com',
135
+ 'SERVER_PORT' => '80',
136
+ 'SCRIPT_NAME' => '',
137
+ 'PATH_INFO' => '/path/to/resource',
138
+ 'QUERY_STRING' => '',
139
+
140
+ # Rack-specific variables
141
+ 'rack.version' => Rack::VERSION,
142
+ 'rack.input' => @body_io,
143
+ 'rack.errors' => STDERR,
144
+ 'rack.multithread' => true,
145
+ 'rack.multiprocess' => true,
146
+ 'rack.run_once' => false,
147
+ 'rack.url_scheme' => 'http',
148
+
149
+ # HTTP headers
150
+ 'HTTP_ACCEPT' => 'application/json',
151
+ 'HTTP_CONTENT_TYPE' => 'application/json; charset=utf-8'
152
+ }
153
+ @request = Rack::Request.new(@env)
154
+ @result = HTTPAdapter.adapt_request(
155
+ @request, HTTPAdapter::RackRequestAdapter
156
+ )
157
+ @method, @uri, @headers, @body = @result
158
+ end
159
+
160
+ it 'should convert the HTTP method properly' do
161
+ @method.should == 'POST'
162
+ end
163
+
164
+ it 'should convert the URI properly' do
165
+ @uri.should == 'http://www.example.com/path/to/resource'
166
+ end
167
+
168
+ it 'should convert the headers properly' do
169
+ accept = nil
170
+ content_type = nil
171
+ @headers.each do |header, value|
172
+ header.should be_kind_of(String)
173
+ value.should be_kind_of(String)
174
+ accept = value if header == 'Accept'
175
+ content_type = value if header == 'Content-Type'
176
+ end
177
+ accept.should == 'application/json'
178
+ content_type.should == 'application/json; charset=utf-8'
179
+ end
180
+
181
+ it 'should convert the body properly' do
182
+ merged_body = ""
183
+ @body.each do |chunk|
184
+ chunk.should be_kind_of(String)
185
+ merged_body += chunk
186
+ end
187
+ merged_body.should == '{"three":3,"two":2,"one":1}'
188
+ end
189
+ end
190
+
191
+ describe HTTPAdapter::RackRequestAdapter,
192
+ 'converting from a POST tuple' do
193
+ before do
194
+ @tuple = [
195
+ 'POST',
196
+ 'http://www.example.com/path?query',
197
+ [
198
+ ['Accept', 'application/json'],
199
+ ['Content-Type', 'application/json; charset=utf-8'],
200
+ ['Content-Length', '27']
201
+ ],
202
+ ['{"three":3,', '"two":2,', '"one":1}']
203
+ ]
204
+ @request = HTTPAdapter.specialize_request(
205
+ @tuple, HTTPAdapter::RackRequestAdapter
206
+ )
207
+ end
208
+
209
+ it 'should convert the HTTP method properly' do
210
+ @request.request_method.should == 'POST'
211
+ end
212
+
213
+ it 'should convert the URI properly' do
214
+ @request.url.should == 'http://www.example.com/path?query'
215
+ end
216
+
217
+ it 'should convert the headers properly' do
218
+ accept = nil
219
+ content_type = nil
220
+ @request.env['HTTP_ACCEPT'].should == 'application/json'
221
+ @request.env['HTTP_CONTENT_TYPE'].should ==
222
+ 'application/json; charset=utf-8'
223
+ end
224
+
225
+ it 'should convert the body properly' do
226
+ @request.body.read.should == '{"three":3,"two":2,"one":1}'
227
+ end
228
+ end
@@ -0,0 +1,158 @@
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/rack'
18
+
19
+ describe HTTPAdapter::RackResponseAdapter 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::RackResponseAdapter
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::RackResponseAdapter
32
+ )
33
+ end).should raise_error(TypeError)
34
+ end
35
+ end
36
+
37
+ describe HTTPAdapter::RackResponseAdapter,
38
+ 'converting from Typhoeus::Response' do
39
+ before do
40
+ @response = Rack::Response.new(
41
+ ['{"three":3,"two":2,"one":1}'],
42
+ 200,
43
+ {
44
+ 'Content-Type' => 'application/json; charset=utf-8',
45
+ 'Content-Length' => '27'
46
+ }
47
+ )
48
+ @result = HTTPAdapter.adapt_response(
49
+ @response, HTTPAdapter::RackResponseAdapter
50
+ )
51
+ @status, @headers, @body = @result
52
+ end
53
+
54
+ it 'should convert the HTTP status properly' do
55
+ @status.should == 200
56
+ end
57
+
58
+ it 'should convert the headers properly' do
59
+ content_type = nil
60
+ @headers.each do |header, value|
61
+ header.should be_kind_of(String)
62
+ value.should be_kind_of(String)
63
+ content_type = value if header == 'Content-Type'
64
+ end
65
+ content_type.should == 'application/json; charset=utf-8'
66
+ end
67
+
68
+ it 'should convert the body properly' do
69
+ merged_body = ""
70
+ @body.each do |chunk|
71
+ merged_body += chunk
72
+ chunk.should be_kind_of(String)
73
+ end
74
+ merged_body.should == '{"three":3,"two":2,"one":1}'
75
+ end
76
+ end
77
+
78
+ describe HTTPAdapter::RackResponseAdapter,
79
+ 'converting from a 200 tuple' do
80
+ before do
81
+ @tuple = [
82
+ 200,
83
+ [
84
+ ['Content-Type', 'application/json; charset=utf-8'],
85
+ ['Content-Length', '27']
86
+ ],
87
+ ['{"three":3,"two":2,"one":1}']
88
+ ]
89
+ @response = HTTPAdapter.specialize_response(
90
+ @tuple, HTTPAdapter::RackResponseAdapter
91
+ )
92
+ end
93
+
94
+ it 'should convert the HTTP status properly' do
95
+ @response.status.to_i.should == 200
96
+ end
97
+
98
+ it 'should parse the content length properly' do
99
+ @response.length.to_i.should == 27
100
+ end
101
+
102
+ it 'should convert the headers properly' do
103
+ content_type = nil
104
+ @response.header.each do |header, value|
105
+ header.should be_kind_of(String)
106
+ value.should be_kind_of(String)
107
+ content_type = value if header == 'Content-Type'
108
+ end
109
+ content_type.should == 'application/json; charset=utf-8'
110
+ end
111
+
112
+ it 'should convert the body properly' do
113
+ merged_body = ""
114
+ @response.body.each do |chunk|
115
+ merged_body += chunk
116
+ end
117
+ merged_body.should == '{"three":3,"two":2,"one":1}'
118
+ end
119
+ end
120
+
121
+ describe HTTPAdapter::RackResponseAdapter,
122
+ 'converting from a 200 tuple' do
123
+ before do
124
+ @tuple = [
125
+ 500,
126
+ [
127
+ ['Content-Type', 'text/html'],
128
+ ['Content-Length', '28']
129
+ ],
130
+ ['<html><body>', '42', '</body></html>']
131
+ ]
132
+ @response = HTTPAdapter.specialize_response(
133
+ @tuple, HTTPAdapter::RackResponseAdapter
134
+ )
135
+ end
136
+
137
+ it 'should convert the HTTP status properly' do
138
+ @response.status.to_i.should == 500
139
+ end
140
+
141
+ it 'should convert the headers properly' do
142
+ content_type = nil
143
+ @response.header.each do |header, value|
144
+ header.should be_kind_of(String)
145
+ value.should be_kind_of(String)
146
+ content_type = value if header == 'Content-Type'
147
+ end
148
+ content_type.should == 'text/html'
149
+ end
150
+
151
+ it 'should convert the body properly' do
152
+ merged_body = ""
153
+ @response.body.each do |chunk|
154
+ merged_body += chunk
155
+ end
156
+ merged_body.should == '<html><body>42</body></html>'
157
+ end
158
+ end
@@ -0,0 +1,203 @@
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/typhoeus'
18
+
19
+ describe HTTPAdapter::TyphoeusRequestAdapter 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::TyphoeusRequestAdapter
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::TyphoeusRequestAdapter
32
+ )
33
+ end).should raise_error(TypeError)
34
+ end
35
+ end
36
+
37
+ describe HTTPAdapter::TyphoeusRequestAdapter,
38
+ 'converting from Typhoeus::Request for GET' do
39
+ before do
40
+ @request = Typhoeus::Request.new(
41
+ 'http://www.example.com/path/to/resource'
42
+ )
43
+ @request.headers['Accept'] = 'application/json'
44
+ @result = HTTPAdapter.adapt_request(
45
+ @request, HTTPAdapter::TyphoeusRequestAdapter
46
+ )
47
+ @method, @uri, @headers, @body = @result
48
+ end
49
+
50
+ it 'should convert the HTTP method properly' do
51
+ @method.should == 'GET'
52
+ end
53
+
54
+ it 'should convert the URI properly' do
55
+ @uri.should == 'http://www.example.com/path/to/resource'
56
+ end
57
+
58
+ it 'should convert the headers properly' do
59
+ accept = nil
60
+ @headers.each do |header, value|
61
+ header.should be_kind_of(String)
62
+ value.should be_kind_of(String)
63
+ accept = value if header == 'Accept'
64
+ end
65
+ accept.should == 'application/json'
66
+ end
67
+
68
+ it 'should convert the body properly' do
69
+ @body.each do |chunk|
70
+ chunk.should be_kind_of(String)
71
+ end
72
+ end
73
+ end
74
+
75
+ describe HTTPAdapter::TyphoeusRequestAdapter,
76
+ 'converting from a GET tuple' do
77
+ before do
78
+ @tuple = [
79
+ 'GET',
80
+ 'http://www.example.com/path?query',
81
+ [
82
+ ['Accept', 'application/json']
83
+ ],
84
+ []
85
+ ]
86
+ @request = HTTPAdapter.specialize_request(
87
+ @tuple, HTTPAdapter::TyphoeusRequestAdapter
88
+ )
89
+ end
90
+
91
+ it 'should convert the HTTP method properly' do
92
+ @request.method.should == :get
93
+ end
94
+
95
+ it 'should convert the URI properly' do
96
+ @request.url.should == 'http://www.example.com/path?query'
97
+ end
98
+
99
+ it 'should convert the headers properly' do
100
+ accept = nil
101
+ @request.headers.each do |header, value|
102
+ header.should be_kind_of(String)
103
+ value.should be_kind_of(String)
104
+ accept = value if header == 'Accept'
105
+ end
106
+ accept.should == 'application/json'
107
+ end
108
+
109
+ it 'should convert the body properly' do
110
+ @request.body.should == ''
111
+ end
112
+ end
113
+
114
+ describe HTTPAdapter::TyphoeusRequestAdapter,
115
+ 'converting from Typhoeus::Request for POST' do
116
+ before do
117
+ @request = Typhoeus::Request.new(
118
+ 'http://www.example.com/path/to/resource',
119
+ :method => :post
120
+ )
121
+ @request.headers['Accept'] = 'application/json'
122
+ @request.headers['Content-Type'] = 'application/json; charset=utf-8'
123
+ @request.body = '{"three":3,"two":2,"one":1}'
124
+ @result = HTTPAdapter.adapt_request(
125
+ @request, HTTPAdapter::TyphoeusRequestAdapter
126
+ )
127
+ @method, @uri, @headers, @body = @result
128
+ end
129
+
130
+ it 'should convert the HTTP method properly' do
131
+ @method.should == 'POST'
132
+ end
133
+
134
+ it 'should convert the URI properly' do
135
+ @uri.should == 'http://www.example.com/path/to/resource'
136
+ end
137
+
138
+ it 'should convert the headers properly' do
139
+ accept = nil
140
+ content_type = nil
141
+ @headers.each do |header, value|
142
+ header.should be_kind_of(String)
143
+ value.should be_kind_of(String)
144
+ accept = value if header == 'Accept'
145
+ content_type = value if header == 'Content-Type'
146
+ end
147
+ accept.should == 'application/json'
148
+ content_type.should == 'application/json; charset=utf-8'
149
+ end
150
+
151
+ it 'should convert the body properly' do
152
+ merged_body = ""
153
+ @body.each do |chunk|
154
+ chunk.should be_kind_of(String)
155
+ merged_body += chunk
156
+ end
157
+ merged_body.should == '{"three":3,"two":2,"one":1}'
158
+ end
159
+ end
160
+
161
+ describe HTTPAdapter::TyphoeusRequestAdapter,
162
+ 'converting from a POST tuple' do
163
+ before do
164
+ @tuple = [
165
+ 'POST',
166
+ 'http://www.example.com/path?query',
167
+ [
168
+ ['Accept', 'application/json'],
169
+ ['Content-Type', 'application/json; charset=utf-8'],
170
+ ['Content-Length', '27']
171
+ ],
172
+ ['{"three":3,', '"two":2,', '"one":1}']
173
+ ]
174
+ @request = HTTPAdapter.specialize_request(
175
+ @tuple, HTTPAdapter::TyphoeusRequestAdapter
176
+ )
177
+ end
178
+
179
+ it 'should convert the HTTP method properly' do
180
+ @request.method.should == :post
181
+ end
182
+
183
+ it 'should convert the URI properly' do
184
+ @request.url.should == 'http://www.example.com/path?query'
185
+ end
186
+
187
+ it 'should convert the headers properly' do
188
+ accept = nil
189
+ content_type = nil
190
+ @request.headers.each do |header, value|
191
+ header.should be_kind_of(String)
192
+ value.should be_kind_of(String)
193
+ accept = value if header == 'Accept'
194
+ content_type = value if header == 'Content-Type'
195
+ end
196
+ accept.should == 'application/json'
197
+ content_type.should == 'application/json; charset=utf-8'
198
+ end
199
+
200
+ it 'should convert the body properly' do
201
+ @request.body.should == '{"three":3,"two":2,"one":1}'
202
+ end
203
+ end