http 0.5.0.pre → 0.5.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of http might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/CHANGES.md +7 -6
- data/README.md +18 -31
- data/Rakefile +2 -2
- data/examples/celluloid.rb +2 -2
- data/http.gemspec +1 -1
- data/lib/http.rb +1 -14
- data/lib/http/chainable.rb +4 -4
- data/lib/http/client.rb +11 -10
- data/lib/http/header.rb +11 -0
- data/lib/http/mime_type.rb +1 -1
- data/lib/http/mime_types/json.rb +1 -1
- data/lib/http/options.rb +7 -6
- data/lib/http/request.rb +11 -3
- data/lib/http/response.rb +6 -3
- data/lib/http/response_parser.rb +2 -2
- data/lib/http/version.rb +1 -1
- data/spec/http/options/body_spec.rb +5 -5
- data/spec/http/options/callbacks_spec.rb +18 -18
- data/spec/http/options/form_spec.rb +5 -5
- data/spec/http/options/headers_spec.rb +9 -9
- data/spec/http/options/merge_spec.rb +11 -11
- data/spec/http/options/new_spec.rb +14 -14
- data/spec/http/options/proxy_spec.rb +6 -6
- data/spec/http/options/response_spec.rb +7 -7
- data/spec/http/options_spec.rb +5 -5
- data/spec/http/request_spec.rb +19 -0
- data/spec/http/request_stream_spec.rb +6 -7
- data/spec/http/response_spec.rb +12 -12
- data/spec/http_spec.rb +39 -39
- data/spec/spec_helper.rb +6 -0
- metadata +7 -7
- data/lib/http/compat/curb.rb +0 -87
- data/spec/http/compat/curb_spec.rb +0 -40
data/lib/http/response.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
require 'delegate'
|
2
|
+
require 'http/header'
|
2
3
|
|
3
4
|
module HTTP
|
4
5
|
class Response
|
6
|
+
include HTTP::Header
|
7
|
+
|
5
8
|
STATUS_CODES = {
|
6
9
|
100 => 'Continue',
|
7
10
|
101 => 'Switching Protocols',
|
@@ -73,7 +76,7 @@ module HTTP
|
|
73
76
|
|
74
77
|
@headers = {}
|
75
78
|
headers.each do |field, value|
|
76
|
-
@headers[
|
79
|
+
@headers[canonicalize_header(field)] = value
|
77
80
|
end
|
78
81
|
end
|
79
82
|
|
@@ -83,7 +86,7 @@ module HTTP
|
|
83
86
|
key = name[CANONICAL_HEADER]
|
84
87
|
|
85
88
|
# Convert to canonical capitalization
|
86
|
-
key ||=
|
89
|
+
key ||= canonicalize_header(name)
|
87
90
|
|
88
91
|
# Check if the header has already been set and group
|
89
92
|
old_value = @headers[key]
|
@@ -102,7 +105,7 @@ module HTTP
|
|
102
105
|
|
103
106
|
# Get a header value
|
104
107
|
def [](name)
|
105
|
-
@headers[name] || @headers[
|
108
|
+
@headers[name] || @headers[canonicalize_header(name)]
|
106
109
|
end
|
107
110
|
|
108
111
|
# Obtain the response body
|
data/lib/http/response_parser.rb
CHANGED
@@ -4,7 +4,7 @@ module HTTP
|
|
4
4
|
attr_reader :headers
|
5
5
|
|
6
6
|
def initialize
|
7
|
-
@parser =
|
7
|
+
@parser = HTTP::Parser.new(self)
|
8
8
|
reset
|
9
9
|
end
|
10
10
|
|
@@ -26,7 +26,7 @@ module HTTP
|
|
26
26
|
end
|
27
27
|
|
28
28
|
#
|
29
|
-
#
|
29
|
+
# HTTP::Parser callbacks
|
30
30
|
#
|
31
31
|
|
32
32
|
def on_headers_complete(headers)
|
data/lib/http/version.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe HTTP::Options, "body" do
|
4
4
|
|
5
|
-
let(:opts){
|
5
|
+
let(:opts){ HTTP::Options.new }
|
6
6
|
|
7
7
|
it 'defaults to nil' do
|
8
|
-
opts.body.
|
8
|
+
expect(opts.body).to be_nil
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'may be specified with with_body' do
|
12
12
|
opts2 = opts.with_body("foo")
|
13
|
-
opts.body.
|
14
|
-
opts2.body.
|
13
|
+
expect(opts.body).to be_nil
|
14
|
+
expect(opts2.body).to eq("foo")
|
15
15
|
end
|
16
16
|
|
17
17
|
end
|
@@ -1,40 +1,40 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe HTTP::Options, "callbacks" do
|
4
4
|
|
5
|
-
let(:opts){
|
5
|
+
let(:opts){ HTTP::Options.new }
|
6
6
|
let(:callback){ Proc.new{|r| nil } }
|
7
7
|
|
8
8
|
it 'recognizes invalid events' do
|
9
|
-
|
9
|
+
expect {
|
10
10
|
opts.with_callback(:notacallback, callback)
|
11
|
-
}.
|
11
|
+
}.to raise_error(ArgumentError, /notacallback/)
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'recognizes invalid callbacks' do
|
15
|
-
|
15
|
+
expect {
|
16
16
|
opts.with_callback(:request, Object.new)
|
17
|
-
}.
|
18
|
-
|
17
|
+
}.to raise_error(ArgumentError, /invalid callback/)
|
18
|
+
expect {
|
19
19
|
opts.with_callback(:request, Proc.new{|a,b| nil})
|
20
|
-
}.
|
20
|
+
}.to raise_error(ArgumentError, /only one argument/)
|
21
21
|
end
|
22
22
|
|
23
23
|
describe "request" do
|
24
24
|
|
25
25
|
it 'defaults to []' do
|
26
|
-
opts.callbacks[:request].
|
26
|
+
expect(opts.callbacks[:request]).to eq([])
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'may be specified with with_callback(:request, ...)' do
|
30
30
|
|
31
31
|
opts2 = opts.with_callback(:request, callback)
|
32
|
-
opts.callbacks[:request].
|
33
|
-
opts2.callbacks[:request].
|
32
|
+
expect(opts.callbacks[:request]).to eq([])
|
33
|
+
expect(opts2.callbacks[:request]).to eq([callback])
|
34
34
|
|
35
35
|
opts3 = opts2.with_callback(:request, callback)
|
36
|
-
opts2.callbacks[:request].
|
37
|
-
opts3.callbacks[:request].
|
36
|
+
expect(opts2.callbacks[:request]).to eq([callback])
|
37
|
+
expect(opts3.callbacks[:request]).to eq([callback, callback])
|
38
38
|
end
|
39
39
|
|
40
40
|
end
|
@@ -42,18 +42,18 @@ describe Http::Options, "callbacks" do
|
|
42
42
|
describe "response" do
|
43
43
|
|
44
44
|
it 'defaults to []' do
|
45
|
-
opts.callbacks[:response].
|
45
|
+
expect(opts.callbacks[:response]).to eq([])
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'may be specified with with_callback(:response, ...)' do
|
49
49
|
|
50
50
|
opts2 = opts.with_callback(:response, callback)
|
51
|
-
opts.callbacks[:response].
|
52
|
-
opts2.callbacks[:response].
|
51
|
+
expect(opts.callbacks[:response]).to eq([])
|
52
|
+
expect(opts2.callbacks[:response]).to eq([callback])
|
53
53
|
|
54
54
|
opts3 = opts2.with_callback(:response, callback)
|
55
|
-
opts2.callbacks[:response].
|
56
|
-
opts3.callbacks[:response].
|
55
|
+
expect(opts2.callbacks[:response]).to eq([callback])
|
56
|
+
expect(opts3.callbacks[:response]).to eq([callback, callback])
|
57
57
|
end
|
58
58
|
|
59
59
|
end
|
@@ -1,17 +1,17 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe HTTP::Options, "form" do
|
4
4
|
|
5
|
-
let(:opts){
|
5
|
+
let(:opts){ HTTP::Options.new }
|
6
6
|
|
7
7
|
it 'defaults to nil' do
|
8
|
-
opts.form.
|
8
|
+
expect(opts.form).to be_nil
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'may be specified with with_form_data' do
|
12
12
|
opts2 = opts.with_form(:foo => 42)
|
13
|
-
opts.form.
|
14
|
-
opts2.form.
|
13
|
+
expect(opts.form).to be_nil
|
14
|
+
expect(opts2.form).to eq(:foo => 42)
|
15
15
|
end
|
16
16
|
|
17
17
|
end
|
@@ -1,29 +1,29 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe HTTP::Options, "headers" do
|
4
4
|
|
5
|
-
let(:opts) {
|
6
|
-
let(:user_agent) { "
|
5
|
+
let(:opts) { HTTP::Options.new }
|
6
|
+
let(:user_agent) { "RubyHTTPGem/#{HTTP::VERSION}" }
|
7
7
|
|
8
8
|
it 'defaults to just the user agent' do
|
9
|
-
opts.headers.
|
9
|
+
expect(opts.headers).to eq("User-Agent" => user_agent)
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'may be specified with with_headers' do
|
13
13
|
opts2 = opts.with_headers("accept" => "json")
|
14
|
-
opts.headers.
|
15
|
-
opts2.headers.
|
14
|
+
expect(opts.headers).to eq("User-Agent" => user_agent)
|
15
|
+
expect(opts2.headers).to eq("accept" => "json", "User-Agent" => user_agent)
|
16
16
|
end
|
17
17
|
|
18
18
|
it 'accepts any object that respond to :to_hash' do
|
19
19
|
x = Struct.new(:to_hash).new("accept" => "json")
|
20
|
-
opts.with_headers(x).headers["accept"].
|
20
|
+
expect(opts.with_headers(x).headers["accept"]).to eq("json")
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'recognizes invalid headers' do
|
24
|
-
|
24
|
+
expect {
|
25
25
|
opts.with_headers(self)
|
26
|
-
}.
|
26
|
+
}.to raise_error(ArgumentError)
|
27
27
|
end
|
28
28
|
|
29
29
|
end
|
@@ -1,25 +1,25 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe HTTP::Options, "merge" do
|
4
4
|
|
5
|
-
let(:opts) {
|
6
|
-
let(:user_agent) { "
|
5
|
+
let(:opts) { HTTP::Options.new }
|
6
|
+
let(:user_agent) { "RubyHTTPGem/#{HTTP::VERSION}" }
|
7
7
|
|
8
8
|
it 'supports a Hash' do
|
9
9
|
old_response = opts.response
|
10
|
-
opts.merge(:response => :body).response.
|
11
|
-
opts.response.
|
10
|
+
expect(opts.merge(:response => :body).response).to eq(:body)
|
11
|
+
expect(opts.response).to eq(old_response)
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'supports another Options' do
|
15
|
-
merged = opts.merge(
|
16
|
-
merged.response.
|
15
|
+
merged = opts.merge(HTTP::Options.new(:response => :body))
|
16
|
+
expect(merged.response).to eq(:body)
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'merges as excepted in complex cases' do
|
20
20
|
# FIXME: yuck :(
|
21
21
|
|
22
|
-
foo =
|
22
|
+
foo = HTTP::Options.new(
|
23
23
|
:response => :body,
|
24
24
|
:params => {:baz => 'bar'},
|
25
25
|
:form => {:foo => 'foo'},
|
@@ -27,7 +27,7 @@ describe Http::Options, "merge" do
|
|
27
27
|
:headers => {:accept => "json", :foo => 'foo'},
|
28
28
|
:proxy => {},
|
29
29
|
:callbacks => {:request => ["common"], :response => ["foo"]})
|
30
|
-
bar =
|
30
|
+
bar = HTTP::Options.new(
|
31
31
|
:response => :parsed_body,
|
32
32
|
:params => {:plop => 'plip'},
|
33
33
|
:form => {:bar => 'bar'},
|
@@ -35,9 +35,9 @@ describe Http::Options, "merge" do
|
|
35
35
|
:headers => {:accept => "xml", :bar => 'bar'},
|
36
36
|
:proxy => {:proxy_address => "127.0.0.1", :proxy_port => 8080},
|
37
37
|
:callbacks => {:request => ["common"], :response => ["bar"]})
|
38
|
-
foo.merge(bar).to_hash.
|
38
|
+
expect(foo.merge(bar).to_hash).to eq(
|
39
39
|
:response => :parsed_body,
|
40
|
-
:params=>{:plop=>"plip"},
|
40
|
+
:params=>{:plop=>"plip"},
|
41
41
|
:form => {:bar => 'bar'},
|
42
42
|
:body => "body-bar",
|
43
43
|
:headers => {:accept => "xml", :foo => "foo", :bar => 'bar', "User-Agent" => user_agent},
|
@@ -1,39 +1,39 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
4
|
-
let(:user_agent) { "
|
3
|
+
describe HTTP::Options, "new" do
|
4
|
+
let(:user_agent) { "RubyHTTPGem/#{HTTP::VERSION}" }
|
5
5
|
|
6
6
|
it 'supports a Options instance' do
|
7
|
-
opts =
|
8
|
-
|
7
|
+
opts = HTTP::Options.new
|
8
|
+
expect(HTTP::Options.new(opts)).to eq(opts)
|
9
9
|
end
|
10
10
|
|
11
11
|
context 'with a Hash' do
|
12
12
|
it 'coerces :response correctly' do
|
13
|
-
opts =
|
14
|
-
opts.response.
|
13
|
+
opts = HTTP::Options.new(:response => :object)
|
14
|
+
expect(opts.response).to eq(:object)
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'coerces :headers correctly' do
|
18
|
-
opts =
|
19
|
-
opts.headers.
|
18
|
+
opts = HTTP::Options.new(:headers => {:accept => "json"})
|
19
|
+
expect(opts.headers).to eq(:accept => "json", "User-Agent" => user_agent)
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'coerces :proxy correctly' do
|
23
|
-
opts =
|
24
|
-
opts.proxy.
|
23
|
+
opts = HTTP::Options.new(:proxy => {:proxy_address => "127.0.0.1", :proxy_port => 8080})
|
24
|
+
expect(opts.proxy).to eq(:proxy_address => "127.0.0.1", :proxy_port => 8080)
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'coerces :form correctly' do
|
28
|
-
opts =
|
29
|
-
opts.form.
|
28
|
+
opts = HTTP::Options.new(:form => {:foo => 42})
|
29
|
+
expect(opts.form).to eq(:foo => 42)
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'coerces :callbacks correctly' do
|
33
33
|
before, after = Proc.new{|r| :before}, Proc.new{|r| :after}
|
34
34
|
callbacks = {:request => [before], :response => [after]}
|
35
|
-
opts =
|
36
|
-
opts.callbacks.
|
35
|
+
opts = HTTP::Options.new(:callbacks => callbacks)
|
36
|
+
expect(opts.callbacks).to eq({
|
37
37
|
:request => [before],
|
38
38
|
:response => [after]
|
39
39
|
})
|
@@ -1,21 +1,21 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe HTTP::Options, "proxy" do
|
4
4
|
|
5
|
-
let(:opts){
|
5
|
+
let(:opts){ HTTP::Options.new }
|
6
6
|
|
7
7
|
it 'defaults to {}' do
|
8
|
-
opts.proxy.
|
8
|
+
expect(opts.proxy).to eq({})
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'may be specified with with_proxy' do
|
12
12
|
opts2 = opts.with_proxy(:proxy_address => "127.0.0.1", :proxy_port => 8080)
|
13
|
-
opts.proxy.
|
14
|
-
opts2.proxy.
|
13
|
+
expect(opts.proxy).to eq({})
|
14
|
+
expect(opts2.proxy).to eq(:proxy_address => "127.0.0.1", :proxy_port => 8080)
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'accepts proxy address, port, username, and password' do
|
18
18
|
opts2 = opts.with_proxy(:proxy_address => "127.0.0.1", :proxy_port => 8080, :proxy_username => "username", :proxy_password => "password")
|
19
|
-
opts2.proxy.
|
19
|
+
expect(opts2.proxy).to eq(:proxy_address => "127.0.0.1", :proxy_port => 8080, :proxy_username => "username", :proxy_password => "password")
|
20
20
|
end
|
21
21
|
end
|
@@ -1,23 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe HTTP::Options, "response" do
|
4
4
|
|
5
|
-
let(:opts){
|
5
|
+
let(:opts){ HTTP::Options.new }
|
6
6
|
|
7
7
|
it 'defaults to :auto' do
|
8
|
-
opts.response.
|
8
|
+
expect(opts.response).to eq(:auto)
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'may be specified with with_response' do
|
12
12
|
opts2 = opts.with_response(:body)
|
13
|
-
opts.response.
|
14
|
-
opts2.response.
|
13
|
+
expect(opts.response).to eq(:auto)
|
14
|
+
expect(opts2.response).to eq(:body)
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'recognizes invalid responses' do
|
18
|
-
|
18
|
+
expect {
|
19
19
|
opts.with_response(:not_a_valid_response)
|
20
|
-
}.
|
20
|
+
}.to raise_error(ArgumentError, /not_a_valid_response/)
|
21
21
|
end
|
22
22
|
|
23
23
|
end
|
data/spec/http/options_spec.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe HTTP::Options do
|
4
4
|
subject { described_class.new(:response => :body) }
|
5
5
|
|
6
6
|
it "behaves like a Hash for reading" do
|
7
|
-
subject[:response].
|
8
|
-
subject[:nosuchone].
|
7
|
+
expect(subject[:response]).to eq(:body)
|
8
|
+
expect(subject[:nosuchone]).to be_nil
|
9
9
|
end
|
10
10
|
|
11
11
|
it "it's gois able to coerce to a Hash" do
|
12
|
-
subject.to_hash.
|
13
|
-
subject.to_hash[:response].
|
12
|
+
expect(subject.to_hash).to be_a(Hash)
|
13
|
+
expect(subject.to_hash[:response]).to eq(:body)
|
14
14
|
end
|
15
15
|
|
16
16
|
it "raises ArgumentError with invalid options" do
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HTTP::Request do
|
4
|
+
describe "headers" do
|
5
|
+
subject { HTTP::Request.new(:get, "http://example.com/", :accept => "text/html") }
|
6
|
+
|
7
|
+
it "sets explicit headers" do
|
8
|
+
expect(subject["Accept"]).to eq("text/html")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "sets implicit headers" do
|
12
|
+
expect(subject["Host"]).to eq("example.com")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "provides a #headers accessor" do
|
16
|
+
expect(subject.headers).to eq("Accept" => "text/html", "Host" => "example.com")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,26 +1,25 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe HTTP::RequestStream do
|
4
4
|
describe "#initalize" do
|
5
5
|
def construct(body)
|
6
|
-
|
6
|
+
HTTP::RequestStream.new(nil, body, [], "")
|
7
7
|
end
|
8
8
|
|
9
9
|
it "doesn't throw on a nil body" do
|
10
|
-
expect {construct []}.
|
10
|
+
expect {construct []}.not_to raise_error
|
11
11
|
end
|
12
12
|
|
13
13
|
it "doesn't throw on a String body" do
|
14
|
-
expect {construct "string body"}.
|
14
|
+
expect {construct "string body"}.not_to raise_error
|
15
15
|
end
|
16
16
|
|
17
17
|
it "doesn't throw on an Enumerable body" do
|
18
|
-
expect {construct ["bees", "cows"]}.
|
18
|
+
expect {construct ["bees", "cows"]}.not_to raise_error
|
19
19
|
end
|
20
20
|
|
21
21
|
it "does throw on a body that isn't string, enumerable or nil" do
|
22
|
-
expect {construct true}.to raise_error
|
23
|
-
|
22
|
+
expect {construct true}.to raise_error
|
24
23
|
end
|
25
24
|
end
|
26
25
|
end
|