httpadapter 0.2.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,130 +0,0 @@
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
- if ([:enable_post_connection_check=, 'enable_post_connection_check='] &
20
- Net::HTTP.instance_methods).empty?
21
- class Net::HTTP
22
- def enable_post_connection_check=(value)
23
- # No-op
24
- end
25
- end
26
- end
27
-
28
- describe HTTPAdapter::NetHTTPRequestAdapter, 'transmitting a GET tuple' do
29
- before do
30
- @tuple = [
31
- 'GET',
32
- 'http://www.google.com/',
33
- [],
34
- []
35
- ]
36
- @response = HTTPAdapter.transmit(
37
- @tuple, HTTPAdapter::NetHTTPRequestAdapter
38
- )
39
- @status, @headers, @chunked_body = @response
40
- @body = ''
41
- @chunked_body.each do |chunk|
42
- @body += chunk
43
- end
44
- end
45
-
46
- it 'should have the correct status' do
47
- @status.should == 200
48
- end
49
-
50
- it 'should have response headers' do
51
- @headers.should_not be_empty
52
- end
53
-
54
- it 'should have a response body' do
55
- @body.length.should > 0
56
- end
57
- end
58
-
59
- describe HTTPAdapter::NetHTTPRequestAdapter, 'transmitting a GET tuple' do
60
- before do
61
- @tuple = [
62
- 'GET',
63
- 'https://encrypted.google.com/',
64
- [],
65
- []
66
- ]
67
- @response = HTTPAdapter.transmit(
68
- @tuple, HTTPAdapter::NetHTTPRequestAdapter
69
- )
70
- @status, @headers, @chunked_body = @response
71
- @body = ''
72
- @chunked_body.each do |chunk|
73
- @body += chunk
74
- end
75
- end
76
-
77
- it 'should have the correct status' do
78
- @status.should == 200
79
- end
80
-
81
- it 'should have response headers' do
82
- @headers.should_not be_empty
83
- end
84
-
85
- it 'should have a response body' do
86
- @body.length.should > 0
87
- end
88
- end
89
-
90
- describe HTTPAdapter::NetHTTPRequestAdapter, 'transmitting with connection' do
91
- before do
92
- @connection = HTTPAdapter::Connection.new(
93
- 'www.google.com', 80,
94
- Net::HTTP.new('www.google.com', 80),
95
- :open => [:start],
96
- :close => [:finish]
97
- )
98
- @connection.open
99
- @tuple = [
100
- 'GET',
101
- 'http://www.google.com/',
102
- [],
103
- []
104
- ]
105
- @response = HTTPAdapter.transmit(
106
- @tuple, HTTPAdapter::NetHTTPRequestAdapter, @connection
107
- )
108
- @status, @headers, @chunked_body = @response
109
- @body = ''
110
- @chunked_body.each do |chunk|
111
- @body += chunk
112
- end
113
- end
114
-
115
- after do
116
- @connection.close
117
- end
118
-
119
- it 'should have the correct status' do
120
- @status.should == 200
121
- end
122
-
123
- it 'should have response headers' do
124
- @headers.should_not be_empty
125
- end
126
-
127
- it 'should have a response body' do
128
- @body.length.should > 0
129
- end
130
- end
@@ -1,239 +0,0 @@
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
229
-
230
- describe HTTPAdapter::RackRequestAdapter, 'transmitting' do
231
- it 'should raise an error indicating that transmission is not possible' do
232
- (lambda do
233
- HTTPAdapter.transmit(
234
- ['GET', 'http://www.google.com/', [], ['']],
235
- HTTPAdapter::RackRequestAdapter
236
- )
237
- end).should raise_error(NotImplementedError)
238
- end
239
- end
@@ -1,158 +0,0 @@
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