curb-fu 0.4.4 → 0.6.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/lib/curb-fu.rb +8 -10
- data/lib/curb-fu/core_ext.rb +13 -10
- data/lib/curb-fu/request/base.rb +14 -20
- data/lib/curb-fu/request/parameter.rb +3 -3
- data/lib/curb-fu/response.rb +29 -2
- data/spec/lib/curb-fu/core_ext_spec.rb +22 -44
- data/spec/lib/curb-fu/request/test_spec.rb +5 -5
- data/spec/lib/curb-fu/response_spec.rb +104 -0
- data/spec/spec_helper.rb +1 -2
- metadata +39 -25
- data/lib/curb-fu/entity.rb +0 -54
- data/spec/lib/curb-fu/entity_spec.rb +0 -37
data/lib/curb-fu.rb
CHANGED
@@ -2,27 +2,25 @@ dir = File.dirname(__FILE__)
|
|
2
2
|
$:.unshift(dir) unless $:.include?(dir)
|
3
3
|
require 'curb-fu/response'
|
4
4
|
require 'curb-fu/request'
|
5
|
-
require 'curb-fu/entity'
|
6
5
|
require 'curb-fu/authentication'
|
7
6
|
require 'curb-fu/core_ext'
|
8
|
-
require 'curb-fu/test'
|
9
7
|
|
10
8
|
module CurbFu
|
11
9
|
class << self
|
12
|
-
def get(*args)
|
13
|
-
CurbFu::Request.get(*args)
|
10
|
+
def get(*args, &block)
|
11
|
+
CurbFu::Request.get(*args, &block)
|
14
12
|
end
|
15
13
|
|
16
|
-
def post(*args)
|
17
|
-
CurbFu::Request.post(*args)
|
14
|
+
def post(*args, &block)
|
15
|
+
CurbFu::Request.post(*args, &block)
|
18
16
|
end
|
19
17
|
|
20
|
-
def put(*args)
|
21
|
-
CurbFu::Request.put(*args)
|
18
|
+
def put(*args, &block)
|
19
|
+
CurbFu::Request.put(*args, &block)
|
22
20
|
end
|
23
21
|
|
24
|
-
def delete(*args)
|
25
|
-
CurbFu::Request.delete(*args)
|
22
|
+
def delete(*args, &block)
|
23
|
+
CurbFu::Request.delete(*args, &block)
|
26
24
|
end
|
27
25
|
|
28
26
|
attr_accessor :stubs
|
data/lib/curb-fu/core_ext.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
require 'cgi'
|
2
2
|
|
3
3
|
##
|
4
|
-
# ActiveSupport look alike for
|
4
|
+
# ActiveSupport look alike for to_param_pair. Very useful.
|
5
5
|
module CurbFu
|
6
6
|
module HashExtensions
|
7
7
|
def self.included(base)
|
8
|
-
base.send(:include, InstanceMethods)
|
8
|
+
base.send(:include, InstanceMethods)
|
9
9
|
#base.extend(ClassMethods)
|
10
10
|
end
|
11
11
|
|
12
12
|
module InstanceMethods
|
13
|
-
def
|
13
|
+
def to_param_pair(prefix)
|
14
14
|
collect do |k, v|
|
15
15
|
key_prefix = prefix ? "#{prefix}[#{k}]" : k
|
16
|
-
v.
|
16
|
+
v.to_param_pair(key_prefix)
|
17
17
|
end.join("&")
|
18
18
|
end
|
19
19
|
end
|
@@ -21,12 +21,12 @@ module CurbFu
|
|
21
21
|
|
22
22
|
module ObjectExtensions
|
23
23
|
def self.included(base)
|
24
|
-
base.send(:include, InstanceMethods)
|
24
|
+
base.send(:include, InstanceMethods)
|
25
25
|
#base.extend(ClassMethods)
|
26
26
|
end
|
27
27
|
|
28
28
|
module InstanceMethods
|
29
|
-
def
|
29
|
+
def to_param_pair(prefix = self.class)
|
30
30
|
value = CGI::escape(to_s)
|
31
31
|
"#{prefix}=#{value}"
|
32
32
|
end
|
@@ -35,14 +35,14 @@ module CurbFu
|
|
35
35
|
|
36
36
|
module ArrayExtensions
|
37
37
|
def self.included(base)
|
38
|
-
base.send(:include, InstanceMethods)
|
38
|
+
base.send(:include, InstanceMethods)
|
39
39
|
#base.extend(ClassMethods)
|
40
40
|
end
|
41
41
|
|
42
42
|
module InstanceMethods
|
43
|
-
def
|
43
|
+
def to_param_pair(prefix)
|
44
44
|
prefix = "#{prefix}[]"
|
45
|
-
collect { |item| "#{item.
|
45
|
+
collect { |item| "#{item.to_param_pair(prefix)}" }.join('&')
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
@@ -54,6 +54,9 @@ end
|
|
54
54
|
class Array
|
55
55
|
include CurbFu::ArrayExtensions
|
56
56
|
end
|
57
|
-
class
|
57
|
+
class String
|
58
|
+
include CurbFu::ObjectExtensions
|
59
|
+
end
|
60
|
+
class Fixnum
|
58
61
|
include CurbFu::ObjectExtensions
|
59
62
|
end
|
data/lib/curb-fu/request/base.rb
CHANGED
@@ -3,7 +3,7 @@ module CurbFu
|
|
3
3
|
module Base
|
4
4
|
include Common
|
5
5
|
|
6
|
-
def build(url_params, query_params = {})
|
6
|
+
def build(url_params, query_params = {}, &block)
|
7
7
|
curb = Curl::Easy.new(build_url(url_params, query_params))
|
8
8
|
|
9
9
|
headers = global_headers
|
@@ -23,6 +23,8 @@ module CurbFu
|
|
23
23
|
curb.headers = headers
|
24
24
|
curb.timeout = @timeout
|
25
25
|
|
26
|
+
yield curb if block_given?
|
27
|
+
|
26
28
|
curb
|
27
29
|
end
|
28
30
|
|
@@ -38,23 +40,23 @@ module CurbFu
|
|
38
40
|
@global_headers ||= {}
|
39
41
|
end
|
40
42
|
|
41
|
-
def get(url, params = {})
|
42
|
-
curb = self.build(url, params)
|
43
|
+
def get(url, params = {}, &block)
|
44
|
+
curb = self.build(url, params, &block)
|
43
45
|
curb.http_get
|
44
46
|
CurbFu::Response::Base.from_curb_response(curb)
|
45
47
|
end
|
46
48
|
|
47
|
-
def put(url, params = {})
|
48
|
-
curb = self.build(url, params)
|
49
|
+
def put(url, params = {}, &block)
|
50
|
+
curb = self.build(url, params, &block)
|
49
51
|
curb.http_put("")
|
50
52
|
CurbFu::Response::Base.from_curb_response(curb)
|
51
53
|
end
|
52
54
|
|
53
|
-
def post(url, params = {})
|
55
|
+
def post(url, params = {}, &block)
|
54
56
|
fields = create_post_fields(params)
|
55
57
|
fields = [fields] if fields.is_a?(String)
|
56
58
|
|
57
|
-
curb = self.build(url)
|
59
|
+
curb = self.build(url, &block)
|
58
60
|
curb.http_post(*fields)
|
59
61
|
response = CurbFu::Response::Base.from_curb_response(curb)
|
60
62
|
if CurbFu.debug?
|
@@ -66,11 +68,11 @@ module CurbFu
|
|
66
68
|
response
|
67
69
|
end
|
68
70
|
|
69
|
-
def post_file(url, params = {}, filez = {})
|
71
|
+
def post_file(url, params = {}, filez = {}, &block)
|
70
72
|
fields = create_post_fields(params)
|
71
73
|
fields += create_file_fields(filez)
|
72
74
|
|
73
|
-
curb = self.build(url)
|
75
|
+
curb = self.build(url, &block)
|
74
76
|
curb.multipart_form_post = true
|
75
77
|
|
76
78
|
begin
|
@@ -80,18 +82,10 @@ module CurbFu
|
|
80
82
|
raise e, "There was an attempt to post invalid fields. The fields were:\n#{field_list.join("\n")}"
|
81
83
|
end
|
82
84
|
CurbFu::Response::Base.from_curb_response(curb)
|
83
|
-
response = CurbFu::Response::Base.from_curb_response(curb)
|
84
|
-
if CurbFu.debug?
|
85
|
-
puts "Response from server was"
|
86
|
-
puts "Status: #{response.status}"
|
87
|
-
puts "Headers: #{response.headers.inspect}"
|
88
|
-
puts "Body: #{response.body.inspect}"
|
89
|
-
end
|
90
|
-
response
|
91
85
|
end
|
92
86
|
|
93
|
-
def delete(url)
|
94
|
-
curb = self.build(url)
|
87
|
+
def delete(url, &block)
|
88
|
+
curb = self.build(url, &block)
|
95
89
|
curb.http_delete
|
96
90
|
CurbFu::Response::Base.from_curb_response(curb)
|
97
91
|
end
|
@@ -130,4 +124,4 @@ module CurbFu
|
|
130
124
|
end
|
131
125
|
end
|
132
126
|
end
|
133
|
-
end
|
127
|
+
end
|
@@ -9,7 +9,7 @@ module CurbFu
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def self.build_uri_params(param_hash)
|
12
|
-
param_hash.
|
12
|
+
param_hash.to_param_pair
|
13
13
|
end
|
14
14
|
|
15
15
|
def self.build_post_fields(param_hash)
|
@@ -17,11 +17,11 @@ module CurbFu
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def to_uri_param
|
20
|
-
value.
|
20
|
+
value.to_param_pair(name)
|
21
21
|
end
|
22
22
|
|
23
23
|
def to_curl_post_field
|
24
|
-
field_string = value.
|
24
|
+
field_string = value.to_param_pair(name)
|
25
25
|
fields = field_string.split('&').collect do |field_value_pair|
|
26
26
|
field_name, field_value = field_value_pair.split('=')
|
27
27
|
Curl::PostField.content(field_name, CGI::unescape(field_value))
|
data/lib/curb-fu/response.rb
CHANGED
@@ -35,7 +35,10 @@ module CurbFu
|
|
35
35
|
header_lines.shift
|
36
36
|
header_lines.inject({}) do |hsh, line|
|
37
37
|
whole_enchillada, key, value = /^(.*?):\s*(.*)$/.match(line.chomp).to_a
|
38
|
-
|
38
|
+
unless whole_enchillada.nil?
|
39
|
+
# note: headers with multiple instances should have multiple values in the headers hash
|
40
|
+
hsh[key] = hsh[key] ? [hsh[key]].flatten << value : value
|
41
|
+
end
|
39
42
|
hsh
|
40
43
|
end
|
41
44
|
end
|
@@ -43,7 +46,31 @@ module CurbFu
|
|
43
46
|
def to_hash
|
44
47
|
{ :status => status, :body => body, :headers => headers }
|
45
48
|
end
|
46
|
-
|
49
|
+
|
50
|
+
def content_length
|
51
|
+
if ( header_value = self['Content-Length'] )
|
52
|
+
header_value.to_i
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def content_type
|
57
|
+
if ( header_value = self['Content-Type'] )
|
58
|
+
header_value.split(';').first
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def get_fields(key)
|
63
|
+
if ( match = @headers.find{|k,v| k.downcase == key.downcase} )
|
64
|
+
[match.last].flatten
|
65
|
+
else
|
66
|
+
[]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def [](key)
|
71
|
+
get_fields(key).last
|
72
|
+
end
|
73
|
+
|
47
74
|
def set_response_type(status)
|
48
75
|
case status
|
49
76
|
when 100..199 then
|
@@ -7,62 +7,40 @@ describe "module inclusion" do
|
|
7
7
|
include CurbFu::ObjectExtensions
|
8
8
|
end
|
9
9
|
|
10
|
-
Tester.new.should respond_to(:
|
11
|
-
end
|
12
|
-
it "should not overwrite a pre-existing method named :to_param" do
|
13
|
-
class TesterWithToParam
|
14
|
-
def to_param
|
15
|
-
"hooray, to_param!"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
TesterWithToParam.send(:include, CurbFu::ObjectExtensions)
|
20
|
-
TesterWithToParam.new.to_param.should == "hooray, to_param!"
|
21
|
-
end
|
22
|
-
it "should not overwrite the pre-existing method even if it comes from a module" do
|
23
|
-
module ActsLikeRails
|
24
|
-
def to_param
|
25
|
-
"foo"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
class TesterWithModule
|
29
|
-
include ActsLikeRails
|
30
|
-
end
|
31
|
-
TesterWithModule.send(:include, CurbFu::ObjectExtensions)
|
32
|
-
TesterWithModule.new.to_param.should == "foo"
|
10
|
+
Tester.new.should respond_to(:to_param_pair)
|
33
11
|
end
|
34
12
|
end
|
35
13
|
|
36
14
|
describe String do
|
37
|
-
it "should respond_to #
|
38
|
-
"".should respond_to(:
|
15
|
+
it "should respond_to #to_param_pair" do
|
16
|
+
"".should respond_to(:to_param_pair)
|
39
17
|
end
|
40
|
-
describe "
|
18
|
+
describe "to_param_pair" do
|
41
19
|
it "should return itself as the value for the passed-in name" do
|
42
|
-
"foo".
|
20
|
+
"foo".to_param_pair("quux").should == "quux=foo"
|
43
21
|
end
|
44
22
|
it "should be CGI escaped" do
|
45
|
-
"Whee, some 'unsafe' uri things".
|
23
|
+
"Whee, some 'unsafe' uri things".to_param_pair("safe").should == "safe=Whee%2C+some+%27unsafe%27+uri+things"
|
46
24
|
end
|
47
25
|
end
|
48
26
|
end
|
49
27
|
|
50
28
|
describe Hash do
|
51
|
-
it "should respond to #
|
52
|
-
{}.should respond_to(:
|
29
|
+
it "should respond to #to_param_pair" do
|
30
|
+
{}.should respond_to(:to_param_pair)
|
53
31
|
end
|
54
|
-
describe "
|
32
|
+
describe "to_param_pair" do
|
55
33
|
it "should collect its keys and values into parameter pairs, prepending the provided prefix" do
|
56
34
|
{
|
57
35
|
"kraplach" => "messy",
|
58
36
|
"zebot" => 2003
|
59
|
-
}.
|
37
|
+
}.to_param_pair("things").should == "things[kraplach]=messy&things[zebot]=2003"
|
60
38
|
end
|
61
39
|
it "should handle having an array as one of its parameters" do
|
62
40
|
result = {
|
63
41
|
"vielleicht" => "perhaps",
|
64
42
|
"ratings" => [5, 3, 5, 2, 4]
|
65
|
-
}.
|
43
|
+
}.to_param_pair("things")
|
66
44
|
result.split('&').size.should == 6
|
67
45
|
result.should =~ /things\[vielleicht\]=perhaps/
|
68
46
|
result.should =~ /things\[ratings\]\[\]=5/
|
@@ -75,26 +53,26 @@ describe Hash do
|
|
75
53
|
end
|
76
54
|
|
77
55
|
describe Array do
|
78
|
-
it "should respond_to #
|
79
|
-
[].should respond_to(:
|
56
|
+
it "should respond_to #to_param_pair" do
|
57
|
+
[].should respond_to(:to_param_pair)
|
80
58
|
end
|
81
|
-
describe "
|
59
|
+
describe "to_param_pair" do
|
82
60
|
it "should join each element, prepending a provided key prefix" do
|
83
|
-
[1, 23, 5].
|
61
|
+
[1, 23, 5].to_param_pair("magic_numbers").should == ["magic_numbers[]=1", "magic_numbers[]=23", "magic_numbers[]=5"].join("&")
|
84
62
|
end
|
85
|
-
it "should call
|
86
|
-
[1, 23, {"barkley" => 5}].
|
63
|
+
it "should call to_param_pair on each element, too" do
|
64
|
+
[1, 23, {"barkley" => 5}].to_param_pair("magic_numbers").should == "magic_numbers[]=1&magic_numbers[]=23&magic_numbers[][barkley]=5"
|
87
65
|
end
|
88
66
|
end
|
89
67
|
end
|
90
68
|
|
91
69
|
describe Integer do
|
92
|
-
it "should respond_to #
|
93
|
-
1.should respond_to(:
|
70
|
+
it "should respond_to #to_param_pair" do
|
71
|
+
1.should respond_to(:to_param_pair)
|
94
72
|
end
|
95
|
-
describe "
|
73
|
+
describe "to_param_pair" do
|
96
74
|
it "should return a stringified version of itself, using the provided key" do
|
97
|
-
5.
|
75
|
+
5.to_param_pair("fixnum").should == "fixnum=5"
|
98
76
|
end
|
99
77
|
end
|
100
|
-
end
|
78
|
+
end
|
@@ -142,7 +142,7 @@ describe CurbFu::Request::Test do
|
|
142
142
|
response = CurbFu::Request.get('http://a.example.com/gimme/html')
|
143
143
|
response.should be_a_kind_of(CurbFu::Response::Base)
|
144
144
|
response.status.should == 200
|
145
|
-
response.headers.should == { 'Content-Type' => 'spec/testcase' }
|
145
|
+
response.headers.should == { 'Content-Type' => 'spec/testcase', "Content-Length"=>"39" }
|
146
146
|
response.body.should == "A is for Archer, an excellent typeface."
|
147
147
|
end
|
148
148
|
end
|
@@ -162,7 +162,7 @@ describe CurbFu::Request::Test do
|
|
162
162
|
response = CurbFu::Request.post('http://a.example.com/gimme/html')
|
163
163
|
response.should be_a_kind_of(CurbFu::Response::Base)
|
164
164
|
response.status.should == 200
|
165
|
-
response.headers.should == { 'Content-Type' => 'spec/testcase' }
|
165
|
+
response.headers.should == { 'Content-Type' => 'spec/testcase', "Content-Length"=>"39" }
|
166
166
|
response.body.should == "A is for Archer, an excellent typeface."
|
167
167
|
end
|
168
168
|
end
|
@@ -181,7 +181,7 @@ describe CurbFu::Request::Test do
|
|
181
181
|
response = CurbFu::Request.post_file('http://a.example.com/gimme/html')
|
182
182
|
response.should be_a_kind_of(CurbFu::Response::Base)
|
183
183
|
response.status.should == 200
|
184
|
-
response.headers.should == { 'Content-Type' => 'spec/testcase' }
|
184
|
+
response.headers.should == { 'Content-Type' => 'spec/testcase', "Content-Length"=>"39" }
|
185
185
|
response.body.should == "A is for Archer, an excellent typeface."
|
186
186
|
end
|
187
187
|
end
|
@@ -198,7 +198,7 @@ describe CurbFu::Request::Test do
|
|
198
198
|
response = CurbFu::Request.put('http://a.example.com/gimme/html')
|
199
199
|
response.should be_a_kind_of(CurbFu::Response::Base)
|
200
200
|
response.status.should == 200
|
201
|
-
response.headers.should == { 'Content-Type' => 'spec/testcase' }
|
201
|
+
response.headers.should == { 'Content-Type' => 'spec/testcase', "Content-Length"=>"39" }
|
202
202
|
response.body.should == "A is for Archer, an excellent typeface."
|
203
203
|
end
|
204
204
|
end
|
@@ -216,7 +216,7 @@ describe CurbFu::Request::Test do
|
|
216
216
|
response = CurbFu::Request.delete('http://a.example.com/gimme/html')
|
217
217
|
response.should be_a_kind_of(CurbFu::Response::Base)
|
218
218
|
response.status.should == 200
|
219
|
-
response.headers.should == { 'Content-Type' => 'spec/testcase' }
|
219
|
+
response.headers.should == { 'Content-Type' => 'spec/testcase', "Content-Length"=>"39" }
|
220
220
|
response.body.should == "A is for Archer, an excellent typeface."
|
221
221
|
end
|
222
222
|
end
|
@@ -106,6 +106,110 @@ describe CurbFu::Response::Base do
|
|
106
106
|
@cf.headers['Content-Length'].should == '18'
|
107
107
|
@cf.headers.keys.length.should == 5
|
108
108
|
end
|
109
|
+
|
110
|
+
it "should use an array to store values for headers fields with multiple instances" do
|
111
|
+
headers = "HTTP/1.1 200 OK\r\nSet-Cookie: first cookie value\r\nSet-Cookie: second cookie value\r\n\r\n"
|
112
|
+
mock_curb = mock(Object, :response_code => 200, :body_str => 'OK', :header_str => headers, :timeout= => nil)
|
113
|
+
@cf = CurbFu::Response::Base.from_curb_response(mock_curb)
|
114
|
+
@cf.headers['Set-Cookie'].should == ["first cookie value", "second cookie value"]
|
115
|
+
end
|
109
116
|
end
|
110
117
|
end
|
118
|
+
|
119
|
+
describe "get_fields" do
|
120
|
+
|
121
|
+
before(:each) do
|
122
|
+
headers = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=ISO-8859-1\r\nContent-Length: 18\r\nSet-Cookie: first cookie value\r\nServer: gws\r\nTransfer-Encoding: chunked\r\nSet-Cookie: second cookie value\r\n\r\n"
|
123
|
+
mock_curb = mock(Object, :response_code => 200, :body_str => 'OK', :header_str => headers, :timeout= => nil)
|
124
|
+
@cf = CurbFu::Response::Base.from_curb_response(mock_curb)
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should return an array containing all matching field values" do
|
128
|
+
@cf.get_fields("Set-Cookie").should == ["first cookie value", "second cookie value"]
|
129
|
+
@cf.get_fields("Content-Length").should == ["18"]
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should do a case-insensitive match of the key to header fields" do
|
133
|
+
@cf.get_fields("content-length").should == ["18"]
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should return empty array when key matches no header field" do
|
137
|
+
@cf.get_fields("non-existent").should == []
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
describe "[]" do
|
143
|
+
|
144
|
+
before(:each) do
|
145
|
+
headers = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=ISO-8859-1\r\nContent-Length: 18\r\nSet-Cookie: first cookie value\r\nServer: gws\r\nTransfer-Encoding: chunked\r\nSet-Cookie: second cookie value\r\n\r\n"
|
146
|
+
mock_curb = mock(Object, :response_code => 200, :body_str => 'OK', :header_str => headers, :timeout= => nil)
|
147
|
+
@cf = CurbFu::Response::Base.from_curb_response(mock_curb)
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should return the last matching field value" do
|
151
|
+
@cf["Set-Cookie"].should == "second cookie value"
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should return the entire header value" do
|
155
|
+
@cf["Content-Type"].should == "text/html; charset=ISO-8859-1"
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should return the header value as a string" do
|
159
|
+
@cf["Content-Length"].should == "18"
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should do a case-insensitive match of the key to header fields" do
|
163
|
+
@cf["content-length"].should == "18"
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should return nil when key matches no header field" do
|
167
|
+
@cf["non-existent"].should == nil
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
describe "content_type" do
|
173
|
+
|
174
|
+
it "should return the content-type as a mime type, disgarding charset or other info found in the content-type header" do
|
175
|
+
headers = "HTTP/1.1 200 OK\r\nContent-Length: 18\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n"
|
176
|
+
mock_curb = mock(Object, :response_code => 200, :body_str => 'OK', :header_str => headers, :timeout= => nil)
|
177
|
+
@cf = CurbFu::Response::Base.from_curb_response(mock_curb)
|
178
|
+
@cf.content_type.should == "text/html"
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should return the content-type from the last header field value" do
|
182
|
+
headers = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=ISO-8859-1\r\nContent-Length: 18\r\nContent-Type: application/xhtml+xml; charset=UTF-8\r\n\r\n"
|
183
|
+
mock_curb = mock(Object, :response_code => 200, :body_str => 'OK', :header_str => headers, :timeout= => nil)
|
184
|
+
@cf = CurbFu::Response::Base.from_curb_response(mock_curb)
|
185
|
+
@cf.content_type.should == "application/xhtml+xml"
|
186
|
+
end
|
187
|
+
|
188
|
+
it "should return nil when the response doesn't contain a content-type header" do
|
189
|
+
headers = "HTTP/1.1 200 OK\r\nr\nContent-Length: 18r\n\r\n"
|
190
|
+
mock_curb = mock(Object, :response_code => 200, :body_str => 'OK', :header_str => headers, :timeout= => nil)
|
191
|
+
@cf = CurbFu::Response::Base.from_curb_response(mock_curb)
|
192
|
+
@cf.content_type.should == nil
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
describe "content_length" do
|
198
|
+
|
199
|
+
it "should return the last content-length header field value, as an integer" do
|
200
|
+
headers = "HTTP/1.1 200 OK\r\nContent-Length: 100\r\nContent-Length: 18\r\n\r\n"
|
201
|
+
mock_curb = mock(Object, :response_code => 200, :body_str => 'OK', :header_str => headers, :timeout= => nil)
|
202
|
+
@cf = CurbFu::Response::Base.from_curb_response(mock_curb)
|
203
|
+
@cf.content_length.should == 18
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should return nil when the response doesn't contain a content-length header" do
|
207
|
+
headers = "HTTP/1.1 200 OK\r\nr\nContent-Type: text/htmlr\n\r\n"
|
208
|
+
mock_curb = mock(Object, :response_code => 200, :body_str => 'OK', :header_str => headers, :timeout= => nil)
|
209
|
+
@cf = CurbFu::Response::Base.from_curb_response(mock_curb)
|
210
|
+
@cf.content_length.should == nil
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
|
111
215
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require "rubygems"
|
2
|
-
require 'spec'
|
3
2
|
require 'htmlentities'
|
4
3
|
|
5
4
|
dir = File.dirname(__FILE__)
|
@@ -8,4 +7,4 @@ $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
|
|
8
7
|
|
9
8
|
Dir.glob(File.join(dir,'helpers','**','*')).each { |helper| require helper }
|
10
9
|
|
11
|
-
require 'curb-fu'
|
10
|
+
require 'curb-fu'
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: curb-fu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 4
|
8
|
-
- 4
|
9
|
-
version: 0.4.4
|
4
|
+
prerelease:
|
5
|
+
version: 0.6.0
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Derek Kastner, Matt Wilson
|
@@ -14,21 +10,16 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date:
|
18
|
-
default_executable:
|
13
|
+
date: 2011-08-11 00:00:00 Z
|
19
14
|
dependencies:
|
20
15
|
- !ruby/object:Gem::Dependency
|
21
16
|
name: curb
|
22
17
|
prerelease: false
|
23
18
|
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
24
20
|
requirements:
|
25
21
|
- - ">="
|
26
22
|
- !ruby/object:Gem::Version
|
27
|
-
segments:
|
28
|
-
- 0
|
29
|
-
- 5
|
30
|
-
- 4
|
31
|
-
- 0
|
32
23
|
version: 0.5.4.0
|
33
24
|
type: :runtime
|
34
25
|
version_requirements: *id001
|
@@ -36,16 +27,35 @@ dependencies:
|
|
36
27
|
name: rack-test
|
37
28
|
prerelease: false
|
38
29
|
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
39
31
|
requirements:
|
40
32
|
- - ">="
|
41
33
|
- !ruby/object:Gem::Version
|
42
|
-
segments:
|
43
|
-
- 0
|
44
|
-
- 2
|
45
|
-
- 0
|
46
34
|
version: 0.2.0
|
47
35
|
type: :runtime
|
48
36
|
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - "="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.3.2
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: htmlentities
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id004
|
49
59
|
description:
|
50
60
|
email: development@greenviewdata.com
|
51
61
|
executables: []
|
@@ -57,7 +67,6 @@ extra_rdoc_files: []
|
|
57
67
|
files:
|
58
68
|
- lib/curb-fu/authentication.rb
|
59
69
|
- lib/curb-fu/core_ext.rb
|
60
|
-
- lib/curb-fu/entity.rb
|
61
70
|
- lib/curb-fu/request/base.rb
|
62
71
|
- lib/curb-fu/request/common.rb
|
63
72
|
- lib/curb-fu/request/parameter.rb
|
@@ -68,7 +77,15 @@ files:
|
|
68
77
|
- lib/curb-fu/test/server.rb
|
69
78
|
- lib/curb-fu/test.rb
|
70
79
|
- lib/curb-fu.rb
|
71
|
-
|
80
|
+
- spec/fixtures/foo.txt
|
81
|
+
- spec/lib/curb-fu/core_ext_spec.rb
|
82
|
+
- spec/lib/curb-fu/request/base_spec.rb
|
83
|
+
- spec/lib/curb-fu/request/parameter_spec.rb
|
84
|
+
- spec/lib/curb-fu/request/test_spec.rb
|
85
|
+
- spec/lib/curb-fu/response_spec.rb
|
86
|
+
- spec/lib/curb_fu_spec.rb
|
87
|
+
- spec/spec.opts
|
88
|
+
- spec/spec_helper.rb
|
72
89
|
homepage:
|
73
90
|
licenses: []
|
74
91
|
|
@@ -78,30 +95,27 @@ rdoc_options: []
|
|
78
95
|
require_paths:
|
79
96
|
- lib
|
80
97
|
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
81
99
|
requirements:
|
82
100
|
- - ">="
|
83
101
|
- !ruby/object:Gem::Version
|
84
|
-
segments:
|
85
|
-
- 0
|
86
102
|
version: "0"
|
87
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
88
105
|
requirements:
|
89
106
|
- - ">="
|
90
107
|
- !ruby/object:Gem::Version
|
91
|
-
segments:
|
92
|
-
- 0
|
93
108
|
version: "0"
|
94
109
|
requirements: []
|
95
110
|
|
96
111
|
rubyforge_project:
|
97
|
-
rubygems_version: 1.
|
112
|
+
rubygems_version: 1.8.5
|
98
113
|
signing_key:
|
99
114
|
specification_version: 3
|
100
115
|
summary: Friendly wrapper for curb
|
101
116
|
test_files:
|
102
117
|
- spec/fixtures/foo.txt
|
103
118
|
- spec/lib/curb-fu/core_ext_spec.rb
|
104
|
-
- spec/lib/curb-fu/entity_spec.rb
|
105
119
|
- spec/lib/curb-fu/request/base_spec.rb
|
106
120
|
- spec/lib/curb-fu/request/parameter_spec.rb
|
107
121
|
- spec/lib/curb-fu/request/test_spec.rb
|
data/lib/curb-fu/entity.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'json'
|
2
|
-
require 'uri'
|
3
|
-
|
4
|
-
module CurbFu
|
5
|
-
class Entity
|
6
|
-
attr_accessor :content_type, :data
|
7
|
-
|
8
|
-
def initialze(data)
|
9
|
-
self.data = data
|
10
|
-
end
|
11
|
-
|
12
|
-
def data
|
13
|
-
@data ||= ''
|
14
|
-
end
|
15
|
-
|
16
|
-
def to_request_data
|
17
|
-
return data if data.is_a?(String)
|
18
|
-
|
19
|
-
case content_type
|
20
|
-
when :uri then
|
21
|
-
to_uri
|
22
|
-
when 'application/json',:json then
|
23
|
-
to_json
|
24
|
-
# when 'application/xml',:xml then to_xml
|
25
|
-
when 'text/www-form-urlencoded',:form,:url then
|
26
|
-
to_www_form_url_encoded
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
# def to_xml
|
31
|
-
# "XML"
|
32
|
-
# end
|
33
|
-
|
34
|
-
def to_uri
|
35
|
-
strings = data.inject([]) do |acc, (name, value)|
|
36
|
-
acc << "#{name.to_s}=#{value}"
|
37
|
-
acc
|
38
|
-
end
|
39
|
-
URI.escape(strings.join('&'))
|
40
|
-
end
|
41
|
-
|
42
|
-
def to_json
|
43
|
-
data.to_json
|
44
|
-
end
|
45
|
-
|
46
|
-
def to_www_form_url_encoded
|
47
|
-
strings = data.inject([]) do |acc, (name, value)|
|
48
|
-
acc << "#{CGI.escape(name.to_s)}=#{CGI.escape(value)}"
|
49
|
-
acc
|
50
|
-
end
|
51
|
-
strings.join('&')
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
-
|
3
|
-
describe "CurbFu::Entity" do
|
4
|
-
before(:each) do
|
5
|
-
@entity = CurbFu::Entity.new
|
6
|
-
@entity.data = {
|
7
|
-
:zoltan => "Space Emperor!",
|
8
|
-
"em-dashes" => "more space=good"
|
9
|
-
}
|
10
|
-
end
|
11
|
-
|
12
|
-
describe "to_request_data" do
|
13
|
-
it "should be able to convert parameters into json" do
|
14
|
-
@entity.content_type = :json
|
15
|
-
|
16
|
-
JSON.parse(@entity.to_request_data).should include("zoltan" => "Space Emperor!", "em-dashes" => "more space=good")
|
17
|
-
end
|
18
|
-
it "should be able to convert parameters into 'XML'" do
|
19
|
-
pending "We need to choose an xml library"
|
20
|
-
|
21
|
-
@entity.content_type = "application/xml"
|
22
|
-
|
23
|
-
@entity.to_request_data.should == "XML"
|
24
|
-
end
|
25
|
-
it "should be able to convert parameters into form_url_encoding" do
|
26
|
-
@entity.content_type = :form
|
27
|
-
|
28
|
-
@entity.to_request_data.split('&').sort.should == ["em-dashes=more+space%3Dgood","zoltan=Space+Emperor%21"]
|
29
|
-
end
|
30
|
-
it "should not attempt to coerce plain text into the requested format" do
|
31
|
-
@entity.content_type = :json
|
32
|
-
@entity.data = "{\"my_own\": \"data\"}"
|
33
|
-
|
34
|
-
@entity.to_request_data.should == "{\"my_own\": \"data\"}"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|