httpi 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,73 @@
1
+ require "uri"
2
+
3
+ module HTTPI
4
+ class Request
5
+
6
+ # Request accessor methods.
7
+ ACCESSORS = [:url, :proxy, :headers, :body, :open_timeout, :read_timeout]
8
+
9
+ # Request authentication methods.
10
+ AUTHENTICATION = [:basic_auth]
11
+
12
+ # Accepts a Hash of +options+ which may contain any number of ACCESSORS and/or
13
+ # AUTHENTICATION credentials to set.
14
+ def initialize(options = {})
15
+ assign_accessors options
16
+ assign_authentication options
17
+ end
18
+
19
+ # Sets the +url+ to access. Raises an +ArgumentError+ unless the +url+ is valid.
20
+ def url=(url)
21
+ @url = normalize_url! url
22
+ end
23
+
24
+ # Returns the +url+ to access.
25
+ attr_reader :url
26
+
27
+ # Sets the +proxy+ to use. Raises an +ArgumentError+ unless the +proxy+ is valid.
28
+ def proxy=(proxy)
29
+ @proxy = normalize_url! proxy
30
+ end
31
+
32
+ # Returns the +proxy+ to use.
33
+ attr_reader :proxy
34
+
35
+ # Returns a Hash of HTTP headers. Defaults to return an empty Hash.
36
+ def headers
37
+ @headers ||= {}
38
+ end
39
+
40
+ # Sets the Hash of HTTP headers.
41
+ attr_writer :headers
42
+
43
+ attr_accessor :body, :open_timeout, :read_timeout
44
+
45
+ # Sets the HTTP basic auth credentials. Accepts an Array or two arguments for the
46
+ # +username+ and +password+. Resets the credentials when +nil+ is passed and returns
47
+ # an Array of credentials when no +args+ where given.
48
+ def basic_auth(*args)
49
+ @basic_auth = extract_credentials @basic_auth, args.flatten
50
+ end
51
+
52
+ private
53
+
54
+ def assign_accessors(options)
55
+ ACCESSORS.each { |a| send("#{a}=", options[a]) if options[a] }
56
+ end
57
+
58
+ def assign_authentication(options)
59
+ AUTHENTICATION.each { |c| send(c, options[c]) if options[c] }
60
+ end
61
+
62
+ def normalize_url!(url)
63
+ raise ArgumentError, "Invalid URL: #{url}" unless url.to_s =~ /^http/
64
+ url.kind_of?(URI) ? url : URI(url)
65
+ end
66
+
67
+ def extract_credentials(credentials, args)
68
+ return unless args.empty? || args.first
69
+ args[1] ? args[0, 2] : credentials
70
+ end
71
+
72
+ end
73
+ end
@@ -1,5 +1,5 @@
1
1
  module HTTPI
2
2
 
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
 
5
5
  end
Binary file
@@ -0,0 +1,10 @@
1
+ <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ex="http://service.example.com/">
2
+ <soapenv:Header/>
3
+ <soapenv:Body>
4
+ <ex:findUser>
5
+ <request>
6
+ <id>1</id>
7
+ </request>
8
+ </ex:findUser>
9
+ </soapenv:Body>
10
+ </soapenv:Envelope>
@@ -1,89 +1,49 @@
1
1
  require "spec_helper"
2
2
  require "httpi/adapter/curb"
3
+ require "httpi/request"
4
+
5
+ require "curb"
3
6
 
4
7
  describe HTTPI::Adapter::Curb do
5
- before do
6
- @adapter = Class.new { include HTTPI::Adapter::Curb }.new
7
- end
8
+ let(:adapter) { HTTPI::Adapter::Curb.new }
8
9
 
9
- describe "#setup" do
10
+ describe ".new" do
10
11
  it "should require the Curb gem" do
11
- @adapter.expects(:require).with("curb")
12
- @adapter.setup
12
+ HTTPI::Adapter::Curb.any_instance.expects(:require).with("curb")
13
+ HTTPI::Adapter::Curb.new
13
14
  end
14
15
  end
15
16
 
16
- context "after #setup:" do
17
- before { @adapter.setup }
18
-
19
- describe "#client" do
20
- it "should return a memoized Curl::Easy instance" do
21
- client = @adapter.client
22
-
23
- client.should be_an(Curl::Easy)
24
- client.should equal(@adapter.client)
25
- end
26
- end
27
-
28
- describe "#headers" do
29
- it "should default to return an empty Hash" do
30
- @adapter.headers.should == {}
31
- end
32
- end
33
-
34
- describe "#headers=" do
35
- it "should set a given Hash of HTTP headers" do
36
- @adapter.headers = Some.headers
37
- @adapter.headers.should == Some.headers
38
- end
17
+ describe "#get" do
18
+ before do
19
+ curb.expects(:http_get)
20
+ curb.expects(:response_code).returns(200)
21
+ curb.expects(:headers).returns(Hash.new)
22
+ curb.expects(:body_str).returns(Fixture.xml)
39
23
  end
