Ruby on rails 如何在集成测试中通过授权令牌头?
A 这意味着我可以在我的 集成测试,如下所示:Ruby on rails 如何在集成测试中通过授权令牌头?,ruby-on-rails,ruby-on-rails-4,minitest,Ruby On Rails,Ruby On Rails 4,Minitest,A 这意味着我可以在我的 集成测试,如下所示: get "/v1/sites", nil, :authorization => "foo" assert_response :success 由于某些原因,标题无法访问我的应用程序: get "/v1/sites", nil, :authorization => "foo" assert_match response.headers, /foo/ Expected {"X-Frame-Options"=>"SAMEORIGIN
get "/v1/sites", nil, :authorization => "foo"
assert_response :success
由于某些原因,标题无法访问我的应用程序:
get "/v1/sites", nil, :authorization => "foo"
assert_match response.headers, /foo/
Expected {"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff", "X-UA-Compatible"=>"chrome=1", "WWW-Authenticate"=>"Token realm=\"Application\"", "Content-Type"=>"text/html; charset=utf-8", "Cache-Control"=>"no-cache", "X-Request-Id"=>"23915302-9cfe-424d-86fe-5d60bc0d6b2c", "X-Runtime"=>"0.054857", "Content-Length"=>"27"} to match /foo/.
授权标头无法通过,我可以在控制器中放置抛出响应.headers
时确认。当我
用卷曲进行测试,我确实看到头部穿过。我在那里
甚至可以设置令牌并获得访问权限。来自
控制器为:
module V1
class SitesController < ApplicationController
before_filter :restrict_access, :only => :index
def index
head :success
end
private
def restrict_access
authenticate_or_request_with_http_token do |token, options|
token == "foo"
end
end
end
end
模块V1
类SitesController:index
def索引
头:成功
结束
私有的
def限制用户访问
使用_http _tokendo | token,选项验证_或_请求|
令牌==“foo”
结束
结束
结束
结束
这是在Rails 4上使用
作为参考,这里是中间件堆栈,它比大多数默认的Rails应用程序瘦得多
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x992cd28>
use Rack::Runtime
use ActionDispatch::RequestId
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use ActionDispatch::RemoteIp
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActiveRecord::Migration::CheckPending
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::ParamsParser
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
run MyApp::Application.routes
使用ActionDispatch::Static
使用Rack::Lock
使用#
使用Rack::Runtime
使用ActionDispatch::RequestId
使用Rails::Rack::Logger
使用ActionDispatch::ShowExceptions
使用ActionDispatch::DebugExceptions
使用ActionDispatch::RemoteIp
使用ActionDispatch::Reloader
使用ActionDispatch::回调
使用ActiveRecord::Migration::CheckPending
使用ActiveRecord::ConnectionAdapters::ConnectionManagement
使用ActiveRecord::QueryCache
使用ActionDispatch::ParamSpaser
使用Rack::Head
使用Rack::ConditionalGet
使用Rack::ETag
运行MyApp::Application.routes
您可以在执行请求之前在请求对象上设置头
request.env['HTTP_AUTHORIZATION'] = 'foo'
get '/v1/sites'
assert_response :success
仅供参考。一切都是对的,我只是愚蠢地在调试时测试错误的东西:
assert_match response.headers, /foo/
显然是错误的,因为这是响应。正确的方法是测试请求
get "/v1/sites", nil, :authorization => %{Token token="foo"}
assert_includes request.headers["HTTP_AUTHORIZATION"], "foo"
这一切都很顺利。不幸的是,没有。在IntegrationTest中,
request
和@request
均未设置或不可用:均为零。不过,这/确实/适用于控制器测试。你是对的抱歉,我在考虑控制器测试。您提到的代码应该与ActionDispatch::Integration::ResquestHelpers()一起使用,您正在使用它吗?似乎我正在调用该方法,是的self.method(:get).owner
reportsActionDispatch::Integration::Runner
。对于Rails 5/5.1,使用headers:
关键字参数(不需要nil)<代码>获取“/v1/sites”,标题:{“HTTP_AUTHORIZATION”=>“Token-Token=1111”}