当我认为它应该是一个可靠的 200 Rspec 3.0 时获得 401 Unauthorized
Getting 401 Unauthorized when I believe it should be a solid 200 Rspec 3.0
我正在尝试进行以下 RSpec 测试:
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
它死了,在这个方法中尝试 post 时:
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",
我可以从第三方应用程序发出此请求并从 post 获取信息时获得 200,当我尝试从应用程序自身在测试中执行此操作时它失败并给我一个 401 .我是不是把headers设置错了?
如您的输出所示,您正在 session 中设置授权和内容类型,而不是 headers。
要在规范中设置 headers,您只需这样做
request.headers["Authorization"] = "..."
request.headers["Content-Type"] = "..."
在您发送请求之前。
我正在尝试进行以下 RSpec 测试:
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
它死了,在这个方法中尝试 post 时:
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",
我可以从第三方应用程序发出此请求并从 post 获取信息时获得 200,当我尝试从应用程序自身在测试中执行此操作时它失败并给我一个 401 .我是不是把headers设置错了?
如您的输出所示,您正在 session 中设置授权和内容类型,而不是 headers。
要在规范中设置 headers,您只需这样做
request.headers["Authorization"] = "..."
request.headers["Content-Type"] = "..."
在您发送请求之前。