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,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
@@ -2,31 +2,29 @@ require "spec_helper"
2
2
  require "httpi/response"
3
3
 
4
4
  describe HTTPI::Response do
5
- before do
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
- @response.code.should == Some.response_code
9
+ response.code.should == 200
12
10
  end
13
11
  end
14
12
 
15
- describe "#code" do
13
+ describe "#headers" do
16
14
  it "should return the HTTP response headers" do
17
- @response.headers.should == Some.headers
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
- @response.body.should == "A short gzip encoded message\n"
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
- @response.raw_body.should == Fixture.gzip
27
+ response.raw_body.should == Fixture.gzip
30
28
  end
31
29
  end
32
30
 
@@ -5,5 +5,5 @@ RSpec.configure do |config|
5
5
  config.mock_with :mocha
6
6
  end
7
7
 
8
- require "support/helper_methods"
8
+ require "support/fixture"
9
9
  require "support/matchers"
@@ -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
@@ -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 == Some.response_code
5
- actual.headers.should == Some.headers
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: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.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-12 00:00:00 +02:00
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: 62196421
62
+ hash: 62196431
62
63
  segments:
63
64
  - 2
64
65
  - 0
65
66
  - 0
66
67
  - beta
67
- - 19
68
- version: 2.0.0.beta.19
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/interface.rb
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/gzip.gz
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/interface_spec.rb
116
+ - spec/httpi/request_spec.rb
116
117
  - spec/httpi/response_spec.rb
117
118
  - spec/spec_helper.rb
118
- - spec/support/helper_methods.rb
119
+ - spec/support/fixture.rb
119
120
  - spec/support/matchers.rb
120
121
  - .rspec
121
122
  has_rdoc: true
@@ -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
@@ -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
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