4 require File.join(File.dirname(__FILE__), 'webrick_testing')
5 require "xmlrpc/server"
6 require 'xmlrpc/client'
8 class TestCookie < Test::Unit::TestCase
9 include WEBrick_Testing
12 s = XMLRPC::WEBrickServlet.new
15 @logged_in_users ||= {}
23 def s.service(request, response)
32 key = Time.now.to_i.to_s
33 valid_user = "valid-user"
34 s.add_handler("test.login") do |user, password|
35 ok = (user == valid_user and password == "secret")
37 s.logged_in_users[key] = user
38 expires = (Time.now + 60 * 60).httpdate
39 cookies = s.response.cookies
40 cookies << "key=\"#{key}\"; path=\"/RPC2\"; expires=#{expires}"
41 cookies << "user=\"#{user}\"; path=\"/RPC2\""
46 s.add_handler("test.require_authenticate_echo") do |string|
48 s.request.cookies.each do |cookie|
49 cookies[cookie.name] = cookie.value
51 if cookies == {"key" => key, "user" => valid_user}
54 raise XMLRPC::FaultException.new(29, "Authentication required")
58 s.set_default_handler do |name, *args|
59 raise XMLRPC::FaultException.new(-99, "Method #{name} missing" +
60 " or wrong number of parameters!")
68 def setup_http_server(port)
69 option = {:Port => port}
71 start_server(option) {|w| w.mount('/RPC2', create_servlet) }
73 @s = XMLRPC::Client.new3(:port => port)
79 setup_http_server(PORT)
87 assert(!@s.call("test.login", "invalid-user", "invalid-password"))
88 exception = assert_raise(XMLRPC::FaultException) do
89 @s.call("test.require_authenticate_echo", "Hello")
91 assert_equal(29, exception.faultCode)
93 assert(@s.call("test.login", "valid-user", "secret"))
94 assert_equal("Hello", @s.call("test.require_authenticate_echo", "Hello"))