Dear All,
I meet a strange issue when I test my Rack Middleware. Could anyone help
me to point the error out? Thanks.
This is the code.
class EIOUAuthorization
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
path_strings = request.path_info.split(’/’, 4)
if path_strings[1] == ‘people’
user = User.get(path_strings[2])
return not_found if user.nil?
digest_authentication(user).call(env)
else
@app.call(env)
end
end
#private
def not_found
[404, {‘Content-Type’ => ‘text/plain’, ‘Content-Length’ => ‘0’}, []]
end
def digest_authentication(user)
auth = Rack::Auth::Digest::MD5.new(@app, user.realm) do |email|
{user.person.email => user.ha1}[email]
end
auth.passwords_hashed = true
auth.opaque = ‘opaque-for-’ + user.realm
auth
end
end
And this is my test case.
describe ‘#call’ do
it ‘should call digest_authentication method with id when get
/people/{person-id}’ do
app.should_receive(:digest_authentication).with(@user).once
get “/people/#{@person.id}”
end
end
describe ‘#digest_authentication’ do
it ‘should respond to call’ do
app.digest_authentication(@user).should respond_to(:call)
end
end
From the result we can see the #digest_authentication is called, and the result should respond to call. But the fact is that it return a nil object in the code.
F.
NoMethodError in ‘EIOUAuthorization#call should call
digest_authentication method with id when get /people/{person-id}’
undefined method call' for nil:NilClass ./spec/../main.rb:21:in
call’
/Library/Ruby/Gems/1.8/gems/rack-test-0.5.4/lib/rack/mock_session.rb:30:in
request' /Library/Ruby/Gems/1.8/gems/rack-test-0.5.4/lib/rack/test.rb:207:in
process_request’
/Library/Ruby/Gems/1.8/gems/rack-test-0.5.4/lib/rack/test.rb:57:in `get’
./spec/eiou_authorization.rb:34:
Finished in 0.024267 seconds
2 examples, 1 failure
Best regards,
Lei, Zhi-Qiang