httprb 0.1.3
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/httprb.rb +14 -0
- data/lib/httprb/delegate.rb +75 -0
- data/lib/httprb/dsl.rb +91 -0
- data/lib/httprb/http_cache.rb +33 -0
- data/lib/httprb/request.rb +160 -0
- data/lib/httprb/version.rb +8 -0
- data/tests/disabled/test_collisions.rb +30 -0
- data/tests/test_dsl.rb +124 -0
- data/tests/test_http_cache.rb +41 -0
- data/tests/test_request.rb +53 -0
- metadata +70 -0
data/lib/httprb.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Delegator
|
2
|
+
#
|
3
|
+
# inspired by Sinatra, required to play nicely with Sinatra
|
4
|
+
# (and any other colliding namespace).
|
5
|
+
#
|
6
|
+
# It is important to note that all methods defined by HTTPrb
|
7
|
+
# (get, head, put, post, delete), if defined by the programmer
|
8
|
+
# or by an included module (ahem, Sinatra), Delegator will
|
9
|
+
# alias them to a dispatch method, which will do the following:
|
10
|
+
# 1. print a warning to the user that a collision was
|
11
|
+
# deteted and handled.
|
12
|
+
# 2. tell the user how to call HTTPrb's methods
|
13
|
+
# 3. call the original method (not HTTPrb's)
|
14
|
+
|
15
|
+
require 'irb/completion'
|
16
|
+
|
17
|
+
module HTTPrb
|
18
|
+
|
19
|
+
module Delegator
|
20
|
+
def self.delegate_collision(method)
|
21
|
+
ENV['HTTPRB_COLLISION_PREFIX'] ||= 'client_'
|
22
|
+
ENV['HTTPRB_IGNORE_COLLISIONS'] ||= 'true'
|
23
|
+
eval <<-RUBY, binding, '(__DELEGATE__)', 1
|
24
|
+
alias_method #{method.inspect}_original, #{method.inspect}
|
25
|
+
|
26
|
+
def #{method}(*args, &b)
|
27
|
+
if ENV['HTTPRB_IGNORE_COLLISIONS'] == 'true'
|
28
|
+
puts "WARNING: Namespace collision detected"
|
29
|
+
puts "HTTPrb::#{method} has been renamed to #{ENV['HTTPRB_COLLISION_PREFIX']}#{method}"
|
30
|
+
puts "To disable this message, set HTTPRB_IGNORE_COLLISIONS to false"
|
31
|
+
end
|
32
|
+
#{method}_original(*args, &b)
|
33
|
+
end
|
34
|
+
|
35
|
+
def #{ENV['HTTPRB_COLLISION_PREFIX']}#{method}(*args, &b)
|
36
|
+
::HTTPrb.send(#{method.inspect}, *args, &b)
|
37
|
+
end
|
38
|
+
private #{method.inspect}
|
39
|
+
RUBY
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.collides?(method)
|
43
|
+
unless Object.const_defined?(:IRB) and IRB.respond_to?(:conf)
|
44
|
+
eval <<-RUBY
|
45
|
+
module ::IRB
|
46
|
+
def IRB.binding
|
47
|
+
#{binding}
|
48
|
+
end
|
49
|
+
def IRB.workspace; IRB; end
|
50
|
+
def IRB.conf; {:MAIN_CONTEXT => IRB}; end
|
51
|
+
end
|
52
|
+
RUBY
|
53
|
+
end
|
54
|
+
::IRB::InputCompletor::CompletionProc.call(method.to_s).include?(method.to_s)
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.delegate(*methods)
|
58
|
+
methods.each do |method_name|
|
59
|
+
if self.collides?(method_name)
|
60
|
+
self.delegate_collision(method_name)
|
61
|
+
else
|
62
|
+
eval <<-RUBY, binding, '(__DELEGATE__)', 1
|
63
|
+
def #{method_name}(*args, &b)
|
64
|
+
::HTTPrb.send(#{method_name.inspect}, *args, &b)
|
65
|
+
end
|
66
|
+
private #{method_name.inspect}
|
67
|
+
RUBY
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
delegate :get, :put, :post, :delete, :head
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
data/lib/httprb/dsl.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# where the magic happens
|
2
|
+
|
3
|
+
# this mixin sets up the DSL, wrapping net/http for
|
4
|
+
# your programming pleasure
|
5
|
+
|
6
|
+
# note that each of the following methods accepts a
|
7
|
+
# block- this is the beauty of the whole thing.
|
8
|
+
|
9
|
+
# if you're wondering about the code duplication, read:
|
10
|
+
#
|
11
|
+
# http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html
|
12
|
+
#
|
13
|
+
# implicit invocation of blocks is just faster. so we
|
14
|
+
# duplicate code. :(
|
15
|
+
|
16
|
+
require 'httprb/request'
|
17
|
+
require 'httprb/http_cache'
|
18
|
+
|
19
|
+
module HTTPrb
|
20
|
+
public
|
21
|
+
# get request
|
22
|
+
#
|
23
|
+
# * options are not generally used; HTTPrb::Request
|
24
|
+
# offers additional methods for option generation/
|
25
|
+
# handling.
|
26
|
+
# * accepts a block, handing off the HTTPrb::Request
|
27
|
+
# object to it, if provided.
|
28
|
+
def HTTPrb.get(url, options = {})
|
29
|
+
options[:type] = 'GET'
|
30
|
+
req = Request.new(url, options)
|
31
|
+
yield(req) if block_given?
|
32
|
+
HTTPrb::HTTPCache.instance.make_request(req)
|
33
|
+
end
|
34
|
+
|
35
|
+
# head request
|
36
|
+
#
|
37
|
+
# * options are not generally used; HTTPrb::Request
|
38
|
+
# offers additional methods for option generation/
|
39
|
+
# handling.
|
40
|
+
# * accepts a block, handing off the HTTPrb::Request
|
41
|
+
# object to it, if provided.
|
42
|
+
def HTTPrb.head(url, options = {})
|
43
|
+
options[:type] = 'HEAD'
|
44
|
+
req = Request.new(url, options)
|
45
|
+
yield(req) if block_given?
|
46
|
+
HTTPrb::HTTPCache.instance.make_request(req)
|
47
|
+
end
|
48
|
+
|
49
|
+
# post request
|
50
|
+
#
|
51
|
+
# * options are not generally used; HTTPrb::Request
|
52
|
+
# offers additional methods for option generation/
|
53
|
+
# handling.
|
54
|
+
# * accepts a block, handing off the HTTPrb::Request
|
55
|
+
# object to it, if provided.
|
56
|
+
def HTTPrb.post(url, options = {})
|
57
|
+
options[:type] = 'POST'
|
58
|
+
req = Request.new(url, options)
|
59
|
+
yield(req) if block_given?
|
60
|
+
HTTPrb::HTTPCache.instance.make_request(req)
|
61
|
+
end
|
62
|
+
|
63
|
+
# put request
|
64
|
+
#
|
65
|
+
# * options are not generally used; HTTPrb::Request
|
66
|
+
# offers additional methods for option generation/
|
67
|
+
# handling.
|
68
|
+
# * accepts a block, handing off the HTTPrb::Request
|
69
|
+
# object to it, if provided.
|
70
|
+
def HTTPrb.put(url, options = {})
|
71
|
+
options[:type] = 'PUT'
|
72
|
+
req = Request.new(url, options)
|
73
|
+
yield(req) if block_given?
|
74
|
+
HTTPrb::HTTPCache.instance.make_request(req)
|
75
|
+
end
|
76
|
+
|
77
|
+
# delete request
|
78
|
+
#
|
79
|
+
# * options are not generally used; HTTPrb::Request
|
80
|
+
# offers additional methods for option generation/
|
81
|
+
# handling.
|
82
|
+
# * accepts a block, handing off the HTTPrb::Request
|
83
|
+
# object to it, if provided.
|
84
|
+
def HTTPrb.delete(url, options = {})
|
85
|
+
options[:type] = 'DELETE'
|
86
|
+
req = Request.new(url, options)
|
87
|
+
yield(req) if block_given?
|
88
|
+
HTTPrb::HTTPCache.instance.make_request(req)
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# a bit of insanity. a singleton representation
|
2
|
+
# of a Net::HTTP object. provides connection
|
3
|
+
# caching when the host is the same. no real
|
4
|
+
# benefit when changing servers (the underlying
|
5
|
+
# connection changes when changing servers, no
|
6
|
+
# way around that).
|
7
|
+
|
8
|
+
require 'singleton'
|
9
|
+
require 'net/http'
|
10
|
+
require 'net/https'
|
11
|
+
|
12
|
+
module HTTPrb
|
13
|
+
|
14
|
+
class HTTPCache
|
15
|
+
include Singleton
|
16
|
+
attr_reader :http
|
17
|
+
|
18
|
+
# accepts an HTTPrb::Request object, performs
|
19
|
+
# the request, and returns the Net::HTTP result
|
20
|
+
def make_request(req)
|
21
|
+
if @http && @http.address == req.uri.host && !@http.started?
|
22
|
+
@http.start
|
23
|
+
else
|
24
|
+
@http = Net::HTTP.new(req.uri.host, req.uri.port)
|
25
|
+
@http.use_ssl = req.ssl?
|
26
|
+
@http.set_debug_output($stderr) if req.debug
|
27
|
+
@http.start
|
28
|
+
end
|
29
|
+
@http.request(req.http_request)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
# more magic found here.
|
2
|
+
|
3
|
+
# HTTPrb::Request is where most of the actual
|
4
|
+
# functionality is provided. URI parsing,
|
5
|
+
# Net::HTTP::Request generation, etc., are all
|
6
|
+
# handled here.
|
7
|
+
|
8
|
+
require 'uri'
|
9
|
+
require 'cgi'
|
10
|
+
|
11
|
+
module HTTPrb
|
12
|
+
|
13
|
+
class Request
|
14
|
+
attr_accessor :uri, :headers, :parameters, :ssl, :debug
|
15
|
+
attr_reader :options
|
16
|
+
|
17
|
+
# create an HTTPrb::Request object
|
18
|
+
#
|
19
|
+
# the uri can be formatted however is most
|
20
|
+
# convenient to you- if the scheme (http://)
|
21
|
+
# is provided, it will be respected. if not
|
22
|
+
# provided, the default is http.
|
23
|
+
#
|
24
|
+
# if https:// is given as the scheme and the
|
25
|
+
# necesary openssl library is installed
|
26
|
+
# (meaning "require 'net/http'" doesn't fail)
|
27
|
+
# then ssl will be used.
|
28
|
+
#
|
29
|
+
# note that the 'options' parameter is only
|
30
|
+
# truly useful (or good) when not passing a
|
31
|
+
# block to the DSL. when a block is provided,
|
32
|
+
# using the accessor methods for each option
|
33
|
+
# (headers, parameters, etc.) is vastly
|
34
|
+
# preferred.
|
35
|
+
def initialize(uri, options = {})
|
36
|
+
if uri.is_a? URI
|
37
|
+
@uri = uri
|
38
|
+
elsif uri.is_a? String
|
39
|
+
@uri = URI.parse(uri)
|
40
|
+
if !@uri.scheme
|
41
|
+
@uri = URI.parse("http://" + uri)
|
42
|
+
end
|
43
|
+
else
|
44
|
+
raise Exception, "Invalid URL"
|
45
|
+
end
|
46
|
+
@uri.path = "/" if @uri.path.empty?
|
47
|
+
@ssl = true if @uri.scheme == "https"
|
48
|
+
|
49
|
+
@options = options
|
50
|
+
@options[:type] = 'GET' unless @options[:type]
|
51
|
+
@debug = options[:debug] ? true : false
|
52
|
+
|
53
|
+
@parameters = {}
|
54
|
+
if @uri.query
|
55
|
+
CGI.parse(@uri.query).each do |k,v|
|
56
|
+
if v.length == 1
|
57
|
+
@parameters[k] = v.first
|
58
|
+
else
|
59
|
+
@parameters[k] = v
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
@headers = {}
|
64
|
+
end
|
65
|
+
|
66
|
+
# sets up HTTP basic auth- identical to
|
67
|
+
# Net::HTTP request objects.
|
68
|
+
def basic_auth(user, pass)
|
69
|
+
@options[:basic_auth] = true
|
70
|
+
@options[:user] = user
|
71
|
+
@options[:pass] = pass
|
72
|
+
end
|
73
|
+
|
74
|
+
# set or add to a query parameter key/value pair
|
75
|
+
# if you wish to set multiple values for a key,
|
76
|
+
# provide an array of values as the second argument.
|
77
|
+
def parameter(key, value = nil)
|
78
|
+
@parameters[key] = value
|
79
|
+
end
|
80
|
+
|
81
|
+
# set a header key/value pair
|
82
|
+
def header(key, value)
|
83
|
+
if @headers[key]
|
84
|
+
if @headers[key].is_a? Array
|
85
|
+
@headers[key] << value
|
86
|
+
else
|
87
|
+
cur = @headers[key]
|
88
|
+
@headers[key] = [cur, value]
|
89
|
+
end
|
90
|
+
else
|
91
|
+
@headers[key] = value
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# generates the query string based on the provided
|
96
|
+
# parameter dictionary
|
97
|
+
def query_string
|
98
|
+
if !@parameters.empty?
|
99
|
+
# this is crabby because query strings can have more than
|
100
|
+
# one value per key- the key, in that case, is simply
|
101
|
+
# repeated with the additional value.
|
102
|
+
queries = []
|
103
|
+
@parameters.each do |k,v|
|
104
|
+
if v.is_a?(Array)
|
105
|
+
v.each {|val| queries << "#{k}=#{CGI.escape(val.to_s)}"}
|
106
|
+
else
|
107
|
+
queries << "#{k}=#{CGI.escape(v.to_s)}"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
return queries.join('&')
|
111
|
+
end
|
112
|
+
return ""
|
113
|
+
end
|
114
|
+
|
115
|
+
# generates a Net::HTTP request object. for use
|
116
|
+
# when the request is passed to Net::HTTP.
|
117
|
+
def http_request
|
118
|
+
path = "#{@uri.path}?#{query_string}"
|
119
|
+
http_req = case @options[:type].upcase
|
120
|
+
when 'GET'
|
121
|
+
Net::HTTP::Get.new(path)
|
122
|
+
when 'POST'
|
123
|
+
Net::HTTP::Post.new(path)
|
124
|
+
when 'PUT'
|
125
|
+
Net::HTTP::Put.new(path)
|
126
|
+
when 'HEAD'
|
127
|
+
Net::HTTP::Head.new(path)
|
128
|
+
when 'DELETE'
|
129
|
+
Net::HTTP::Delete.new(path)
|
130
|
+
else
|
131
|
+
Net::HTTP::Get.new(path)
|
132
|
+
end
|
133
|
+
if @options[:basic_auth] && @options[:user] && @options[:pass]
|
134
|
+
http_req.basic_auth(@options[:user], @options[:pass])
|
135
|
+
end
|
136
|
+
|
137
|
+
@headers.each do |key, value|
|
138
|
+
if value.is_a? Array
|
139
|
+
value.each {|v| http_req.add_field(key, v)}
|
140
|
+
else
|
141
|
+
http_req.add_field(key, value)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
return http_req
|
146
|
+
end
|
147
|
+
|
148
|
+
# yep, you got it. we're messing with your mind
|
149
|
+
# here. nothing to see here, move along...
|
150
|
+
def method_missing(method)
|
151
|
+
if method == :"ssl?"
|
152
|
+
return @ssl
|
153
|
+
elsif @options.keys.include?(method)
|
154
|
+
return @options[method]
|
155
|
+
end
|
156
|
+
super
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
$:.unshift File.join(File.dirname(File.dirname(__FILE__)), 'lib')
|
4
|
+
require 'httprb'
|
5
|
+
|
6
|
+
class TestDSL < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_collisions
|
14
|
+
assert_not_nil res = client_get('http://mozy.com')
|
15
|
+
assert_not_nil res = client_head('mozy.com')
|
16
|
+
assert_not_nil res = client_put('mozy.com')
|
17
|
+
assert_not_nil res = client_post('mozy.com')
|
18
|
+
assert_not_nil res = client_delete('mozy.com')
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_custom_prefix
|
22
|
+
ENV['HTTPRB_COLLISION_PREFIX'] = 'my_client'
|
23
|
+
load 'httprb.rb'
|
24
|
+
assert_not_nil res = my_client_get('mozy.com')
|
25
|
+
assert_not_nil res = my_client_head('mozy.com')
|
26
|
+
assert_not_nil res = my_client_put('mozy.com')
|
27
|
+
assert_not_nil res = my_client_post('mozy.com')
|
28
|
+
assert_not_nil res = my_client_delete('mozy.com')
|
29
|
+
end
|
30
|
+
end
|
data/tests/test_dsl.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
$:.unshift File.join(File.dirname(File.dirname(__FILE__)), 'lib')
|
4
|
+
require 'httprb'
|
5
|
+
|
6
|
+
class TestDSL < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_get_urls
|
14
|
+
assert_not_nil res = get('http://mozy.com')
|
15
|
+
assert_not_nil res = get('mozy.com')
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_get
|
19
|
+
assert_not_nil res = get('http://mozy.com')
|
20
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_get_block
|
24
|
+
res = get 'http://google.com' do |req|
|
25
|
+
assert_not_nil req
|
26
|
+
assert req.options[:type] == 'GET'
|
27
|
+
assert req.uri.host = 'mozy.com'
|
28
|
+
end
|
29
|
+
assert_not_nil res
|
30
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_get_ssl
|
34
|
+
assert_not_nil res = get('https://mozy.com/', {:use_ssl => true})
|
35
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_get_ssl_block
|
39
|
+
res = get 'https://mozy.com/' do |req|
|
40
|
+
assert_not_nil req
|
41
|
+
assert req.options[:type] == 'GET'
|
42
|
+
assert req.uri.host = 'mozy.com'
|
43
|
+
end
|
44
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_head
|
48
|
+
assert_not_nil res = head('http://mozy.com')
|
49
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_head_block
|
53
|
+
res = head 'http://google.com' do |req|
|
54
|
+
assert_not_nil req
|
55
|
+
assert req.options[:type] == 'HEAD'
|
56
|
+
assert req.uri.host = 'mozy.com'
|
57
|
+
end
|
58
|
+
assert_not_nil res
|
59
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_put
|
63
|
+
assert_not_nil res = put('http://mozy.com')
|
64
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_put_block
|
68
|
+
res = put 'http://google.com' do |req|
|
69
|
+
assert_not_nil req
|
70
|
+
assert req.options[:type] == 'PUT'
|
71
|
+
assert req.uri.host = 'mozy.com'
|
72
|
+
end
|
73
|
+
assert_not_nil res
|
74
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_post
|
78
|
+
assert_not_nil res = post('http://mozy.com')
|
79
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_post_block
|
83
|
+
res = post 'http://google.com' do |req|
|
84
|
+
assert_not_nil req
|
85
|
+
assert req.options[:type] == 'POST'
|
86
|
+
assert req.uri.host = 'mozy.com'
|
87
|
+
end
|
88
|
+
assert_not_nil res
|
89
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_delete
|
93
|
+
assert_not_nil res = delete('http://mozy.com')
|
94
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_delete_block
|
98
|
+
res = delete 'http://google.com' do |req|
|
99
|
+
assert_not_nil req
|
100
|
+
assert req.options[:type] == 'DELETE'
|
101
|
+
assert req.uri.host = 'mozy.com'
|
102
|
+
end
|
103
|
+
assert_not_nil res
|
104
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_params
|
108
|
+
res = get 'http://mozy.com' do |req|
|
109
|
+
assert req.parameter "key", "value"
|
110
|
+
assert req.parameter "key2", "value"
|
111
|
+
assert req.parameters.include?("key")
|
112
|
+
assert req.parameters.include?("key2")
|
113
|
+
assert req.http_request.path.include?("key=value")
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_headers
|
118
|
+
res = get 'http://mozy.com' do |req|
|
119
|
+
assert req.header "x-key", "value"
|
120
|
+
assert req.headers["x-key"] == "value"
|
121
|
+
assert req.http_request["x-key"] == "value"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require File.join(File.dirname(File.dirname(__FILE__)), 'lib/httprb/http_cache.rb')
|
4
|
+
require File.join(File.dirname(File.dirname(__FILE__)), 'lib/httprb/request.rb')
|
5
|
+
|
6
|
+
class TestHTTPCache < Test::Unit::TestCase
|
7
|
+
def setup; end
|
8
|
+
def teardown; end
|
9
|
+
|
10
|
+
def test_connection_caching
|
11
|
+
assert_not_nil cache = HTTPrb::HTTPCache.instance
|
12
|
+
|
13
|
+
# start things out with a little google...
|
14
|
+
assert_not_nil req = HTTPrb::Request.new("http://google.com")
|
15
|
+
assert_not_nil res = cache.make_request(req)
|
16
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
17
|
+
assert cache.http
|
18
|
+
assert cache.http.started?
|
19
|
+
|
20
|
+
# play it again, sam... (to test connection caching)
|
21
|
+
assert_not_nil res = cache.make_request(req)
|
22
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
23
|
+
assert cache.http
|
24
|
+
assert cache.http.started?
|
25
|
+
|
26
|
+
# try a different host- should transparently stop
|
27
|
+
# caching, start over, and make the request
|
28
|
+
assert_not_nil req = HTTPrb::Request.new("http://yahoo.com")
|
29
|
+
assert_not_nil res = cache.make_request(req)
|
30
|
+
assert res.code =~ /2[0-9].|3[0-9]./
|
31
|
+
assert cache.http
|
32
|
+
assert cache.http.started?
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_ssl
|
36
|
+
assert_not_nil cache = HTTPrb::HTTPCache.instance
|
37
|
+
|
38
|
+
assert_not_nil req = HTTPrb::Request.new("https://www.bankofamerica.com")
|
39
|
+
assert_not_nil res = cache.make_request(req)
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require File.join(File.dirname(File.dirname(__FILE__)), 'lib/httprb/request.rb')
|
4
|
+
require 'net/http'
|
5
|
+
|
6
|
+
class TestRequest < Test::Unit::TestCase
|
7
|
+
def setup; end
|
8
|
+
def teardown; end
|
9
|
+
|
10
|
+
def test_request_types
|
11
|
+
assert_not_nil req = HTTPrb::Request.new('http://google.com', {:type => 'POST'})
|
12
|
+
assert req.http_request.is_a? Net::HTTP::Post
|
13
|
+
assert_not_nil req = HTTPrb::Request.new('http://google.com', {:type => 'PUT'})
|
14
|
+
assert req.http_request.is_a? Net::HTTP::Put
|
15
|
+
assert_not_nil req = HTTPrb::Request.new('http://google.com', {:type => 'HEAD'})
|
16
|
+
assert req.http_request.is_a? Net::HTTP::Head
|
17
|
+
assert_not_nil req = HTTPrb::Request.new('http://google.com', {:type => 'DELETE'})
|
18
|
+
assert req.http_request.is_a? Net::HTTP::Delete
|
19
|
+
assert_not_nil req = HTTPrb::Request.new('http://google.com', {:type => 'GET'})
|
20
|
+
assert req.http_request.is_a? Net::HTTP::Get
|
21
|
+
assert_not_nil req = HTTPrb::Request.new('http://google.com')
|
22
|
+
assert req.http_request.is_a? Net::HTTP::Get
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_ssl
|
26
|
+
assert_not_nil req = HTTPrb::Request.new("https://www.bankofamerica.com")
|
27
|
+
assert req.ssl?
|
28
|
+
assert req.ssl
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_paramaters
|
32
|
+
assert_not_nil req = HTTPrb::Request.new('http://localhost', {:type => 'GET'})
|
33
|
+
assert req.parameter "key", "value"
|
34
|
+
assert req.http_request.path == '/?key=value'
|
35
|
+
assert req.parameter "key", ["value", "value2"]
|
36
|
+
assert req.http_request.path == '/?key=value&key=value2' || req.http_request.path == '/?key=value2&key=value'
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_query_string_params
|
40
|
+
assert_not_nil req = HTTPrb::Request.new('http://localhost?key=value&key2=value2', {:type => 'GET'})
|
41
|
+
assert req.parameters["key"] == "value"
|
42
|
+
assert req.parameters["key2"] == "value2"
|
43
|
+
assert_not_nil req = HTTPrb::Request.new('http://localhost?key=value&key=value2', {:type => 'GET'})
|
44
|
+
assert req.parameters["key"].is_a?(Array)
|
45
|
+
assert req.parameters["key"].include?('value') && req.parameters["key"].include?('value2')
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_headers
|
49
|
+
assert_not_nil req = HTTPrb::Request.new('http://localhost', {:type => 'GET'})
|
50
|
+
assert(req.header "key", "value")
|
51
|
+
assert(req.http_request["key"] == "value")
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: httprb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- thomas metge
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-11-13 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: HTTP client DSL, inspired by sinatra.
|
22
|
+
email: tom@accident-prone.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/httprb/delegate.rb
|
31
|
+
- lib/httprb/dsl.rb
|
32
|
+
- lib/httprb/http_cache.rb
|
33
|
+
- lib/httprb/request.rb
|
34
|
+
- lib/httprb/version.rb
|
35
|
+
- lib/httprb.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/tommetge/httprb
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.6
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: "http the ruby way: sinatra-like http client DSL"
|
66
|
+
test_files:
|
67
|
+
- tests/disabled/test_collisions.rb
|
68
|
+
- tests/test_dsl.rb
|
69
|
+
- tests/test_http_cache.rb
|
70
|
+
- tests/test_request.rb
|