зеркало из https://github.com/github/ruby.git
* lib/xmlrpc/client.rb: fix cookie handling. [ruby-dev:34403]
* test/xmlrpc/test_cookie.rb: add a test for the above fix. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16042 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
5a4c8a1ad7
Коммит
17ea268764
|
@ -1,3 +1,9 @@
|
|||
Tue Apr 15 23:08:46 2008 Kouhei Sutou <kou@cozmixng.org>
|
||||
|
||||
* lib/xmlrpc/client.rb: fix cookie handling. [ruby-dev:34403]
|
||||
|
||||
* test/xmlrpc/test_cookie.rb: add a test for the above fix.
|
||||
|
||||
Tue Apr 15 19:20:14 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||
|
||||
* io.c: #undef rb_argv moved before #define.
|
||||
|
|
|
@ -565,8 +565,13 @@ module XMLRPC
|
|||
raise "Wrong size. Was #{data.size}, should be #{expected}"
|
||||
end
|
||||
|
||||
c = resp["Set-Cookie"]
|
||||
@cookie = c if c
|
||||
set_cookies = resp.get_fields("Set-Cookie")
|
||||
if set_cookies and !set_cookies.empty?
|
||||
@cookie = set_cookies.collect do |set_cookie|
|
||||
cookie = WEBrick::Cookie.parse_set_cookie(set_cookie)
|
||||
WEBrick::Cookie.new(cookie.name, cookie.value).to_s
|
||||
end.join("; ")
|
||||
end
|
||||
|
||||
return data
|
||||
end
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
require 'test/unit'
|
||||
require 'time'
|
||||
require 'webrick'
|
||||
require File.join(File.dirname(__FILE__), 'webrick_testing')
|
||||
require "xmlrpc/server"
|
||||
require 'xmlrpc/client'
|
||||
|
||||
class TestCookie < Test::Unit::TestCase
|
||||
include WEBrick_Testing
|
||||
|
||||
def create_servlet
|
||||
s = XMLRPC::WEBrickServlet.new
|
||||
|
||||
def s.logged_in_users
|
||||
@logged_in_users ||= {}
|
||||
end
|
||||
def s.request
|
||||
@request
|
||||
end
|
||||
def s.response
|
||||
@response
|
||||
end
|
||||
def s.service(request, response)
|
||||
@request = request
|
||||
@response = response
|
||||
super
|
||||
ensure
|
||||
@request = nil
|
||||
@response = nil
|
||||
end
|
||||
|
||||
key = Time.now.to_i.to_s
|
||||
valid_user = "valid-user"
|
||||
s.add_handler("test.login") do |user, password|
|
||||
ok = (user == valid_user and password == "secret")
|
||||
if ok
|
||||
s.logged_in_users[key] = user
|
||||
expires = (Time.now + 60 * 60).httpdate
|
||||
cookies = s.response.cookies
|
||||
cookies << "key=\"#{key}\"; path=\"/RPC2\"; expires=#{expires}"
|
||||
cookies << "user=\"#{user}\"; path=\"/RPC2\""
|
||||
end
|
||||
ok
|
||||
end
|
||||
|
||||
s.add_handler("test.require_authenticate_echo") do |string|
|
||||
cookies = {}
|
||||
s.request.cookies.each do |cookie|
|
||||
cookies[cookie.name] = cookie.value
|
||||
end
|
||||
if cookies == {"key" => key, "user" => valid_user}
|
||||
string
|
||||
else
|
||||
raise XMLRPC::FaultException.new(29, "Authentication required")
|
||||
end
|
||||
end
|
||||
|
||||
s.set_default_handler do |name, *args|
|
||||
raise XMLRPC::FaultException.new(-99, "Method #{name} missing" +
|
||||
" or wrong number of parameters!")
|
||||
end
|
||||
|
||||
s.add_introspection
|
||||
|
||||
s
|
||||
end
|
||||
|
||||
def setup_http_server(port)
|
||||
option = {:Port => port}
|
||||
|
||||
start_server(option) {|w| w.mount('/RPC2', create_servlet) }
|
||||
|
||||
@s = XMLRPC::Client.new3(:port => port)
|
||||
end
|
||||
|
||||
PORT = 8070
|
||||
def test_cookie
|
||||
begin
|
||||
setup_http_server(PORT)
|
||||
do_test
|
||||
ensure
|
||||
stop_server
|
||||
end
|
||||
end
|
||||
|
||||
def do_test
|
||||
assert(!@s.call("test.login", "invalid-user", "invalid-password"))
|
||||
exception = assert_raise(XMLRPC::FaultException) do
|
||||
@s.call("test.require_authenticate_echo", "Hello")
|
||||
end
|
||||
assert_equal(29, exception.faultCode)
|
||||
|
||||
assert(@s.call("test.login", "valid-user", "secret"))
|
||||
assert_equal("Hello", @s.call("test.require_authenticate_echo", "Hello"))
|
||||
end
|
||||
end
|
Загрузка…
Ссылка в новой задаче