40
24
 
41
- describe "#proxy" do
42
- it "should set the proxy server to use" do
43
- @adapter.proxy = Some.proxy_url
44
- @adapter.proxy.should == URI(Some.proxy_url)
45
- end
46
-
47
- it "should accept a URI" do
48
- @adapter.proxy = URI(Some.proxy_url)
49
- @adapter.proxy.should == URI(Some.proxy_url)
50
- end
25
+ it "should return a valid HTTPI::Response" do
26
+ request = HTTPI::Request.new :url => "http://example.com"
27
+ adapter.get(request).should be_a_valid_httpi_response
51
28
  end
29
+ end
52
30
 
53
- describe "#auth" do
54
- it "should set authentication credentials" do
55
- @adapter.client.expects(:username=).with("username")
56
- @adapter.client.expects(:password=).with("password")
57
-
58
- @adapter.auth "username", "password"
59
- end
31
+ describe "#post" do
32
+ before do
33
+ curb.expects(:http_post)
34
+ curb.expects(:response_code).returns(200)
35
+ curb.expects(:headers).returns(Hash.new)
36
+ curb.expects(:body_str).returns(Fixture.xml)
60
37
  end
61
38
 
62
- describe "#get" do
63
- before do
64
- @adapter.client.expects(:http_get)
65
- @adapter.client.expects(:response_code).returns(Some.response_code)
66
- @adapter.client.expects(:headers).returns(Some.headers)
67
- @adapter.client.expects(:body_str).returns(Fixture.xml)
68
- end
69
-
70
- it "should return a valid HTTPI::Response" do
71
- @adapter.get(Some.url).should be_a_valid_httpi_response
72
- end
39
+ it "should return a valid HTTPI::Response" do
40
+ request = HTTPI::Request.new :url => "http://example.com"
41
+ adapter.post(request).should be_a_valid_httpi_response
73
42
  end
43
+ end
74
44
 
75
- describe "#post" do
76
- before do
77
- @adapter.client.expects(:http_post)
78
- @adapter.client.expects(:response_code).returns(Some.response_code)
79
- @adapter.client.expects(:headers).returns(Some.headers)
80
- @adapter.client.expects(:body_str).returns(Fixture.xml)
81
- end
82
-
83
- it "should return a valid HTTPI::Response" do
84
- @adapter.post(Some.url, Fixture.xml).should be_a_valid_httpi_response
85
- end
86
- end
45
+ def curb
46
+ Curl::Easy.any_instance
87
47
  end
88
48
 
89
49
  end
@@ -1,86 +1,45 @@
1
1
  require "spec_helper"
2
2
  require "httpi/adapter/httpclient"
3
+ require "httpi/request"
4
+
5
+ require "httpclient"
3
6
 
4
7
  describe HTTPI::Adapter::HTTPClient do
5
- before do
6
- @adapter = Class.new { include HTTPI::Adapter::HTTPClient }.new
7
- end
8
+ let(:adapter) { HTTPI::Adapter::HTTPClient.new }
8
9
 
9
- describe "#setup" do
10
+ describe ".new" do
10
11
  it "should require the HTTPClient gem" do
11
- @adapter.expects(:require).with("httpclient")
12
- @adapter.setup
12
+ HTTPI::Adapter::HTTPClient.any_instance.expects(:require).with("httpclient")
13
+ HTTPI::Adapter::HTTPClient.new
13
14
  end
14
15
  end
15
16
 
16
- context "after #setup:" do
17
- before { @adapter.setup }
18
-
19
- describe "#client" do
20
- it "should return a memoized HTTPClient instance" do
21
- client = @adapter.client
22
-
23
- client.should be_an(HTTPClient)
24
- client.should equal(@adapter.client)
25
- end
17
+ describe "#get" do
18
+ before do
19
+ @request = HTTPI::Request.new :url => "http://example.com"
20
+ response = HTTP::Message.new_response Fixture.xml
21
+ httpclient.expects(:get).with(@request.url, nil, @request.headers).returns(response)
26
22
  end
27
23
 
