2015-02-06 37 views
0

我嘗試做以下RSpec的測試未經授權:獲得401時,我認爲它應該是一個堅實的200 Rspec的3.0

it "should publish the request information" do 
    post :create, { 
    :response_code => 200, 
    :response_base_url => 'http://google.ca', 
    :response_headers => {:some_site => 'some_site'}, 
    :publish_to_site => {:site_name => 'sample'} 
    }, { 
    "Authorization" => "Token token=\"#{YAML.load(File.read('config/application.yml'))['TEST_APP_API_KEY'].to_s}\"", 
    'Content-Type' => 'application/json' 
    } 

    expect(response.status).to eql 200 
end 

我相信這應該通過,因爲API密鑰已經存在於系統中,但而不是回來的響應代碼是401

死了,試圖在這一方法發佈時:

def restrict_application_api_access 
     authenticate_or_request_with_http_token do |token, options| 
     @api_key = ApplicationApiKey.find_by(api_key: token) 
     end 
    end 

它死亡的具體地點是:

 # Right here ... 
     authenticate_or_request_with_http_token do |token, options| 
     @api_key = ApplicationApiKey.find_by(api_key: token) 
     end 

它從來沒有進入塊。 。

所以在這之前,我做了binding.pry探討未來在請求信息有趣的部分是:

"rack.session"=> 
    {"Authorization"=>"Token token=\"################################\"", "Content-Type"=>"application/json"}, 

授權令牌被通入,內容類型設置,參數甚至設置得:

"action_dispatch.request.request_parameters"=> 
    {"response_code"=>"200", 
    "response_base_url"=>"http://google.ca", 
    "response_headers"=>{"some_site"=>"some_site"}, 
    "publish_to_site"=>{"site_name"=>"sample"}}, 
    "action_dispatch.request.path_parameters"=>{"controller"=>"api/internal/v1/response_analytics", "action"=>"create"}, 
    "action_dispatch.request.flash_hash"=>#<ActionDispatch::Flash::FlashHash:0x007ff84a08c380 @discard=#<Set: {}>, @flashes={}, @now=nil>, 
    "PATH_INFO"=>"/internal/v1/response_analytics", 

我可以從第三方應用這一請求,並可以發佈信息,當我試圖得到一個200,做它從應用程序自身在測試中失敗,並給了我一個401我是否設置標題錯誤?

回答

1

由於您的輸出顯示您正在設置會話中的授權和內容類型而不是標題。

要設置頭的規格你只是做

request.headers["Authorization"] = "..." 
request.headers["Content-Type"] = "..." 

在你發送請求。

相關問題