regenersis-httpi 0.9.6
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/.autotest +5 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +69 -0
- data/Gemfile +9 -0
- data/LICENSE +20 -0
- data/README.md +223 -0
- data/Rakefile +18 -0
- data/autotest/discover.rb +1 -0
- data/lib/httpi.rb +198 -0
- data/lib/httpi/adapter.rb +67 -0
- data/lib/httpi/adapter/curb.rb +125 -0
- data/lib/httpi/adapter/httpclient.rb +98 -0
- data/lib/httpi/adapter/net_http.rb +117 -0
- data/lib/httpi/auth/config.rb +81 -0
- data/lib/httpi/auth/ssl.rb +91 -0
- data/lib/httpi/dime.rb +56 -0
- data/lib/httpi/request.rb +90 -0
- data/lib/httpi/response.rb +85 -0
- data/lib/httpi/version.rb +5 -0
- data/lib/regenersis-httpi.rb +1 -0
- data/regenersis-httpi.gemspec +26 -0
- data/spec/fixtures/attachment.gif +0 -0
- data/spec/fixtures/client_cert.pem +16 -0
- data/spec/fixtures/client_key.pem +15 -0
- data/spec/fixtures/xml.gz +0 -0
- data/spec/fixtures/xml.xml +10 -0
- data/spec/fixtures/xml_dime.dime +0 -0
- data/spec/fixtures/xml_dime.xml +1 -0
- data/spec/httpi/adapter/curb_spec.rb +232 -0
- data/spec/httpi/adapter/httpclient_spec.rb +164 -0
- data/spec/httpi/adapter/net_http_spec.rb +142 -0
- data/spec/httpi/adapter_spec.rb +55 -0
- data/spec/httpi/auth/config_spec.rb +117 -0
- data/spec/httpi/auth/ssl_spec.rb +128 -0
- data/spec/httpi/httpi_spec.rb +284 -0
- data/spec/httpi/request_spec.rb +135 -0
- data/spec/httpi/response_spec.rb +125 -0
- data/spec/integration/request_spec.rb +95 -0
- data/spec/integration/server.rb +39 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/fixture.rb +27 -0
- data/spec/support/matchers.rb +19 -0
- metadata +185 -0
@@ -0,0 +1,125 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "httpi/response"
|
3
|
+
|
4
|
+
describe HTTPI::Response do
|
5
|
+
|
6
|
+
context "normal" do
|
7
|
+
let(:response) { HTTPI::Response.new 200, {}, Fixture.xml }
|
8
|
+
|
9
|
+
describe "#error?" do
|
10
|
+
it "returns false" do
|
11
|
+
response.should_not be_an_error
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#headers" do
|
16
|
+
it "returns the HTTP response headers" do
|
17
|
+
response.headers.should == {}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#code" do
|
22
|
+
it "returns the HTTP response code" do
|
23
|
+
response.code.should == 200
|
24
|
+
end
|
25
|
+
|
26
|
+
it "always returns an Integer" do
|
27
|
+
response = HTTPI::Response.new "200", {}, ""
|
28
|
+
response.code.should == 200
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#multipart" do
|
33
|
+
it "returns false" do
|
34
|
+
response.should_not be_multipart
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "empty" do
|
40
|
+
let(:response) { HTTPI::Response.new 204, {}, nil }
|
41
|
+
|
42
|
+
describe "#body" do
|
43
|
+
it "returns an empty String" do
|
44
|
+
response.body.should == ""
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "multipart" do
|
50
|
+
let(:response) { HTTPI::Response.new 200, { "Content-Type" => "multipart/related" }, "multipart" }
|
51
|
+
|
52
|
+
describe "#multipart" do
|
53
|
+
it "returns true" do
|
54
|
+
response.should be_multipart
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "error" do
|
60
|
+
let(:response) { HTTPI::Response.new 404, {}, "" }
|
61
|
+
|
62
|
+
describe "#error?" do
|
63
|
+
it "returns true" do
|
64
|
+
response.should be_an_error
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "gzipped" do
|
70
|
+
let(:response) { HTTPI::Response.new 200, { "Content-Encoding" => "gzip" }, Fixture.gzip }
|
71
|
+
|
72
|
+
describe "#headers" do
|
73
|
+
it "returns the HTTP response headers" do
|
74
|
+
response.headers.should == { "Content-Encoding" => "gzip" }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#body" do
|
79
|
+
it "returns the (gzip decoded) HTTP response body" do
|
80
|
+
response.body.should == Fixture.xml
|
81
|
+
end
|
82
|
+
|
83
|
+
it "bubbles Zlib errors" do
|
84
|
+
arbitrary_error = Class.new(ArgumentError)
|
85
|
+
Zlib::GzipReader.expects(:new).raises(arbitrary_error)
|
86
|
+
expect { response.body }.to raise_error(arbitrary_error)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "#raw_body" do
|
91
|
+
it "returns the raw HTML response body" do
|
92
|
+
response.raw_body.should == Fixture.gzip
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context "DIME" do
|
98
|
+
let(:response) { HTTPI::Response.new 200, { "Content-Type" => "application/dime" }, Fixture.dime }
|
99
|
+
|
100
|
+
describe "#headers" do
|
101
|
+
it "returns the HTTP response headers" do
|
102
|
+
response.headers.should == { "Content-Type" => "application/dime" }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "#body" do
|
107
|
+
it "returns the (dime decoded) HTTP response body" do
|
108
|
+
response.body.should == Fixture.xml_dime
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "#raw_body" do
|
113
|
+
it "returns the raw HTML response body" do
|
114
|
+
response.raw_body.should == Fixture.dime
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "#attachments" do
|
119
|
+
it "returns proper attachment when given a dime response" do
|
120
|
+
response.attachments.first.data == File.read(File.expand_path("../../fixtures/attachment.gif", __FILE__))
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "httpi"
|
3
|
+
require "integration/server"
|
4
|
+
|
5
|
+
MockServer.new(IntegrationServer, 4000).start
|
6
|
+
|
7
|
+
describe HTTPI do
|
8
|
+
let(:client) { HTTPI }
|
9
|
+
|
10
|
+
before :all do
|
11
|
+
WebMock.allow_net_connect!
|
12
|
+
|
13
|
+
@username = "admin"
|
14
|
+
@password = "pwd"
|
15
|
+
@error_message = "Authorization Required"
|
16
|
+
@example_web_page = "Hello"
|
17
|
+
@content_type = "text/plain"
|
18
|
+
end
|
19
|
+
|
20
|
+
shared_examples_for "an HTTP client" do
|
21
|
+
it "and send HTTP headers" do
|
22
|
+
request = HTTPI::Request.new :url => "http://localhost:4000/x-header"
|
23
|
+
request.headers["X-Header"] = "HTTPI"
|
24
|
+
|
25
|
+
response = HTTPI.get request, adapter
|
26
|
+
response.body.should include("X-Header is HTTPI")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "and execute an HTTP GET request" do
|
30
|
+
response = HTTPI.get "http://localhost:4000", adapter
|
31
|
+
response.body.should include(@example_web_page)
|
32
|
+
response.headers["Content-Type"].should include(@content_type)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "and execute an HTTP POST request" do
|
36
|
+
response = HTTPI.post "http://localhost:4000", "<some>xml</some>", adapter
|
37
|
+
response.body.should include(@example_web_page)
|
38
|
+
response.headers["Content-Type"].should include(@content_type)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "and execute an HTTP HEAD request" do
|
42
|
+
response = HTTPI.head "http://localhost:4000", adapter
|
43
|
+
response.code.should == 200
|
44
|
+
response.headers["Content-Type"].should include(@content_type)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "and execute an HTTP PUT request" do
|
48
|
+
response = HTTPI.put "http://localhost:4000", "<some>xml</some>", adapter
|
49
|
+
response.body.should include("PUT is not allowed")
|
50
|
+
response.headers["Content-Type"].should include(@content_type)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "and execute an HTTP DELETE request" do
|
54
|
+
response = HTTPI.delete "http://localhost:4000", adapter
|
55
|
+
response.body.should include("DELETE is not allowed")
|
56
|
+
response.headers["Content-Type"].should include(@content_type)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
shared_examples_for "it works with HTTP basic auth" do
|
61
|
+
it "and access a secured page" do
|
62
|
+
request = HTTPI::Request.new :url => "http://localhost:4000/auth/basic"
|
63
|
+
request.auth.basic @username, @password
|
64
|
+
|
65
|
+
response = HTTPI.get request, adapter
|
66
|
+
response.body.should_not include(@error_message)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
shared_examples_for "it works with HTTP digest auth" do
|
71
|
+
it "and access a secured page" do
|
72
|
+
request = HTTPI::Request.new :url => "http://localhost:4000/auth/digest"
|
73
|
+
request.auth.digest @username, @password
|
74
|
+
|
75
|
+
response = HTTPI.get request, adapter
|
76
|
+
response.body.should_not include(@error_message)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
HTTPI::Adapter::ADAPTERS.keys.each do |adapter|
|
81
|
+
context "using :#{adapter}" do
|
82
|
+
let(:adapter) { adapter }
|
83
|
+
it_should_behave_like "an HTTP client"
|
84
|
+
it_should_behave_like "it works with HTTP basic auth"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
(HTTPI::Adapter::ADAPTERS.keys - [:net_http]).each do |adapter|
|
89
|
+
context "using :#{adapter}" do
|
90
|
+
let(:adapter) { adapter }
|
91
|
+
it_should_behave_like "it works with HTTP digest auth"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "mock_server"
|
3
|
+
|
4
|
+
IntegrationServer = Rack::Builder.new do
|
5
|
+
map "/" do
|
6
|
+
run lambda {|env|
|
7
|
+
case env["REQUEST_METHOD"]
|
8
|
+
when "HEAD" then
|
9
|
+
[200, {"Content-Type" => "text/plain", "Content-Length" => "5"}, []]
|
10
|
+
when "GET", "POST" then
|
11
|
+
[200, {"Content-Type" => "text/plain", "Content-Length" => "5"}, ["Hello"]]
|
12
|
+
when "PUT", "DELETE"
|
13
|
+
body = "#{env["REQUEST_METHOD"]} is not allowed"
|
14
|
+
[200, {"Content-Type" => "text/plain", "Content-Length" => body.size.to_s}, [body]]
|
15
|
+
end
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
map "/x-header" do
|
20
|
+
run lambda {|env|
|
21
|
+
body = "X-Header is #{env["HTTP_X_HEADER"]}"
|
22
|
+
[200, {"Content-Type" => "text/plain", "Content-Length" => body.size.to_s}, [body]]
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
map "/auth" do
|
27
|
+
map "/basic" do
|
28
|
+
run Rack::Auth::Basic do |user, password|
|
29
|
+
user == "admin" && password == "secret"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
map "/digest" do
|
34
|
+
run Rack::Auth::Digest::MD5 do |username|
|
35
|
+
{"admin" => "pwd"}[username]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
class Fixture
|
2
|
+
class << self
|
3
|
+
|
4
|
+
def xml
|
5
|
+
@xml ||= load :xml
|
6
|
+
end
|
7
|
+
|
8
|
+
def xml_dime
|
9
|
+
@xml_dime ||= load :xml_dime
|
10
|
+
end
|
11
|
+
|
12
|
+
def gzip
|
13
|
+
@gzip ||= load :xml, :gz
|
14
|
+
end
|
15
|
+
|
16
|
+
def dime
|
17
|
+
@dime ||= load :xml_dime, :dime
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def load(fixture, type = :xml)
|
23
|
+
File.read File.expand_path("../../fixtures/#{fixture}.#{type}", __FILE__)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
RSpec::Matchers.define :match_response do |options|
|
2
|
+
defaults = { :code => 200, :headers => { "Accept-encoding" => "utf-8" }, :body => "" }
|
3
|
+
response = defaults.merge options
|
4
|
+
|
5
|
+
match do |actual|
|
6
|
+
actual.should be_an(HTTPI::Response)
|
7
|
+
actual.code.should == response[:code]
|
8
|
+
downcase(actual.headers).should == downcase(response[:headers])
|
9
|
+
actual.body.should == response[:body]
|
10
|
+
end
|
11
|
+
|
12
|
+
def downcase(hash)
|
13
|
+
hash.inject({}) do |memo, (key, value)|
|
14
|
+
memo[key.downcase] = value.downcase
|
15
|
+
memo
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: regenersis-httpi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 55
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 9
|
9
|
+
- 6
|
10
|
+
version: 0.9.6
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Daniel Harrington
|
14
|
+
- Martin Tepper
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-10-11 00:00:00 +01:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: rack
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 7
|
45
|
+
segments:
|
46
|
+
- 2
|
47
|
+
- 2
|
48
|
+
version: "2.2"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: autotest
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: mocha
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ~>
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 41
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
- 9
|
77
|
+
- 9
|
78
|
+
version: 0.9.9
|
79
|
+
type: :development
|
80
|
+
version_requirements: *id004
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: webmock
|
83
|
+
prerelease: false
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 7
|
90
|
+
segments:
|
91
|
+
- 1
|
92
|
+
- 4
|
93
|
+
- 0
|
94
|
+
version: 1.4.0
|
95
|
+
type: :development
|
96
|
+
version_requirements: *id005
|
97
|
+
description: HTTPI provides a common interface for Ruby HTTP libraries.
|
98
|
+
email: me@rubiii.com
|
99
|
+
executables: []
|
100
|
+
|
101
|
+
extensions: []
|
102
|
+
|
103
|
+
extra_rdoc_files: []
|
104
|
+
|
105
|
+
files:
|
106
|
+
- .autotest
|
107
|
+
- .gitignore
|
108
|
+
- .rspec
|
109
|
+
- .travis.yml
|
110
|
+
- CHANGELOG.md
|
111
|
+
- Gemfile
|
112
|
+
- LICENSE
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- autotest/discover.rb
|
116
|
+
- lib/httpi.rb
|
117
|
+
- lib/httpi/adapter.rb
|
118
|
+
- lib/httpi/adapter/curb.rb
|
119
|
+
- lib/httpi/adapter/httpclient.rb
|
120
|
+
- lib/httpi/adapter/net_http.rb
|
121
|
+
- lib/httpi/auth/config.rb
|
122
|
+
- lib/httpi/auth/ssl.rb
|
123
|
+
- lib/httpi/dime.rb
|
124
|
+
- lib/httpi/request.rb
|
125
|
+
- lib/httpi/response.rb
|
126
|
+
- lib/httpi/version.rb
|
127
|
+
- lib/regenersis-httpi.rb
|
128
|
+
- regenersis-httpi.gemspec
|
129
|
+
- spec/fixtures/attachment.gif
|
130
|
+
- spec/fixtures/client_cert.pem
|
131
|
+
- spec/fixtures/client_key.pem
|
132
|
+
- spec/fixtures/xml.gz
|
133
|
+
- spec/fixtures/xml.xml
|
134
|
+
- spec/fixtures/xml_dime.dime
|
135
|
+
- spec/fixtures/xml_dime.xml
|
136
|
+
- spec/httpi/adapter/curb_spec.rb
|
137
|
+
- spec/httpi/adapter/httpclient_spec.rb
|
138
|
+
- spec/httpi/adapter/net_http_spec.rb
|
139
|
+
- spec/httpi/adapter_spec.rb
|
140
|
+
- spec/httpi/auth/config_spec.rb
|
141
|
+
- spec/httpi/auth/ssl_spec.rb
|
142
|
+
- spec/httpi/httpi_spec.rb
|
143
|
+
- spec/httpi/request_spec.rb
|
144
|
+
- spec/httpi/response_spec.rb
|
145
|
+
- spec/integration/request_spec.rb
|
146
|
+
- spec/integration/server.rb
|
147
|
+
- spec/spec_helper.rb
|
148
|
+
- spec/support/fixture.rb
|
149
|
+
- spec/support/matchers.rb
|
150
|
+
has_rdoc: true
|
151
|
+
homepage: http://github.com/rubiii/httpi
|
152
|
+
licenses: []
|
153
|
+
|
154
|
+
post_install_message:
|
155
|
+
rdoc_options: []
|
156
|
+
|
157
|
+
require_paths:
|
158
|
+
- lib
|
159
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
160
|
+
none: false
|
161
|
+
requirements:
|
162
|
+
- - ">="
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
hash: 3
|
165
|
+
segments:
|
166
|
+
- 0
|
167
|
+
version: "0"
|
168
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
169
|
+
none: false
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
hash: 3
|
174
|
+
segments:
|
175
|
+
- 0
|
176
|
+
version: "0"
|
177
|
+
requirements: []
|
178
|
+
|
179
|
+
rubyforge_project: regenersis-httpi
|
180
|
+
rubygems_version: 1.3.7
|
181
|
+
signing_key:
|
182
|
+
specification_version: 3
|
183
|
+
summary: Interface for Ruby HTTP libraries
|
184
|
+
test_files: []
|
185
|
+
|