On Oct 1, 2010, at 8:48 PM, David Chelimsky wrote: > On Oct 1, 2010, at 6:11 AM, Zhi-Qiang Lei wrote: > >> 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. >> >> 1) >> 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 > > > How are you initializing the app object? > > _______________________________________________ > rspec-users mailing list > rspec-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
As follow. $:.unshift File.join(File.dirname(__FILE__), '..') require 'main' require 'rack/test' describe EIOUAuthorization do include Rack::Test::Methods before :all do @person = Person.create(:email => 'zhiqiang....@gmail.com') @user = User.new @user.name = 'levin' @user.person = @person @user.password = 'password' @user.save end def unprotected_app lambda do |env| [200, {'Content-Type' => 'text/plain'}, ['Hello World!']] end end def protected_app @protected_app ||= EIOUAuthorization.new(unprotected_app) end alias :app :protected_app 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 after :all do Person.all.destroy User.all.destroy end end _______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users