ber*_*kes 6 ruby-on-rails minitest ruby-on-rails-4
一个相关的问题 意味着我可以在我的集成测试中使用令牌认证来测试请求,如下所示:
get "/v1/sites", nil, :authorization => "foo"
assert_response :success
Run Code Online (Sandbox Code Playgroud)
出于某种原因,标题无法访问我的应用程序:
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/.
Run Code Online (Sandbox Code Playgroud)
授权标头无法通过,我可以throw response.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
Run Code Online (Sandbox Code Playgroud)
这是最小的,在Rails 4上,使用 Rails-API
作为参考,这里是中间件堆栈,它比大多数默认的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
Run Code Online (Sandbox Code Playgroud)
仅供参考.一切都是正确的,我只是在做傻事,并在调试时测试错误的东西:
assert_match response.headers, /foo/
Run Code Online (Sandbox Code Playgroud)
显然是假的,因为这是回应.正确的是测试请求
get "/v1/sites", nil, :authorization => %{Token token="foo"}
assert_includes request.headers["HTTP_AUTHORIZATION"], "foo"
Run Code Online (Sandbox Code Playgroud)
这传递得很好.
| 归档时间: |
|
| 查看次数: |
7789 次 |
| 最近记录: |