diff --git a/test/openid_server.ru b/test/openid_server.ru deleted file mode 100644 index eb82a17..0000000 --- a/test/openid_server.ru +++ /dev/null @@ -1,21 +0,0 @@ -require 'rots' -require 'tempfile' - -config = { - 'identity' => 'john.doe', - 'sreg' => { - 'nickname' => 'jdoe', - 'fullname' => 'John Doe', - 'email' => 'jhon@doe.com', - 'dob' => Date.parse('1985-09-21'), - 'gender' => 'M' - } -} - -map("/%s" % config['identity']) do - run Rots::IdentityPageApp.new(config, {}) -end - -map '/server' do - run Rots::ServerApp.new(config, :storage => Dir.tmpdir) -end diff --git a/test/test_openid.rb b/test/test_openid.rb index 40cd8a1..2d3a92e 100644 --- a/test/test_openid.rb +++ b/test/test_openid.rb @@ -1,14 +1,40 @@ require 'test/unit' require 'net/http' -require 'rack/mock' -require 'rack/session/pool' +require 'rack' require 'rack/openid' log = Logger.new(STDOUT) log.level = Logger::WARN OpenID::Util.logger = log +class MockFetcher + def initialize(app) + @app = app + end + + def fetch(url, body = nil, headers = nil, limit = nil) + opts = (headers || {}).dup + opts[:input] = body + opts[:method] = "POST" if body + env = Rack::MockRequest.env_for(url, opts) + + status, headers, body = @app.call(env) + + buf = [] + buf << "HTTP/1.1 #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]}" + headers.each { |header, value| buf << "#{header}: #{value}" } + buf << "" + body.each { |part| buf << part } + + io = Net::BufferedIO.new(StringIO.new(buf.join("\n"))) + res = Net::HTTPResponse.read_new(io) + res.reading_body(io, true) {} + OpenID::HTTPResponse._from_net_response(res, url) + end +end + + class TestHeader < Test::Unit::TestCase def test_build_header assert_equal 'OpenID identity="http://example.com/"', @@ -44,40 +70,32 @@ def test_parse_header end class TestOpenID < Test::Unit::TestCase - RotsServer = 'http://localhost:9292' - - @server_started = false - - def self.start_server! - return if @server_started - - pid = fork { - STDIN.reopen "/dev/null" - STDOUT.reopen "/dev/null", "a" - STDERR.reopen "/dev/null", "a" - - exec "rackup test/openid_server.ru" - } - - at_exit { - Process.kill 9, pid - Process.wait(pid) + RotsServerUrl = 'http://localhost:9292' + + RotsApp = Rack::Builder.new do + require 'rots' + + config = { + 'identity' => 'john.doe', + 'sreg' => { + 'nickname' => 'jdoe', + 'fullname' => 'John Doe', + 'email' => 'jhon@doe.com', + 'dob' => Date.parse('1985-09-21'), + 'gender' => 'M' + } } - begin - uri = URI.parse(RotsServer) - response = Net::HTTP.get_response(uri) - rescue Errno::ECONNREFUSED - sleep 0.5 - retry + map("/%s" % config['identity']) do + run Rots::IdentityPageApp.new(config, {}) end - @server_started = true + map '/server' do + run Rots::ServerApp.new(config, :storage => Dir.tmpdir) + end end - def setup - self.class.start_server! - end + OpenID.fetcher = MockFetcher.new(RotsApp) def test_with_get @app = app @@ -91,7 +109,7 @@ def test_with_get def test_with_deprecated_identity @app = app - process('/', :method => 'GET', :identity => "#{RotsServer}/john.doe?openid.success=true") + process('/', :method => 'GET', :identity => "#{RotsServerUrl}/john.doe?openid.success=true") follow_redirect! assert_equal 200, @response.status assert_equal 'GET', @response.headers['X-Method'] @@ -162,7 +180,7 @@ def test_with_attribute_exchange end def test_with_missing_id - @app = app(:identifier => "#{RotsServer}/john.doe") + @app = app(:identifier => "#{RotsServerUrl}/john.doe") process('/', :method => 'GET') follow_redirect! assert_equal 400, @response.status @@ -172,7 +190,7 @@ def test_with_missing_id end def test_with_timeout - @app = app(:identifier => RotsServer) + @app = app(:identifier => RotsServerUrl) process('/', :method => "GET") assert_equal 400, @response.status assert_equal 'GET', @response.headers['X-Method'] @@ -197,7 +215,7 @@ def test_passthrough_standard_http_basic_auth private def app(options = {}) - options[:identifier] ||= "#{RotsServer}/john.doe?openid.success=true" + options[:identifier] ||= "#{RotsServerUrl}/john.doe?openid.success=true" app = lambda { |env| if resp = env[Rack::OpenID::RESPONSE] @@ -228,9 +246,11 @@ def process(*args) def follow_redirect! assert @response assert_equal 303, @response.status - location = URI.parse(@response.headers['Location']) - response = Net::HTTP.get_response(location) - uri = URI(response['Location']) + + env = Rack::MockRequest.env_for(@response.headers['Location']) + status, headers, body = RotsApp.call(env) + + uri = URI(headers['Location']) process("#{uri.path}?#{uri.query}") end end