patron 0.3.2
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/LICENSE +19 -0
- data/README.txt +56 -0
- data/Rakefile +150 -0
- data/ext/patron/extconf.rb +43 -0
- data/ext/patron/session_ext.c +357 -0
- data/lib/patron.rb +42 -0
- data/lib/patron/error.rb +55 -0
- data/lib/patron/request.rb +90 -0
- data/lib/patron/response.rb +57 -0
- data/lib/patron/session.rb +114 -0
- data/spec/patron_spec.rb +38 -0
- data/spec/request_spec.rb +75 -0
- data/spec/session_spec.rb +134 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +36 -0
- metadata +81 -0
@@ -0,0 +1,134 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) 2008 The Hive http://www.thehive.com/
|
4
|
+
##
|
5
|
+
## Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
## of this software and associated documentation files (the "Software"), to deal
|
7
|
+
## in the Software without restriction, including without limitation the rights
|
8
|
+
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
## copies of the Software, and to permit persons to whom the Software is
|
10
|
+
## furnished to do so, subject to the following conditions:
|
11
|
+
##
|
12
|
+
## The above copyright notice and this permission notice shall be included in
|
13
|
+
## all copies or substantial portions of the Software.
|
14
|
+
##
|
15
|
+
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
## THE SOFTWARE.
|
22
|
+
##
|
23
|
+
## -------------------------------------------------------------------
|
24
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
25
|
+
require 'webrick'
|
26
|
+
require 'base64'
|
27
|
+
|
28
|
+
describe Patron::Session do
|
29
|
+
|
30
|
+
before(:each) do
|
31
|
+
@session = Patron::Session.new
|
32
|
+
@session.base_url = "http://localhost:9001"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should escape and unescape strings symetrically" do
|
36
|
+
string = "foo~bar baz/"
|
37
|
+
escaped = @session.escape(string)
|
38
|
+
unescaped = @session.unescape(escaped)
|
39
|
+
unescaped.should == string
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should raise an error when no URL is provided" do
|
43
|
+
@session.base_url = nil
|
44
|
+
lambda {@session.get(nil)}.should raise_error(ArgumentError)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should retrieve a url with :get" do
|
48
|
+
response = @session.get("/test")
|
49
|
+
body = YAML::load(response.body)
|
50
|
+
body.request_method.should == "GET"
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should include custom headers in a request" do
|
54
|
+
response = @session.get("/test", {"User-Agent" => "PatronTest"})
|
55
|
+
body = YAML::load(response.body)
|
56
|
+
body.header["user-agent"].should == ["PatronTest"]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should merge custom headers with session headers" do
|
60
|
+
@session.headers["X-Test"] = "Testing"
|
61
|
+
response = @session.get("/test", {"User-Agent" => "PatronTest"})
|
62
|
+
body = YAML::load(response.body)
|
63
|
+
body.header["user-agent"].should == ["PatronTest"]
|
64
|
+
body.header["x-test"].should == ["Testing"]
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should raise an exception on timeout" do
|
68
|
+
@session.timeout = 1
|
69
|
+
lambda {@session.get("/timeout")}.should raise_error(Patron::TimeoutError)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should follow redirects by default" do
|
73
|
+
@session.max_redirects = 1
|
74
|
+
response = @session.get("/redirect")
|
75
|
+
body = YAML::load(response.body)
|
76
|
+
response.status.should == 200
|
77
|
+
body.path.should == "/test"
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should include redirect count in response" do
|
81
|
+
@session.max_redirects = 1
|
82
|
+
response = @session.get("/redirect")
|
83
|
+
response.redirect_count.should == 1
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should not follow redirects when configured to do so" do
|
87
|
+
@session.max_redirects = 0
|
88
|
+
response = @session.get("/redirect")
|
89
|
+
response.status.should == 301
|
90
|
+
response.body.should be_empty
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should retrieve URL metadata with :head" do
|
94
|
+
response = @session.head("/test")
|
95
|
+
response.status.should == 200
|
96
|
+
response.body.should be_empty
|
97
|
+
response.headers.should_not be_empty
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should send a delete request with :delete" do
|
101
|
+
response = @session.delete("/test")
|
102
|
+
body = YAML::load(response.body)
|
103
|
+
body.request_method.should == "DELETE"
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should upload data with :put" do
|
107
|
+
data = "upload data"
|
108
|
+
response = @session.put("/test", data)
|
109
|
+
body = YAML::load(response.body)
|
110
|
+
body.request_method.should == "PUT"
|
111
|
+
body.header['content-length'].should == [data.size.to_s]
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should upload data with :post" do
|
115
|
+
data = "upload data"
|
116
|
+
response = @session.post("/test", data)
|
117
|
+
body = YAML::load(response.body)
|
118
|
+
body.request_method.should == "POST"
|
119
|
+
body.header['content-length'].should == [data.size.to_s]
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should pass credentials as http basic auth" do
|
123
|
+
@session.username = "foo"
|
124
|
+
@session.password = "bar"
|
125
|
+
response = @session.get("/test")
|
126
|
+
body = YAML::load(response.body)
|
127
|
+
body.header['authorization'].should == [encode_authz("foo", "bar")]
|
128
|
+
end
|
129
|
+
|
130
|
+
def encode_authz(user, passwd)
|
131
|
+
"Basic " + Base64.encode64("#{user}:#{passwd}").strip
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) 2008 The Hive http://www.thehive.com/
|
4
|
+
##
|
5
|
+
## Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
## of this software and associated documentation files (the "Software"), to deal
|
7
|
+
## in the Software without restriction, including without limitation the rights
|
8
|
+
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
## copies of the Software, and to permit persons to whom the Software is
|
10
|
+
## furnished to do so, subject to the following conditions:
|
11
|
+
##
|
12
|
+
## The above copyright notice and this permission notice shall be included in
|
13
|
+
## all copies or substantial portions of the Software.
|
14
|
+
##
|
15
|
+
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
## THE SOFTWARE.
|
22
|
+
##
|
23
|
+
## -------------------------------------------------------------------
|
24
|
+
require 'test/unit'
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'spec'
|
28
|
+
rescue LoadError
|
29
|
+
require 'rubygems'
|
30
|
+
gem 'rspec'
|
31
|
+
require 'spec'
|
32
|
+
end
|
33
|
+
|
34
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
35
|
+
$:.unshift(File.dirname(__FILE__) + '/../ext')
|
36
|
+
require 'patron'
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: patron
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Phillip Toland
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-03 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Ruby HTTP client library based on libcurl
|
17
|
+
email: phil.toland@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions:
|
21
|
+
- ext/patron/extconf.rb
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.txt
|
24
|
+
files:
|
25
|
+
- LICENSE
|
26
|
+
- README.txt
|
27
|
+
- Rakefile
|
28
|
+
- ext/patron/extconf.rb
|
29
|
+
- ext/patron/session_ext.c
|
30
|
+
- lib/patron.rb
|
31
|
+
- lib/patron/error.rb
|
32
|
+
- lib/patron/request.rb
|
33
|
+
- lib/patron/response.rb
|
34
|
+
- lib/patron/session.rb
|
35
|
+
- spec/patron_spec.rb
|
36
|
+
- spec/request_spec.rb
|
37
|
+
- spec/session_spec.rb
|
38
|
+
- spec/spec.opts
|
39
|
+
- spec/spec_helper.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/toland/Patron
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --quiet
|
47
|
+
- --title
|
48
|
+
- Patron documentation
|
49
|
+
- --opname
|
50
|
+
- index.html
|
51
|
+
- --line-numbers
|
52
|
+
- --main
|
53
|
+
- README.txt
|
54
|
+
- --inline-source
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
- ext
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: patron
|
73
|
+
rubygems_version: 1.3.2
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Patron HTTP client
|
77
|
+
test_files:
|
78
|
+
- spec/patron_spec.rb
|
79
|
+
- spec/request_spec.rb
|
80
|
+
- spec/session_spec.rb
|
81
|
+
- spec/spec_helper.rb
|