summaryrefslogtreecommitdiff
path: root/test/xmlrpc
diff options
context:
space:
mode:
authortenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-03-06 22:40:38 +0000
committertenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-03-06 22:40:38 +0000
commit1ac1163f562cc026199c81abefe4c8f9ab840b24 (patch)
tree3dcf5d3269c06331b58a8b112f070401bd314fbb /test/xmlrpc
parent2fbc01fb56332d5a611984570ff7346acd844eeb (diff)
* 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
Diffstat (limited to 'test/xmlrpc')
-rw-r--r--test/xmlrpc/data/blog.xml18
-rw-r--r--test/xmlrpc/test_client.rb107
2 files changed, 110 insertions, 15 deletions
diff --git a/test/xmlrpc/data/blog.xml b/test/xmlrpc/data/blog.xml
new file mode 100644
index 0000000..cb325f1
--- /dev/null
+++ b/test/xmlrpc/data/blog.xml
@@ -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>
diff --git a/test/xmlrpc/test_client.rb b/test/xmlrpc/test_client.rb
index fd22630..a509aec 100644
--- a/test/xmlrpc/test_client.rb
+++ b/test/xmlrpc/test_client.rb
@@ -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 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
- def initialize(*args)
- @args = args
- super
+ 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