2016-01-22 57 views
1

設置如下。對於每個http請求,經理都會在頭部(名稱,密碼)中發送他的憑證。這些檢查與數據庫中的條目,如果他們成功返回所需的用戶對象。在請求測試中如何實現基本的http_auth?我只想添加密碼和用戶名並測試返回值?請求測試的目標是什麼?我想沒有多少成功如下:Rspec Rails 4.2.5使用基本http驗證的請求測試傳遞

我創建了一個化AuthHelper模塊spec/support/auth_helper.rb

module AuthHelper 
    def http_login 
    user = 'test' 
    pw = 'test' 
    request.ENV['HTTP_AUTHORIZATION'] =ActionController::HttpAuthentication::Basic.encode_credentials(user,pw) 
    end 
end 

和如下

include AuthHelper 
describe "User API get 1 user object" do  
     before(:each) do 
       http_login 
     end 

,但我收到此錯誤信息使用它在requests/api/user_spec.rb。我怎樣才能解決這個問題,並使我的測試通過http_auth?我在這裏看到很多類似的topis和問題,但 他們主要適用於舊版本的rspec和導軌,並且不適用於我的案例

在此先感謝!

Failure/Error: request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw) 

NoMethodError: 
    undefined method `env' for nil:NilClass# ./spec/support 

# ./spec/support/auth_helper.rb:5:in `http_login' 
# ./spec/requests/api/user_spec.rb:8:in `block (2 levels) in <top (required)>' 

更新

我移動的請求內的報頭生成。我查了一下Auth的動詞,所以我認爲這個任務應該可以工作。我在rails控制檯中測試了ActionController調用,並收到一個字符串。我想這也是correct.My整個測試現在看起來是這樣的:

describe "User API get 1 user object", type: :request do 
     it 'Get sends back one User object ' do    
       headers = { 
          "AUTHORIZATION" =>ActionController::HttpAuthentication::Basic.encode_credentials("test","test") 
    #      "AUTHORIZATION" =>ActionController::HttpAuthentication::Token.encode_credentials("test","test") 
       } 
       FactoryGirl.create(:user) 
       get "/api/1/user", headers 
       #json = JSON.parse(response.body) 
       expect(response).to be_success 
     #  expect(response.content_type).to eq("application/json") 
     end  
    end  

收到以下錯誤:

這incudles所以訪問被拒絕還是行@buf=["HTTP Basic: Access denied.\n"]

Failure/Error: expect(response).to be_success 
    expected `#<ActionDispatch::TestResponse:0x000000070d1d38 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x000000070d1c98>, @stream=#<ActionDispatch::Response::Buffer:0x000000070d1c48 @response=#<ActionDispatch::TestResponse:0x000000070d1d38 ...>, 
@buf=["HTTP Basic: Access denied.\n"], @closed=false>, @header={"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff", "WWW-Authenticate"=>"Basic realm=\"Application\"", "Content-Type"=>"text/html; charset=utf-8", "Cache-Control"=>"no-cache", "X-Request-Id"=>"9c27d4e9-84c0-4ef3-82ed-cccfb19876a0", "X-Runtime"=>"0.134230", "Content-Length"=>"27"}, @status=401, @sending_file=false, @blank=false, 
@cv=#<MonitorMixin::ConditionVariable:0x000000070d1bf8 @monitor=#<ActionDispatch::TestResponse:0x000000070d1d38 ...>, @cond=#<Thread::ConditionVariable:0x000000070d1bd0>>, @committed=false, @sending=false, @sent=false, @content_type=#<Mime::Type:0x00000002af78f8 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, @charset="utf-8", @cache_control={:no_cache=>true}, @etag=nil>.success?` 
to return true, got false 

SOLUTION 這個測試不拋光(還),但至少現在通過。

describe "User API get 1 user object", type: :request do 
    it 'Get sends back one User object ' do    
     @env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw) 
       FactoryGirl.create(:user) 

     get "/api/1/user", {}, @env 

     JSON.parse(response.body) 
     expect(response).to be_success 
     expect(response.status).to eq 200 
    end  
end  

回答

1

仔細閱讀錯誤:undefined method `env' for nil:NilClass表示request爲零。您是否試圖在之前設置標題,然後在測試中定義請求?

您可能想查看關於如何設置標題的文檔for an example

如果仍然卡住,也可以發佈其中一個測試。

+0

不錯!肯定的是,標題設置有點奇怪。是的,我認爲在請求中定義頭部會更好。我改變了http認證的設置,但我仍然堅持。仍然訪問被拒絕,我不知道爲什麼。我更新我的問題。謝謝! – theDrifter

+0

您可以使用[let block](http://betterspecs.org/#let)在測試間共享標題以保持乾爽;)而且,在您更新的代碼中,您似乎並沒有將標題變量作爲get方法的一個參數?這將解釋爲什麼訪問仍然被拒絕。贊同或標記我接受的答案當然是歡迎:) – Rafe

+0

感謝您的區塊鏈接。我會把它弄乾,然後讓它運行。是的,我的眼睛現在幾乎是平方。我真的忽視了通過請求的頭部。 (;但仍然不會成功,我仍然得到錯誤訪問被拒絕...... – theDrifter

0

這行看起來可疑:

request.ENV['HTTP_AUTHORIZATION'] =ActionController::HttpAuthentication::Basic.encode_credentials(user,pw) 

你肯定"ENV"應該大寫?我認爲它應該寫成像"env"

+0

哦,是的,我原本是用小寫字母寫的,但有人建議嘗試大寫。但通過'request.env ['HTTP_AUTHORIZATION']'或'request.env ['HTTP_AUTHORIZATION']' – theDrifter

+0

獲得相同的錯誤消息,您是否嘗試過添加'infer_spec_type_from_file_location!'到rails_helper文件的配置部分? –