28
- describe "#headers" do
29
- it "should default to return an empty Hash" do
30
- @adapter.headers.should == {}
31
- end
32
- end
33
-
34
- describe "#headers=" do
35
- it "should set a given Hash of HTTP headers" do
36
- @adapter.headers = Some.headers
37
- @adapter.headers.should == Some.headers
38
- end
39
- end
40
-
41
- describe "#proxy" do
42
- it "should set the proxy server to use" do
43
- @adapter.proxy = Some.proxy_url
44
- @adapter.proxy.should == URI(Some.proxy_url)
45
- end
46
-
47
- it "should accept a URI" do
48
- @adapter.proxy = URI(Some.proxy_url)
49
- @adapter.proxy.should == URI(Some.proxy_url)
50
- end
24
+ it "should return a valid HTTPI::Response" do
25
+ adapter.get(@request).should be_a_valid_httpi_response
51
26
  end
27
+ end
52
28
 
53
- describe "#auth" do
54
- it "should set authentication credentials" do
55
- @adapter.client.expects(:set_auth).with(nil, "username", "password")
56
- @adapter.auth "username", "password"
57
- end
29
+ describe "#post" do
30
+ before do
31
+ @request = HTTPI::Request.new :url => "http://example.com", :body => Fixture.xml
32
+ response = HTTP::Message.new_response Fixture.xml
33
+ httpclient.expects(:post).with(@request.url, @request.body, @request.headers).returns(response)
58
34
  end
59
35
 
60
- describe "#get" do
61
- before do
62
- response = HTTP::Message.new_response Fixture.xml
63
- response.header.add *Some.headers.to_a.first
64
- @adapter.client.expects(:get).with(Some.url).returns(response)
65
- end
66
-
67
- it "should return a valid HTTPI::Response" do
68
- @adapter.get(Some.url).should be_a_valid_httpi_response
69
- end
36
+ it "should return a valid HTTPI::Response" do
37
+ adapter.post(@request).should be_a_valid_httpi_response
70
38
  end
39
+ end
71
40
 
72
- describe "#post" do
73
- before do
74
- response = HTTP::Message.new_response Fixture.xml
75
- response.header.add *Some.headers.to_a.first
76
- @adapter.headers = Some.headers
77
- @adapter.client.expects(:post).with(Some.url, Fixture.xml, Some.headers).returns(response)
78
- end
79
-
80
- it "should return a valid HTTPI::Response" do
81
- @adapter.post(Some.url, Fixture.xml).should be_a_valid_httpi_response
82
- end
83
- end
41
+ def httpclient
42
+ HTTPClient.any_instance
84
43
  end
85
44
 
86
45
  end
@@ -2,31 +2,30 @@ require "spec_helper"
2
2
  require "httpi/adapter"
3
3
 
4
4
  describe HTTPI::Adapter do
5
+ let(:adapter) { HTTPI::Adapter }
5
6
 
6
7
  describe ".use" do
7
8
  it "should default to HTTPClient" do
8
- HTTPI::Adapter.use.should == :httpclient
9
+ adapter.use.should == :httpclient
9
10
  end
10
- end
11
-
12
- describe ".use=" do
11
+
13
12
  it "should accept an adapter to use" do
14
- HTTPI::Adapter.use = :curb
15
- HTTPI::Adapter.use.should == :curb
13
+ adapter.use = :curb
14
+ adapter.use.should == :curb
16
15
 
17
16
  # reset to default
18
- HTTPI::Adapter.use = HTTPI::Adapter::DEFAULT
17
+ adapter.use = HTTPI::Adapter::DEFAULT
19
18
  end
20
19
 
21
20
  it "should raise an ArgumentError in case of an invalid adapter" do
22
- lambda { HTTPI::Adapter.use = :unknown }.should raise_error(ArgumentError)
21
+ lambda { adapter.use = :unknown }.should raise_error(ArgumentError)
23
22
  end
24
23
  end
25
24
 
26
25
  describe ".adapters" do
27
26
  it "should return a memoized Hash of adapters" do
