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.

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[Http.canonicalize_header(field)] = value
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 ||= Http.canonicalize_header(name)
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[Http.canonicalize_header(name)]
108
+ @headers[name] || @headers[canonicalize_header(name)]
106
109
  end
107
110
 
108
111
  # Obtain the response body
@@ -4,7 +4,7 @@ module HTTP
4
4
  attr_reader :headers
5
5
 
6
6
  def initialize
7
- @parser = Http::Parser.new(self)
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
- # Http::Parser callbacks
29
+ # HTTP::Parser callbacks
30
30
  #
31
31
 
32
32
  def on_headers_complete(headers)
data/lib/http/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module HTTP
2
- VERSION = "0.5.0.pre"
2
+ VERSION = "0.5.0.pre2" unless defined?(HTTP::VERSION)
3
3
  end
@@ -1,17 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Http::Options, "body" do
3
+ describe HTTP::Options, "body" do
4
4
 
5
- let(:opts){ Http::Options.new }
5
+ let(:opts){ HTTP::Options.new }
6
6
 
7
7
  it 'defaults to nil' do
8
- opts.body.should be_nil
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.should be_nil
14
- opts2.body.should eq("foo")
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 Http::Options, "callbacks" do
3
+ describe HTTP::Options, "callbacks" do
4
4
 
5
- let(:opts){ Http::Options.new }
5
+ let(:opts){ HTTP::Options.new }
6
6
  let(:callback){ Proc.new{|r| nil } }
7
7
 
8
8
  it 'recognizes invalid events' do
9
- lambda{
9
+ expect {
10
10
  opts.with_callback(:notacallback, callback)
11
- }.should raise_error(ArgumentError, /notacallback/)
11
+ }.to raise_error(ArgumentError, /notacallback/)
12
12
  end
13
13
 
14
14
  it 'recognizes invalid callbacks' do
15
- lambda{
15
+ expect {
16
16
  opts.with_callback(:request, Object.new)
17
- }.should raise_error(ArgumentError, /invalid callback/)
18
- lambda{
17
+ }.to raise_error(ArgumentError, /invalid callback/)
18
+ expect {
19
19
  opts.with_callback(:request, Proc.new{|a,b| nil})
20
- }.should raise_error(ArgumentError, /only one argument/)
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].should eq([])
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].should eq([])
33
- opts2.callbacks[:request].should eq([callback])
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].should eq([callback])
37
- opts3.callbacks[:request].should eq([callback, callback])
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].should eq([])
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].should eq([])
52
- opts2.callbacks[:response].should eq([callback])
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].should eq([callback])
56
- opts3.callbacks[:response].should eq([callback, callback])
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 Http::Options, "form" do
3
+ describe HTTP::Options, "form" do
4
4
 
5
- let(:opts){ Http::Options.new }
5
+ let(:opts){ HTTP::Options.new }
6
6
 
7
7
  it 'defaults to nil' do
8
- opts.form.should be_nil
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.should be_nil
14
- opts2.form.should eq(:foo => 42)
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 Http::Options, "headers" do
3
+ describe HTTP::Options, "headers" do
4
4
 
5
- let(:opts) { Http::Options.new }
6
- let(:user_agent) { "RubyHttpGem/#{Http::VERSION}" }
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.should eq("User-Agent" => user_agent)
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.should eq("User-Agent" => user_agent)
15
- opts2.headers.should eq("accept" => "json", "User-Agent" => user_agent)
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"].should eq("json")
20
+ expect(opts.with_headers(x).headers["accept"]).to eq("json")
21
21
  end
22
22
 
23
23
  it 'recognizes invalid headers' do
24
- lambda{
24
+ expect {
25
25
  opts.with_headers(self)
26
- }.should raise_error(ArgumentError)
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 Http::Options, "merge" do
3
+ describe HTTP::Options, "merge" do
4
4
 
5
- let(:opts) { Http::Options.new }
6
- let(:user_agent) { "RubyHttpGem/#{Http::VERSION}" }
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.should eq(:body)
11
- opts.response.should eq(old_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(Http::Options.new(:response => :body))
16
- merged.response.should eq(:body)
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 = Http::Options.new(
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 = Http::Options.new(
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.should eq(
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 Http::Options, "new" do
4
- let(:user_agent) { "RubyHttpGem/#{Http::VERSION}" }
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 = Http::Options.new
8
- Http::Options.new(opts).should eq(opts)
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 = Http::Options.new(:response => :object)
14
- opts.response.should eq(:object)
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 = Http::Options.new(:headers => {:accept => "json"})
19
- opts.headers.should eq(:accept => "json", "User-Agent" => user_agent)
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 = Http::Options.new(:proxy => {:proxy_address => "127.0.0.1", :proxy_port => 8080})
24
- opts.proxy.should eq(:proxy_address => "127.0.0.1", :proxy_port => 8080)
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 = Http::Options.new(:form => {:foo => 42})
29
- opts.form.should eq(:foo => 42)
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 = Http::Options.new(:callbacks => callbacks)
36
- opts.callbacks.should eq({
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 Http::Options, "proxy" do
3
+ describe HTTP::Options, "proxy" do
4
4
 
5
- let(:opts){ Http::Options.new }
5
+ let(:opts){ HTTP::Options.new }
6
6
 
7
7
  it 'defaults to {}' do
8
- opts.proxy.should eq({})
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.should eq({})
14
- opts2.proxy.should eq(:proxy_address => "127.0.0.1", :proxy_port => 8080)
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.should eq(:proxy_address => "127.0.0.1", :proxy_port => 8080, :proxy_username => "username", :proxy_password => "password")
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 Http::Options, "response" do
3
+ describe HTTP::Options, "response" do
4
4
 
5
- let(:opts){ Http::Options.new }
5
+ let(:opts){ HTTP::Options.new }
6
6
 
7
7
  it 'defaults to :auto' do
8
- opts.response.should eq(:auto)
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.should eq(:auto)
14
- opts2.response.should eq(:body)
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
- lambda{
18
+ expect {
19
19
  opts.with_response(:not_a_valid_response)
20
- }.should raise_error(ArgumentError, /not_a_valid_response/)
20
+ }.to raise_error(ArgumentError, /not_a_valid_response/)
21
21
  end
22
22
 
23
23
  end
@@ -1,16 +1,16 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Http::Options do
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].should eq(:body)
8
- subject[:nosuchone].should be_nil
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.should be_a(Hash)
13
- subject.to_hash[:response].should eq(:body)
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 Http::RequestStream do
3
+ describe HTTP::RequestStream do
4
4
  describe "#initalize" do
5
5
  def construct(body)
6
- Http::RequestStream.new(nil, body, [], "")
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 []}.to_not raise_error(ArgumentError)
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"}.to_not raise_error(ArgumentError)
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"]}.to_not raise_error(ArgumentError)
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(ArgumentError)
23
-
22
+ expect {construct true}.to raise_error
24
23
  end
25
24
  end
26
25
  end