Skip to content

Commit

Permalink
stub out openid fetcher instead of starting another server process
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Apr 17, 2010
1 parent e4dec67 commit 01cb836
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 58 deletions.
21 changes: 0 additions & 21 deletions test/openid_server.ru

This file was deleted.

94 changes: 57 additions & 37 deletions 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/"',
Expand Down Expand Up @@ -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
Expand All @@ -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']
Expand Down Expand Up @@ -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
Expand All @@ -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']
Expand All @@ -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]
Expand Down Expand Up @@ -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

0 comments on commit 01cb836

Please sign in to comment.