mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* test/xmlrpc/test_client.rb: adding a test for performing an XMLRPC
call. * test/xmlrpc/data/blog.xml: supporting XML document for the response. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34933 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
2fbc01fb56
commit
1ac1163f56
3 changed files with 116 additions and 15 deletions
|
@ -1,3 +1,9 @@
|
|||
Wed Mar 7 07:39:28 2012 Aaron Patterson <aaron@tenderlovemaking.com>
|
||||
|
||||
* test/xmlrpc/test_client.rb: adding a test for performing an XMLRPC
|
||||
call.
|
||||
* test/xmlrpc/data/blog.xml: supporting XML document for the response.
|
||||
|
||||
Tue Mar 6 16:24:01 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* parse.y (parser_tokadd_string): escape simple regexp meta
|
||||
|
|
18
test/xmlrpc/data/blog.xml
Normal file
18
test/xmlrpc/data/blog.xml
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0"?>
|
||||
<methodResponse>
|
||||
<params>
|
||||
<param>
|
||||
<value>
|
||||
<array><data>
|
||||
<value><struct>
|
||||
<member><name>isAdmin</name><value><boolean>1</boolean></value></member>
|
||||
<member><name>url</name><value><string>http://tenderlovemaking.com/</string></value></member>
|
||||
<member><name>blogid</name><value><string>1</string></value></member>
|
||||
<member><name>blogName</name><value><string>Tender Lovemaking</string></value></member>
|
||||
<member><name>xmlrpc</name><value><string>http://tenderlovemaking.com/xmlrpc.php</string></value></member>
|
||||
</struct></value>
|
||||
</data></array>
|
||||
</value>
|
||||
</param>
|
||||
</params>
|
||||
</methodResponse>
|
|
@ -1,19 +1,63 @@
|
|||
require 'minitest/autorun'
|
||||
require 'xmlrpc/client'
|
||||
require 'net/http'
|
||||
|
||||
module XMLRPC
|
||||
class ClientTest < MiniTest::Unit::TestCase
|
||||
class FakeClient < XMLRPC::Client
|
||||
attr_reader :args
|
||||
module Fake
|
||||
class HTTP
|
||||
attr_accessor :read_timeout, :open_timeout, :use_ssl
|
||||
|
||||
def initialize(*args)
|
||||
@args = args
|
||||
super
|
||||
def initialize responses = {}
|
||||
@started = false
|
||||
@responses = responses
|
||||
end
|
||||
|
||||
def started?
|
||||
@started
|
||||
end
|
||||
def start; @started = true; end
|
||||
|
||||
def request_post path, request, headers
|
||||
@responses[path].shift
|
||||
end
|
||||
end
|
||||
|
||||
class Client < XMLRPC::Client
|
||||
attr_reader :args, :http
|
||||
|
||||
def initialize(*args)
|
||||
@args = args
|
||||
super
|
||||
end
|
||||
|
||||
private
|
||||
def net_http host, port, proxy_host, proxy_port
|
||||
HTTP.new
|
||||
end
|
||||
end
|
||||
|
||||
class Response
|
||||
def self.new body, fields = [], status = '200'
|
||||
klass = Class.new(Net::HTTPResponse::CODE_TO_OBJ[status]) {
|
||||
def initialize(*args)
|
||||
super
|
||||
@read = true
|
||||
end
|
||||
}
|
||||
|
||||
resp = klass.new '1.1', status, 'OK'
|
||||
resp.body = body
|
||||
fields.each do |k,v|
|
||||
resp.add_field k, v
|
||||
end
|
||||
resp
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_new2_host_path_port
|
||||
client = FakeClient.new2 'http://example.org/foo'
|
||||
client = Fake::Client.new2 'http://example.org/foo'
|
||||
host, path, port, *rest = client.args
|
||||
|
||||
assert_equal 'example.org', host
|
||||
|
@ -24,7 +68,7 @@ module XMLRPC
|
|||
end
|
||||
|
||||
def test_new2_custom_port
|
||||
client = FakeClient.new2 'http://example.org:1234/foo'
|
||||
client = Fake::Client.new2 'http://example.org:1234/foo'
|
||||
host, path, port, *rest = client.args
|
||||
|
||||
assert_equal 'example.org', host
|
||||
|
@ -35,7 +79,7 @@ module XMLRPC
|
|||
end
|
||||
|
||||
def test_new2_ssl
|
||||
client = FakeClient.new2 'https://example.org/foo'
|
||||
client = Fake::Client.new2 'https://example.org/foo'
|
||||
host, path, port, proxy_host, proxy_port, user, password, use_ssl, timeout = client.args
|
||||
|
||||
assert_equal 'example.org', host
|
||||
|
@ -51,7 +95,7 @@ module XMLRPC
|
|||
end
|
||||
|
||||
def test_new2_ssl_custom_port
|
||||
client = FakeClient.new2 'https://example.org:1234/foo'
|
||||
client = Fake::Client.new2 'https://example.org:1234/foo'
|
||||
host, path, port, proxy_host, proxy_port, user, password, use_ssl, timeout = client.args
|
||||
|
||||
assert_equal 'example.org', host
|
||||
|
@ -66,7 +110,7 @@ module XMLRPC
|
|||
end
|
||||
|
||||
def test_new2_user_password
|
||||
client = FakeClient.new2 'http://aaron:tenderlove@example.org/foo'
|
||||
client = Fake::Client.new2 'http://aaron:tenderlove@example.org/foo'
|
||||
host, path, port, proxy_host, proxy_port, user, password, use_ssl, timeout = client.args
|
||||
|
||||
[ host, path, port ].each { |x| assert x }
|
||||
|
@ -77,7 +121,7 @@ module XMLRPC
|
|||
end
|
||||
|
||||
def test_new2_proxy_host
|
||||
client = FakeClient.new2 'http://example.org/foo', 'example.com'
|
||||
client = Fake::Client.new2 'http://example.org/foo', 'example.com'
|
||||
host, path, port, proxy_host, proxy_port, user, password, use_ssl, timeout = client.args
|
||||
|
||||
[ host, path, port ].each { |x| assert x }
|
||||
|
@ -88,7 +132,7 @@ module XMLRPC
|
|||
end
|
||||
|
||||
def test_new2_proxy_port
|
||||
client = FakeClient.new2 'http://example.org/foo', 'example.com:1234'
|
||||
client = Fake::Client.new2 'http://example.org/foo', 'example.com:1234'
|
||||
host, path, port, proxy_host, proxy_port, user, password, use_ssl, timeout = client.args
|
||||
|
||||
[ host, path, port ].each { |x| assert x }
|
||||
|
@ -100,7 +144,7 @@ module XMLRPC
|
|||
end
|
||||
|
||||
def test_new2_no_path
|
||||
client = FakeClient.new2 'http://example.org'
|
||||
client = Fake::Client.new2 'http://example.org'
|
||||
host, path, port, *rest = client.args
|
||||
|
||||
assert_equal 'example.org', host
|
||||
|
@ -111,7 +155,7 @@ module XMLRPC
|
|||
end
|
||||
|
||||
def test_new2_slash_path
|
||||
client = FakeClient.new2 'http://example.org/'
|
||||
client = Fake::Client.new2 'http://example.org/'
|
||||
host, path, port, *rest = client.args
|
||||
|
||||
assert_equal 'example.org', host
|
||||
|
@ -134,7 +178,7 @@ module XMLRPC
|
|||
end
|
||||
|
||||
def test_new2_path_with_query
|
||||
client = FakeClient.new2 'http://example.org/foo?bar=baz'
|
||||
client = Fake::Client.new2 'http://example.org/foo?bar=baz'
|
||||
host, path, port, *rest = client.args
|
||||
|
||||
assert_equal 'example.org', host
|
||||
|
@ -143,5 +187,38 @@ module XMLRPC
|
|||
|
||||
rest.each { |x| refute x }
|
||||
end
|
||||
|
||||
def test_bad_content_type
|
||||
fh = read 'blog.xml'
|
||||
|
||||
responses = {
|
||||
'/foo' => [ Fake::Response.new(fh, [['Content-Type', 'text/xml']]) ]
|
||||
}
|
||||
|
||||
client = fake_client(responses).new2 'http://example.org/foo'
|
||||
|
||||
resp = client.call('wp.getUsersBlogs', 'tlo', 'omg')
|
||||
|
||||
expected = [{
|
||||
"isAdmin" => true,
|
||||
"url" => "http://tenderlovemaking.com/",
|
||||
"blogid" => "1",
|
||||
"blogName" => "Tender Lovemaking",
|
||||
"xmlrpc" => "http://tenderlovemaking.com/xmlrpc.php"
|
||||
}]
|
||||
|
||||
assert_equal expected, resp
|
||||
end
|
||||
|
||||
private
|
||||
def read filename
|
||||
File.read File.expand_path(File.join(__FILE__, '..', 'data', filename))
|
||||
end
|
||||
|
||||
def fake_client responses
|
||||
Class.new(Fake::Client) {
|
||||
define_method(:net_http) { |*_| Fake::HTTP.new(responses) }
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue