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,203 +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/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 == nil
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
@@ -1,146 +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/typhoeus'
18
-
19
- describe HTTPAdapter::TyphoeusResponseAdapter 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::TyphoeusResponseAdapter
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::TyphoeusResponseAdapter
32
- )
33
- end).should raise_error(TypeError)
34
- end
35
- end
36
-
37
- describe HTTPAdapter::TyphoeusResponseAdapter,
38
- 'converting from Typhoeus::Response' do
39
- before do
40
- @response = Typhoeus::Response.new(
41
- :code => 200,
42
- :headers => <<-HEADERS,
43
- Content-Type: application/json; charset=utf-8
44
- Content-Length: 27
45
- HEADERS
46
- :body => '{"three":3,"two":2,"one":1}'
47
- )
48
- @result = HTTPAdapter.adapt_response(
49
- @response, HTTPAdapter::TyphoeusResponseAdapter
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::TyphoeusResponseAdapter,
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::TyphoeusResponseAdapter
91
- )
92
- end
93
-
94
- it 'should convert the HTTP status properly' do
95
- @response.code.to_i.should == 200
96
- end
97
-
98
- it 'should convert the headers properly' do
99
- content_type = nil
100
- @response.headers_hash.each do |header, value|
101
- header.should be_kind_of(String)
102
- value.should be_kind_of(String)
103
- content_type = value if header == 'Content-Type'
104
- end
105
- content_type.should == 'application/json; charset=utf-8'
106
- end
107
-
108
- it 'should convert the body properly' do
109
- @response.body.should == '{"three":3,"two":2,"one":1}'
110
- end
111
- end
112
-
113
- describe HTTPAdapter::TyphoeusResponseAdapter,
114
- 'converting from a 200 tuple' do
115
- before do
116
- @tuple = [
117
- 500,
118
- [
119
- ['Content-Type', 'text/html'],
120
- ['Content-Length', '28']
121
- ],
122
- ['<html><body>', '42', '</body></html>']
123
- ]
124
- @response = HTTPAdapter.specialize_response(
125
- @tuple, HTTPAdapter::TyphoeusResponseAdapter
126
- )
127
- end
128
-
129
- it 'should convert the HTTP status properly' do
130
- @response.code.to_i.should == 500
131
- end
132
-
133
- it 'should convert the headers properly' do
134
- content_type = nil
135
- @response.headers_hash.each do |header, value|
136
- header.should be_kind_of(String)
137
- value.should be_kind_of(String)
138
- content_type = value if header == 'Content-Type'
139
- end
140
- content_type.should == 'text/html'
141
- end
142
-
143
- it 'should convert the body properly' do
144
- @response.body.should == '<html><body>42</body></html>'
145
- end
146
- end
@@ -1,89 +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/typhoeus'
18
-
19
- describe HTTPAdapter::TyphoeusRequestAdapter, 'transmitting a GET tuple' do
20
- before do
21
- @tuple = [
22
- 'GET',
23
- 'http://www.google.com/',
24
- [],
25
- []
26
- ]
27
- @response = HTTPAdapter.transmit(
28
- @tuple, HTTPAdapter::TyphoeusRequestAdapter
29
- )
30
- @status, @headers, @chunked_body = @response
31
- @body = ''
32
- @chunked_body.each do |chunk|
33
- @body += chunk
34
- end
35
- end
36
-
37
- it 'should have the correct status' do
38
- @status.should == 200
39
- end
40
-
41
- it 'should have response headers' do
42
- @headers.should_not be_empty
43
- end
44
-
45
- it 'should have a response body' do
46
- @body.length.should > 0
47
- end
48
- end
49
-
50
- describe HTTPAdapter::TyphoeusRequestAdapter, 'transmitting with connection' do
51
- before do
52
- @connection = HTTPAdapter::Connection.new(
53
- 'www.google.com', 80,
54
- Typhoeus::Hydra.new,
55
- :join => [:run, [], nil]
56
- )
57
- @connection.open
58
- @tuple = [
59
- 'GET',
60
- 'http://www.google.com/',
61
- [],
62
- []
63
- ]
64
- @response = HTTPAdapter.transmit(
65
- @tuple, HTTPAdapter::TyphoeusRequestAdapter, @connection
66
- )
67
- @status, @headers, @chunked_body = @response
68
- @body = ''
69
- @chunked_body.each do |chunk|
70
- @body += chunk
71
- end
72
- end
73
-
74
- after do
75
- @connection.close
76
- end
77
-
78
- it 'should have the correct status' do
79
- @status.should == 200
80
- end
81
-
82
- it 'should have response headers' do
83
- @headers.should_not be_empty
84
- end
85
-
86
- it 'should have a response body' do
87
- @body.length.should > 0
88
- end
89
- end