httpi 0.1.0 → 0.2.0
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/Gemfile +1 -1
- data/Gemfile.lock +28 -0
- data/README.rdoc +68 -39
- data/Rakefile +28 -2
- data/lib/httpi/adapter.rb +1 -1
- data/lib/httpi/adapter/curb.rb +26 -34
- data/lib/httpi/adapter/httpclient.rb +23 -25
- data/lib/httpi/client.rb +63 -5
- data/lib/httpi/request.rb +73 -0
- data/lib/httpi/version.rb +1 -1
- data/spec/fixtures/xml.gz +0 -0
- data/spec/fixtures/xml.xml +10 -0
- data/spec/httpi/adapter/curb_spec.rb +29 -69
- data/spec/httpi/adapter/httpclient_spec.rb +25 -66
- data/spec/httpi/adapter_spec.rb +11 -12
- data/spec/httpi/client_spec.rb +89 -8
- data/spec/httpi/request_spec.rb +104 -0
- data/spec/httpi/response_spec.rb +6 -8
- data/spec/spec_helper.rb +1 -1
- data/spec/support/fixture.rb +19 -0
- data/spec/support/matchers.rb +2 -2
- metadata +13 -12
- data/lib/httpi/adapter/base.rb +0 -16
- data/lib/httpi/interface.rb +0 -21
- data/spec/fixtures/gzip.gz +0 -0
- data/spec/httpi/interface_spec.rb +0 -33
- data/spec/support/helper_methods.rb +0 -41
@@ -0,0 +1,104 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "httpi/request"
|
3
|
+
|
4
|
+
describe HTTPI::Request do
|
5
|
+
let(:request) { HTTPI::Request.new }
|
6
|
+
|
7
|
+
describe ".new" do
|
8
|
+
it "accepts a Hash of accessors to set" do
|
9
|
+
request = HTTPI::Request.new :url => "http://example.com", :open_timeout => 30
|
10
|
+
request.url.should == URI("http://example.com")
|
11
|
+
request.open_timeout.should == 30
|
12
|
+
end
|
13
|
+
|
14
|
+
it "accepts a Hash of authentication credentials to set" do
|
15
|
+
request = HTTPI::Request.new :basic_auth => ["username", "password"]
|
16
|
+
request.basic_auth.should == ["username", "password"]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#url" do
|
21
|
+
it "lets you specify the URL to access as a String" do
|
22
|
+
request.url = "http://example.com"
|
23
|
+
request.url.should == URI("http://example.com")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "also accepts a URI object" do
|
27
|
+
request.url = URI("http://example.com")
|
28
|
+
request.url.should == URI("http://example.com")
|
29
|
+
end
|
30
|
+
|
31
|
+
it "raises an ArgumentError in case the url does not seem to be valid" do
|
32
|
+
lambda { request.url = "invalid" }.should raise_error(ArgumentError)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#proxy" do
|
37
|
+
it "lets you specify the proxy URL to use as a String" do
|
38
|
+
request.proxy = "http://proxy.example.com"
|
39
|
+
request.proxy.should == URI("http://proxy.example.com")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "also accepts a URI object" do
|
43
|
+
request.proxy = URI("http://proxy.example.com")
|
44
|
+
request.proxy.should == URI("http://proxy.example.com")
|
45
|
+
end
|
46
|
+
|
47
|
+
it "raises an ArgumentError in case the url does not seem to be valid" do
|
48
|
+
lambda { request.proxy = "invalid" }.should raise_error(ArgumentError)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "#headers" do
|
53
|
+
it "lets you specify a Hash of HTTP request headers" do
|
54
|
+
request.headers = { "Accept-Encoding" => "gzip" }
|
55
|
+
request.headers.should == { "Accept-Encoding" => "gzip" }
|
56
|
+
end
|
57
|
+
|
58
|
+
it "defaults to return an empty Hash" do
|
59
|
+
request.headers.should == {}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#body" do
|
64
|
+
it "lets you specify the HTTP request body" do
|
65
|
+
request.body = "<some>xml</some>"
|
66
|
+
request.body.should == "<some>xml</some>"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "#open_timeout" do
|
71
|
+
it "lets you specify the open timeout" do
|
72
|
+
request.open_timeout = 30
|
73
|
+
request.open_timeout.should == 30
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "#read_timeout" do
|
78
|
+
it "lets you specify the read timeout" do
|
79
|
+
request.read_timeout = 45
|
80
|
+
request.read_timeout.should == 45
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "#basic_auth" do
|
85
|
+
it "lets you specify the basic auth credentials" do
|
86
|
+
request.basic_auth "username", "password"
|
87
|
+
request.basic_auth.should == ["username", "password"]
|
88
|
+
end
|
89
|
+
|
90
|
+
it "also accepts an Array of credentials" do
|
91
|
+
request.basic_auth ["username", "password"]
|
92
|
+
request.basic_auth.should == ["username", "password"]
|
93
|
+
end
|
94
|
+
|
95
|
+
it "lets you reset the credentials" do
|
96
|
+
request.basic_auth "username", "password"
|
97
|
+
request.basic_auth.should == ["username", "password"]
|
98
|
+
|
99
|
+
request.basic_auth nil
|
100
|
+
request.basic_auth.should be_nil
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
data/spec/httpi/response_spec.rb
CHANGED
@@ -2,31 +2,29 @@ require "spec_helper"
|
|
2
2
|
require "httpi/response"
|
3
3
|
|
4
4
|
describe HTTPI::Response do
|
5
|
-
|
6
|
-
@response = HTTPI::Response.new Some.response_code, Some.headers, Fixture.gzip
|
7
|
-
end
|
5
|
+
let(:response) { HTTPI::Response.new 200, { "Content-Encoding" => "gzip" }, Fixture.gzip }
|
8
6
|
|
9
7
|
describe "#code" do
|
10
8
|
it "should return the HTTP response code" do
|
11
|
-
|
9
|
+
response.code.should == 200
|
12
10
|
end
|
13
11
|
end
|
14
12
|
|
15
|
-
describe "#
|
13
|
+
describe "#headers" do
|
16
14
|
it "should return the HTTP response headers" do
|
17
|
-
|
15
|
+
response.headers.should == { "Content-Encoding" => "gzip" }
|
18
16
|
end
|
19
17
|
end
|
20
18
|
|
21
19
|
describe "#body" do
|
22
20
|
it "should return the (gzip decoded) HTTP response body" do
|
23
|
-
|
21
|
+
response.body.should == Fixture.xml
|
24
22
|
end
|
25
23
|
end
|
26
24
|
|
27
25
|
describe "#raw_body" do
|
28
26
|
it "should return the raw HTML response body" do
|
29
|
-
|
27
|
+
response.raw_body.should == Fixture.gzip
|
30
28
|
end
|
31
29
|
end
|
32
30
|
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
class Fixture
|
2
|
+
class << self
|
3
|
+
|
4
|
+
def xml
|
5
|
+
@xml ||= load :xml
|
6
|
+
end
|
7
|
+
|
8
|
+
def gzip
|
9
|
+
@gzip ||= load :xml, :gz
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def load(fixture, type = :xml)
|
15
|
+
File.read File.expand_path("../../fixtures/#{fixture}.#{type}", __FILE__)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
data/spec/support/matchers.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
RSpec::Matchers.define :be_a_valid_httpi_response do
|
2
2
|
match do |actual|
|
3
3
|
actual.should be_an(HTTPI::Response)
|
4
|
-
actual.code.should ==
|
5
|
-
actual.headers.should
|
4
|
+
actual.code.should == 200
|
5
|
+
actual.headers.should be_a(Hash)
|
6
6
|
actual.body.should == Fixture.xml
|
7
7
|
end
|
8
8
|
end
|
metadata
CHANGED
@@ -1,21 +1,22 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: httpi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Daniel Harrington
|
14
|
+
- Martin Tepper
|
14
15
|
autorequire:
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2010-09-
|
19
|
+
date: 2010-09-23 00:00:00 +02:00
|
19
20
|
default_executable:
|
20
21
|
dependencies:
|
21
22
|
- !ruby/object:Gem::Dependency
|
@@ -58,14 +59,14 @@ dependencies:
|
|
58
59
|
requirements:
|
59
60
|
- - "="
|
60
61
|
- !ruby/object:Gem::Version
|
61
|
-
hash:
|
62
|
+
hash: 62196431
|
62
63
|
segments:
|
63
64
|
- 2
|
64
65
|
- 0
|
65
66
|
- 0
|
66
67
|
- beta
|
67
|
-
-
|
68
|
-
version: 2.0.0.beta.
|
68
|
+
- 22
|
69
|
+
version: 2.0.0.beta.22
|
69
70
|
type: :development
|
70
71
|
version_requirements: *id003
|
71
72
|
- !ruby/object:Gem::Dependency
|
@@ -94,28 +95,28 @@ extra_rdoc_files: []
|
|
94
95
|
|
95
96
|
files:
|
96
97
|
- Gemfile
|
98
|
+
- Gemfile.lock
|
97
99
|
- Rakefile
|
98
100
|
- README.rdoc
|
99
101
|
- autotest/discover.rb
|
100
|
-
- lib/httpi/adapter/base.rb
|
101
102
|
- lib/httpi/adapter/curb.rb
|
102
103
|
- lib/httpi/adapter/httpclient.rb
|
103
104
|
- lib/httpi/adapter.rb
|
104
105
|
- lib/httpi/client.rb
|
105
|
-
- lib/httpi/
|
106
|
+
- lib/httpi/request.rb
|
106
107
|
- lib/httpi/response.rb
|
107
108
|
- lib/httpi/version.rb
|
108
109
|
- lib/httpi.rb
|
109
|
-
- spec/fixtures/
|
110
|
+
- spec/fixtures/xml.gz
|
110
111
|
- spec/fixtures/xml.xml
|
111
112
|
- spec/httpi/adapter/curb_spec.rb
|
112
113
|
- spec/httpi/adapter/httpclient_spec.rb
|
113
114
|
- spec/httpi/adapter_spec.rb
|
114
115
|
- spec/httpi/client_spec.rb
|
115
|
-
- spec/httpi/
|
116
|
+
- spec/httpi/request_spec.rb
|
116
117
|
- spec/httpi/response_spec.rb
|
117
118
|
- spec/spec_helper.rb
|
118
|
-
- spec/support/
|
119
|
+
- spec/support/fixture.rb
|
119
120
|
- spec/support/matchers.rb
|
120
121
|
- .rspec
|
121
122
|
has_rdoc: true
|
data/lib/httpi/adapter/base.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
module HTTPI
|
2
|
-
module Adapter
|
3
|
-
module Base
|
4
|
-
|
5
|
-
# List of methods expected to be implemented by an adapter.
|
6
|
-
METHODS = %w(setup client headers headers= proxy proxy= get post auth)
|
7
|
-
|
8
|
-
METHODS.each do |method|
|
9
|
-
define_method method do |*args|
|
10
|
-
raise NotImplementedError, "#{Adapter.use} does not implement a #{method} method"
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
data/lib/httpi/interface.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
require "httpi/adapter"
|
2
|
-
|
3
|
-
module HTTPI
|
4
|
-
module Interface
|
5
|
-
|
6
|
-
# Loads a given +adapter+. Defaults to load <tt>HTTPI::Adapter::DEFAULT</tt>.
|
7
|
-
def load!(adapter = nil)
|
8
|
-
adapter ||= Adapter.use
|
9
|
-
include_adapter Adapter.find(adapter)
|
10
|
-
setup
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
# Includes a given +adapter+ into the current class.
|
16
|
-
def include_adapter(adapter)
|
17
|
-
self.class.send :include, adapter
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
end
|
data/spec/fixtures/gzip.gz
DELETED
Binary file
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
require "httpi/interface"
|
3
|
-
|
4
|
-
describe HTTPI::Interface do
|
5
|
-
|
6
|
-
context "when included" do
|
7
|
-
before do
|
8
|
-
@httpi = Class.new { include HTTPI::Interface }.new
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should offer a #load! method for loading an adapter" do
|
12
|
-
@httpi.load!
|
13
|
-
@httpi.client.should be_an(HTTPClient)
|
14
|
-
end
|
15
|
-
|
16
|
-
describe "#load!" do
|
17
|
-
it "should accept an adapter to use" do
|
18
|
-
@httpi.load! :curb
|
19
|
-
@httpi.client.should be_a(Curl::Easy)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should raise an ArgumentError in case of an invalid adapter" do
|
23
|
-
lambda { @httpi.load! :unknown }.should raise_error(ArgumentError)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should call the adapter's #setup method" do
|
27
|
-
@httpi.expects(:setup)
|
28
|
-
@httpi.load!
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
class Some
|
2
|
-
class << self
|
3
|
-
|
4
|
-
def url
|
5
|
-
@url ||= "http://example.com"
|
6
|
-
end
|
7
|
-
|
8
|
-
def proxy_url
|
9
|
-
@proxy_url ||= "http://proxy.example.com"
|
10
|
-
end
|
11
|
-
|
12
|
-
def headers
|
13
|
-
@headers ||= { "Content-Type" => "text/html; charset=utf-8" }
|
14
|
-
end
|
15
|
-
|
16
|
-
def response_code
|
17
|
-
@code ||= 200
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class Fixture
|
24
|
-
class << self
|
25
|
-
|
26
|
-
def xml
|
27
|
-
@xml ||= load :xml
|
28
|
-
end
|
29
|
-
|
30
|
-
def gzip
|
31
|
-
@gzip ||= load :gzip, :gz
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def load(fixture, type = :xml)
|
37
|
-
File.read File.expand_path("../../fixtures/#{fixture}.#{type}", __FILE__)
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
end
|