28
- HTTPI::Adapter.adapters.should have(2).items
29
- HTTPI::Adapter.adapters.should include(
27
+ adapter.adapters.should have(2).items
28
+ adapter.adapters.should include(
30
29
  :httpclient => HTTPI::Adapter::HTTPClient,
31
30
  :curb => HTTPI::Adapter::Curb
32
31
  )
@@ -35,11 +34,11 @@ describe HTTPI::Adapter do
35
34
 
36
35
  describe ".find" do
37
36
  it "should return the adapter for a given Symbol" do
38
- HTTPI::Adapter.find(:httpclient).should == HTTPI::Adapter::HTTPClient
37
+ adapter.find(:httpclient).should == HTTPI::Adapter::HTTPClient
39
38
  end
40
39
 
41
40
  it "should raise an ArgumentError in case of an invalid adapter" do
42
- lambda { HTTPI::Adapter.find :unknown }.should raise_error(ArgumentError)
41
+ lambda { adapter.find :unknown }.should raise_error(ArgumentError)
43
42
  end
44
43
  end
45
44
 
@@ -2,20 +2,101 @@ require "spec_helper"
2
2
  require "httpi"
3
3
 
4
4
  describe HTTPI::Client do
5
+ let(:client) { HTTPI::Client }
6
+ let(:default_adapter) { HTTPI::Adapter.find HTTPI::Adapter.use }
7
+ let(:curb) { HTTPI::Adapter.find :curb }
5
8
 
6
- describe ".new" do
7
- it "should default to use HTTPI::Adapter::DEFAULT" do
8
- @httpi = HTTPI::Client.new
9
- @httpi.client.should be_an(HTTPClient)
9
+ describe ".get(request)" do
10
+ it "should execute an HTTP GET request using the default adapter" do
11
+ request = HTTPI::Request.new
12
+ default_adapter.any_instance.expects(:get).with(request)
13
+
14
+ client.get request
10
15
  end
16
+ end
11
17
 
12
- it "should accept an adapter to use" do
13
- @httpi = HTTPI::Client.new :curb
14
- @httpi.client.should be_a(Curl::Easy)
18
+ describe ".get(request, adapter)" do
19
+ it "should execute an HTTP GET request using the given adapter" do
20
+ request = HTTPI::Request.new
21
+ curb.any_instance.expects(:get).with(request)
22
+
23
+ client.get request, :curb
15
24
  end
25
+ end
16
26
 
27
+ describe ".get(url)" do
28
+ it "should execute an HTTP GET request using the default adapter" do
29
+ HTTPI::Request.any_instance.expects(:url=).with("http://example.com")
30
+ default_adapter.any_instance.expects(:get).with(instance_of(HTTPI::Request))
31
+
32
+ client.get "http://example.com"
33
+ end
34
+ end
35
+
36
+ describe ".get(url, adapter)" do
37
+ it "should execute an HTTP GET request using the given adapter" do
38
+ HTTPI::Request.any_instance.expects(:url=).with("http://example.com")
39
+ curb.any_instance.expects(:get).with(instance_of(HTTPI::Request))
40
+
41
+ client.get "http://example.com", :curb
42
+ end
43
+ end
44
+
45
+ describe ".get" do
17
46
  it "should raise an ArgumentError in case of an invalid adapter" do
18
- lambda { HTTPI::Client.new :unknown }.should raise_error(ArgumentError)
47
+ lambda { client.get HTTPI::Request.new, :invalid }.should raise_error(ArgumentError)
48
+ end
49
+
50
+ it "should raise an ArgumentError in case of an invalid URL" do
51
+ lambda { client.get "invalid" }.should raise_error(ArgumentError)
52
+ end
53
+ end
54
+
55
+ describe ".post(request)" do
56
+ it "should execute an HTTP POST request using the default adapter" do
57
+ request = HTTPI::Request.new
58
+ default_adapter.any_instance.expects(:post).with(request)
59
+
60
+ client.post request
61
+ end
62
+ end
63
+
64
+ describe ".post(request, adapter)" do
65
+ it "should execute an HTTP POST request using the given adapter" do
66
+ request = HTTPI::Request.new
67
+ curb.any_instance.expects(:post).with(request)
68
+
69
+ client.post request, :curb
70
+ end
71
+ end
72
+
73
+ describe ".post(url, body)" do
74
+ it "should execute an HTTP POST request using the default adapter" do
75
+ HTTPI::Request.any_instance.expects(:url=).with("http://example.com")
76
+ HTTPI::Request.any_instance.expects(:body=).with("<some>xml</some>")
77
+ default_adapter.any_instance.expects(:post).with(instance_of(HTTPI::Request))
78
+
79
+ client.post "http://example.com", "<some>xml</some>"
80
+ end
81
+ end
82
+
83
+ describe ".post(url, body, adapter)" do
84
+ it "should execute an HTTP POST request using the given adapter" do
85
+ HTTPI::Request.any_instance.expects(:url=).with("http://example.com")
86
+ HTTPI::Request.any_instance.expects(:body=).with("<some>xml</some>")
87
+ curb.any_instance.expects(:post).with(instance_of(HTTPI::Request))
88
+
89
+ client.post "http://example.com", "<some>xml</some>", :curb
90
+ end
91
+ end
92
+
93
+ describe ".post" do
94
+ it "should raise an ArgumentError in case of an invalid adapter" do
95
+ lambda { client.post HTTPI::Request.new, :invalid }.should raise_error(ArgumentError)
96
+ end
97
+
98
+ it "should raise an ArgumentError in case of an invalid URL" do
99
+ lambda { client.post "invalid" }.should raise_error(ArgumentError)
19
100
  end
20
101
  end
21
102