celluloid-http 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +12 -0
- data/README.md +11 -5
- data/Rakefile +10 -0
- data/celluloid-http.gemspec +1 -0
- data/lib/celluloid-http/body_decoder.rb +31 -0
- data/lib/celluloid-http/builder.rb +19 -0
- data/lib/celluloid-http/connection.rb +2 -1
- data/lib/celluloid-http/http.rb +6 -2
- data/lib/celluloid-http/request.rb +14 -4
- data/lib/celluloid-http/response.rb +15 -4
- data/lib/celluloid-http/version.rb +1 -1
- data/lib/celluloid-http.rb +2 -0
- data/test/fixtures/gzip.wikipedia.org +0 -0
- data/test/fixtures/post.example.com +8 -0
- data/test/unit/builder_test.rb +27 -0
- data/test/unit/connection_test.rb +12 -0
- data/test/unit/http_test.rb +18 -0
- data/test/unit/request_test.rb +30 -0
- metadata +35 -2
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Celluloid::Http
|
2
2
|
|
3
|
-
|
3
|
+
[](https://travis-ci.org/NOX73/celluloid-http)
|
4
|
+
|
5
|
+
Http request implementation based on celluloid::io.
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -18,12 +20,16 @@ Or install it yourself as:
|
|
18
20
|
|
19
21
|
## Usage
|
20
22
|
|
21
|
-
|
23
|
+
Celluloid::Http.get('http://en.wikipedia.org/wiki/Http')
|
24
|
+
|
25
|
+
See test/unit/http_test.rb for more examples.
|
22
26
|
|
23
27
|
## Contributing
|
24
28
|
|
25
29
|
1. Fork it
|
26
30
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
-
3.
|
28
|
-
4.
|
29
|
-
5.
|
31
|
+
3. Write code
|
32
|
+
4. Run test (`bundle exec rake test`)
|
33
|
+
5. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
6. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
7. Create new Pull Request
|
data/Rakefile
CHANGED
data/celluloid-http.gemspec
CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |gem|
|
|
22
22
|
gem.add_runtime_dependency('http_parser.rb')
|
23
23
|
gem.add_runtime_dependency('activesupport')
|
24
24
|
|
25
|
+
gem.add_development_dependency('rake')
|
25
26
|
gem.add_development_dependency('turn')
|
26
27
|
gem.add_development_dependency('minitest')
|
27
28
|
gem.add_development_dependency('mocha')
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'zlib'
|
2
|
+
|
3
|
+
class Celluloid::Http::BodyDecoder
|
4
|
+
|
5
|
+
class<<self
|
6
|
+
|
7
|
+
def decode(headers, raw_body)
|
8
|
+
new(headers, raw_body).decode.body
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
attr_reader :headers, :body
|
14
|
+
def initialize(headers, raw_body)
|
15
|
+
@headers, @body = headers, raw_body
|
16
|
+
end
|
17
|
+
|
18
|
+
def decode
|
19
|
+
@body = gunzip(@body) if zip?
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def gunzip(body)
|
24
|
+
Zlib::GzipReader.new(StringIO.new(body)).read
|
25
|
+
end
|
26
|
+
|
27
|
+
def zip?
|
28
|
+
@headers['Content-Encoding'] == 'gzip'
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Celluloid::Http::Builder
|
2
|
+
|
3
|
+
class<<self
|
4
|
+
|
5
|
+
def get(url)
|
6
|
+
Celluloid::Http::Request.new url
|
7
|
+
end
|
8
|
+
|
9
|
+
def post(url, params)
|
10
|
+
options = {
|
11
|
+
form_data: params,
|
12
|
+
method: :post
|
13
|
+
}
|
14
|
+
Celluloid::Http::Request.new url, options
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -8,6 +8,7 @@ class Celluloid::Http::Connection
|
|
8
8
|
|
9
9
|
def close
|
10
10
|
socket.close
|
11
|
+
terminate
|
11
12
|
end
|
12
13
|
|
13
14
|
def send_request(request)
|
@@ -32,7 +33,7 @@ class Celluloid::Http::Connection
|
|
32
33
|
|
33
34
|
def self.open(host, port = 80)
|
34
35
|
connection = self.new
|
35
|
-
connection.open(host,
|
36
|
+
connection.open(host, port)
|
36
37
|
connection
|
37
38
|
end
|
38
39
|
|
data/lib/celluloid-http/http.rb
CHANGED
@@ -9,8 +9,12 @@ module Celluloid::Http::Http
|
|
9
9
|
response
|
10
10
|
end
|
11
11
|
|
12
|
-
def get(
|
13
|
-
send_request Celluloid::Http::
|
12
|
+
def get(*args)
|
13
|
+
send_request Celluloid::Http::Builder.get(*args)
|
14
|
+
end
|
15
|
+
|
16
|
+
def post(*args)
|
17
|
+
send_request Celluloid::Http::Builder.post(*args)
|
14
18
|
end
|
15
19
|
|
16
20
|
end
|
@@ -5,16 +5,16 @@ class Celluloid::Http::Request
|
|
5
5
|
DEFAULT_METHOD = :get
|
6
6
|
DEFAULT_HTTP_VERSION = '1.1'
|
7
7
|
|
8
|
-
attr_accessor :method, :body
|
8
|
+
attr_accessor :method, :body, :form_data
|
9
9
|
|
10
10
|
def_delegators :@uri, :scheme, :host, :path, :port, :query
|
11
11
|
def_delegators :@uri, :scheme=, :host=, :path=, :port=
|
12
12
|
|
13
13
|
def initialize(url, options = {})
|
14
|
-
@url = url
|
15
14
|
@uri = URI.parse url
|
16
15
|
@method = options[:method] || DEFAULT_METHOD
|
17
|
-
@
|
16
|
+
@raw_body = options[:raw_body]
|
17
|
+
@form_data = options[:form_data]
|
18
18
|
|
19
19
|
merge_query_params(options[:query_params]) if options[:query_params]
|
20
20
|
end
|
@@ -24,13 +24,17 @@ class Celluloid::Http::Request
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def to_s
|
27
|
-
"#{method.to_s.upcase} #{
|
27
|
+
"#{method.to_s.upcase} #{uri} HTTP/#{DEFAULT_HTTP_VERSION}\nHost: #{host}\n\n#{body}"
|
28
28
|
end
|
29
29
|
|
30
30
|
def url
|
31
31
|
@uri.to_s
|
32
32
|
end
|
33
33
|
|
34
|
+
def uri
|
35
|
+
"#{ "/" if path.length.zero? }#{path}#{ "?" if query }#{query}"
|
36
|
+
end
|
37
|
+
|
34
38
|
def query=(val)
|
35
39
|
@uri.query = val.is_a?(Hash) ? val.to_query : val
|
36
40
|
end
|
@@ -40,4 +44,10 @@ class Celluloid::Http::Request
|
|
40
44
|
self.query = params
|
41
45
|
end
|
42
46
|
|
47
|
+
def body
|
48
|
+
@body = @raw_body if @raw_body
|
49
|
+
@body = @form_data.to_query if @form_data
|
50
|
+
@body
|
51
|
+
end
|
52
|
+
|
43
53
|
end
|
@@ -6,11 +6,12 @@ class Celluloid::Http::Response
|
|
6
6
|
def_delegators :parser, :<<
|
7
7
|
|
8
8
|
attr_writer :status
|
9
|
-
attr_accessor :headers, :
|
9
|
+
attr_accessor :headers, :raw_body
|
10
10
|
|
11
|
-
def initialize(status = nil, headers = {}, body =
|
11
|
+
def initialize(status = nil, headers = {}, body = nil)
|
12
12
|
@status, @headers, @body = status, headers, body
|
13
|
-
@
|
13
|
+
@raw_body = ""
|
14
|
+
@finished = !!@body
|
14
15
|
end
|
15
16
|
|
16
17
|
def status
|
@@ -18,6 +19,7 @@ class Celluloid::Http::Response
|
|
18
19
|
end
|
19
20
|
|
20
21
|
def on_message_complete
|
22
|
+
@body = decoded_body
|
21
23
|
@finished = true
|
22
24
|
end
|
23
25
|
|
@@ -26,7 +28,7 @@ class Celluloid::Http::Response
|
|
26
28
|
end
|
27
29
|
|
28
30
|
def on_body(chunk)
|
29
|
-
@
|
31
|
+
@raw_body << chunk
|
30
32
|
end
|
31
33
|
|
32
34
|
def on_headers_complete(headers)
|
@@ -45,4 +47,13 @@ class Celluloid::Http::Response
|
|
45
47
|
reason.downcase.gsub(/\s|-/, '_').to_sym
|
46
48
|
end
|
47
49
|
|
50
|
+
def body
|
51
|
+
on_message_complete unless finished?
|
52
|
+
@body ||= ""
|
53
|
+
end
|
54
|
+
|
55
|
+
def decoded_body
|
56
|
+
Celluloid::Http::BodyDecoder.decode(headers, raw_body)
|
57
|
+
end
|
58
|
+
|
48
59
|
end
|
data/lib/celluloid-http.rb
CHANGED
@@ -10,6 +10,8 @@ module Celluloid
|
|
10
10
|
autoload :Request, 'celluloid-http/request'
|
11
11
|
autoload :Http, 'celluloid-http/http'
|
12
12
|
autoload :Connection, 'celluloid-http/connection'
|
13
|
+
autoload :Builder, 'celluloid-http/builder'
|
14
|
+
autoload :BodyDecoder, 'celluloid-http/body_decoder'
|
13
15
|
|
14
16
|
extend Http
|
15
17
|
end
|
Binary file
|
@@ -0,0 +1,8 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Date: Mon, 28 Jan 2013 18:56:25 GMT
|
3
|
+
Status: 200 OK
|
4
|
+
Connection: close
|
5
|
+
Content-Type: application/json; charset=utf-8
|
6
|
+
Content-Length: 133
|
7
|
+
|
8
|
+
{"comment":{"id":31,"message":"My comment.","author_name":"Guest 1359395318.7592747","created_at":"Mon, 28 Jan 2013 22:56:25 +0400"}}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class BuilderTest < TestCase
|
4
|
+
|
5
|
+
def test_create_get_request
|
6
|
+
url = 'http://en.wikipedia.org/wiki/Http?param=value'
|
7
|
+
|
8
|
+
request = Celluloid::Http::Builder.get(url)
|
9
|
+
|
10
|
+
assert_equal "en.wikipedia.org", request.host
|
11
|
+
assert_equal :get, request.method
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_create_post_request
|
15
|
+
url = 'http://en.wikipedia.org/wiki/Http?param=value'
|
16
|
+
|
17
|
+
params = {:param2 => "value2"}
|
18
|
+
|
19
|
+
request = Celluloid::Http::Builder.post(url, params)
|
20
|
+
|
21
|
+
assert_equal "en.wikipedia.org", request.host
|
22
|
+
assert_equal :post, request.method
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ConnectionTest < TestCase
|
4
|
+
|
5
|
+
def test_static_constructor
|
6
|
+
connection = mock('Connection')
|
7
|
+
connection.expects(:open).with('host',8080)
|
8
|
+
Celluloid::Http::Connection.expects(:new).returns(connection)
|
9
|
+
|
10
|
+
Celluloid::Http::Connection.open('host',8080)
|
11
|
+
end
|
12
|
+
end
|
data/test/unit/http_test.rb
CHANGED
@@ -15,7 +15,25 @@ class HttpTest < TestCase
|
|
15
15
|
stub_connection_with_fixture("en.wikipedia.org")
|
16
16
|
|
17
17
|
response = Celluloid::Http.get('http://en.wikipedia.org/wiki/Http')
|
18
|
+
|
18
19
|
assert_match /Wikipedia, the free encyclopedia/, response.body
|
19
20
|
end
|
20
21
|
|
22
|
+
def test_http_get_gzip_method
|
23
|
+
stub_connection_with_fixture("gzip.wikipedia.org")
|
24
|
+
|
25
|
+
response = Celluloid::Http.get('http://en.wikipedia.org/wiki/Chunked_transfer_encoding')
|
26
|
+
|
27
|
+
assert_match /Chunked transfer encoding/, response.body
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_http_post_method
|
31
|
+
stub_connection_with_fixture("post.example.com")
|
32
|
+
|
33
|
+
params = { :comment => { :message => "My comment." } }
|
34
|
+
response = Celluloid::Http.post('http://example.com/api/comments', params)
|
35
|
+
|
36
|
+
assert_match /"message":"My comment."/, response.body
|
37
|
+
end
|
38
|
+
|
21
39
|
end
|
data/test/unit/request_test.rb
CHANGED
@@ -45,5 +45,35 @@ class RequestTest < TestCase
|
|
45
45
|
assert_equal 'value3', request.query_params['param3']
|
46
46
|
end
|
47
47
|
|
48
|
+
def test_get_http_request_to_s
|
49
|
+
request = Celluloid::Http::Request.new 'http://localhost?param=value'
|
50
|
+
|
51
|
+
string_request = <<-eos
|
52
|
+
GET /?param=value HTTP/1.1
|
53
|
+
Host: localhost
|
54
|
+
|
55
|
+
eos
|
56
|
+
|
57
|
+
assert_equal string_request, request.to_s
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_post_http_request_to_s
|
61
|
+
options = {
|
62
|
+
method: :post,
|
63
|
+
form_data: {param: :value, param2: :value2 }
|
64
|
+
}
|
65
|
+
request = Celluloid::Http::Request.new 'http://localhost', options
|
66
|
+
|
67
|
+
string_request = <<-eos
|
68
|
+
POST / HTTP/1.1
|
69
|
+
Host: localhost
|
70
|
+
|
71
|
+
param2=value2¶m=value
|
72
|
+
eos
|
73
|
+
|
74
|
+
# without \n symbol
|
75
|
+
assert_equal string_request[0...-1], request.to_s
|
76
|
+
end
|
77
|
+
|
48
78
|
|
49
79
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: celluloid-http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-05-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -75,6 +75,22 @@ dependencies:
|
|
75
75
|
- - ! '>='
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rake
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
78
94
|
- !ruby/object:Gem::Dependency
|
79
95
|
name: turn
|
80
96
|
requirement: !ruby/object:Gem::Requirement
|
@@ -131,20 +147,27 @@ extensions: []
|
|
131
147
|
extra_rdoc_files: []
|
132
148
|
files:
|
133
149
|
- .gitignore
|
150
|
+
- .travis.yml
|
134
151
|
- Gemfile
|
135
152
|
- LICENSE.txt
|
136
153
|
- README.md
|
137
154
|
- Rakefile
|
138
155
|
- celluloid-http.gemspec
|
139
156
|
- lib/celluloid-http.rb
|
157
|
+
- lib/celluloid-http/body_decoder.rb
|
158
|
+
- lib/celluloid-http/builder.rb
|
140
159
|
- lib/celluloid-http/connection.rb
|
141
160
|
- lib/celluloid-http/http.rb
|
142
161
|
- lib/celluloid-http/request.rb
|
143
162
|
- lib/celluloid-http/response.rb
|
144
163
|
- lib/celluloid-http/version.rb
|
145
164
|
- test/fixtures/en.wikipedia.org
|
165
|
+
- test/fixtures/gzip.wikipedia.org
|
166
|
+
- test/fixtures/post.example.com
|
146
167
|
- test/support/tcp_socket_stub.rb
|
147
168
|
- test/test_helper.rb
|
169
|
+
- test/unit/builder_test.rb
|
170
|
+
- test/unit/connection_test.rb
|
148
171
|
- test/unit/http_test.rb
|
149
172
|
- test/unit/request_test.rb
|
150
173
|
- test/unit/response_test.rb
|
@@ -160,12 +183,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
160
183
|
- - ! '>='
|
161
184
|
- !ruby/object:Gem::Version
|
162
185
|
version: '0'
|
186
|
+
segments:
|
187
|
+
- 0
|
188
|
+
hash: 4403004894707993416
|
163
189
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
190
|
none: false
|
165
191
|
requirements:
|
166
192
|
- - ! '>='
|
167
193
|
- !ruby/object:Gem::Version
|
168
194
|
version: '0'
|
195
|
+
segments:
|
196
|
+
- 0
|
197
|
+
hash: 4403004894707993416
|
169
198
|
requirements: []
|
170
199
|
rubyforge_project:
|
171
200
|
rubygems_version: 1.8.24
|
@@ -174,8 +203,12 @@ specification_version: 3
|
|
174
203
|
summary: Http requests for celluloid. Based on Celluloid::IO.
|
175
204
|
test_files:
|
176
205
|
- test/fixtures/en.wikipedia.org
|
206
|
+
- test/fixtures/gzip.wikipedia.org
|
207
|
+
- test/fixtures/post.example.com
|
177
208
|
- test/support/tcp_socket_stub.rb
|
178
209
|
- test/test_helper.rb
|
210
|
+
- test/unit/builder_test.rb
|
211
|
+
- test/unit/connection_test.rb
|
179
212
|
- test/unit/http_test.rb
|
180
213
|
- test/unit/request_test.rb
|
181
214
|
- test/unit/response_test.rb
|