1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
require "pathname"
if defined? Minitest::Test
# We're on Minitest 5+. Nothing to do here.
else
# Minitest 4 doesn't have Minitest::Test yet.
Minitest::Test = MiniTest::Unit::TestCase
end
module OpenID
module TestDataMixin
TESTS_DIR = Pathname.new(__FILE__).dirname
TEST_DATA_DIR = Pathname.new('data')
def read_data_file(filename, lines=true, data_dir=TEST_DATA_DIR)
fname = TESTS_DIR.join(data_dir, filename)
if lines
fname.readlines
else
fname.read
end
end
end
module FetcherMixin
def with_fetcher(fetcher)
original_fetcher = OpenID.fetcher
begin
OpenID.fetcher = fetcher
return yield
ensure
OpenID.fetcher = original_fetcher
end
end
end
module Const
def const(symbol, value)
(class << self;self;end).instance_eval do
define_method(symbol) { value }
end
end
end
class MockResponse
attr_reader :code, :body
def initialize(code, body)
@code = code.to_s
@body = body
end
end
module ProtocolErrorMixin
def assert_protocol_error(str_prefix)
begin
result = yield
rescue ProtocolError => why
message = "Expected prefix #{str_prefix.inspect}, got "\
"#{why.message.inspect}"
assert(why.message.start_with?(str_prefix), message)
else
fail("Expected ProtocolError. Got #{result.inspect}")
end
end
end
module OverrideMethodMixin
def with_method_overridden(method_name, proc)
original = method(method_name)
begin
# TODO: find a combination of undef calls which prevent the warning
verbose, $VERBOSE = $VERBOSE, false
define_method(method_name, proc)
module_function(method_name)
$VERBOSE = verbose
yield
ensure
if original.respond_to? :owner
original.owner.send(:undef_method, method_name)
original.owner.send :define_method, method_name, original
else
define_method(method_name, original)
module_function(method_name)
end
end
end
end
# To use:
# > x = Object.new
# > x.extend(InstanceDefExtension)
# > x.instance_def(:monkeys) do
# > "bananas"
# > end
# > x.monkeys
#
module InstanceDefExtension
def instance_def(method_name, &proc)
(class << self;self;end).instance_eval do
# TODO: find a combination of undef calls which prevent the warning
verbose, $VERBOSE = $VERBOSE, false
define_method(method_name, proc)
$VERBOSE = verbose
end
end
end
GOODSIG = '[A Good Signature]'
class GoodAssoc
attr_accessor :handle, :expires_in
def initialize(handle='-blah-')
@handle = handle
@expires_in = 3600
end
def check_message_signature(msg)
msg.get_arg(OPENID_NS, 'sig') == GOODSIG
end
end
class HTTPResponse
def self._from_raw_data(status, body="", headers={}, final_url=nil)
resp = Net::HTTPResponse.new('1.1', status.to_s, 'NONE')
me = self._from_net_response(resp, final_url)
me.initialize_http_header headers
me.body = body
return me
end
end
end
|