加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 无法在Minitest中发送请求标头

发布时间:2020-12-17 01:42:40 所属栏目:百科 来源:网络整理
导读:我正在尝试使用Minitest中的自定义标头发送请求: get '/api/v1/tasks/all',nil,{:authorization = 'sdlfjkvnsdfljn'} 但由于某种原因,请求标头未被发送. 这是request.headers.env的值: "{"rack.version"=[1,2],"rack.input"=#StringIO:0x000000055a55f0,"r
我正在尝试使用Minitest中的自定义标头发送请求:

get '/api/v1/tasks/all',nil,{:authorization => 'sdlfjkvnsdfljn'}

但由于某种原因,请求标头未被发送.
这是request.headers.env的值:

"{"rack.version"=>[1,2],"rack.input"=>#<StringIO:0x000000055a55f0>,"rack.errors"=>#<StringIO:0x000000055a5708>,"rack.multithread"=>true,"rack.multiprocess"=>true,"rack.run_once"=>false,"REQUEST_METHOD"=>"GET","SERVER_NAME"=>"example.org","SERVER_PORT"=>"80","QUERY_STRING"=>"","PATH_INFO"=>"/api/v1/tasks/all","rack.url_scheme"=>"http","HTTPS"=>"off","SCRIPT_NAME"=>"","CONTENT_LENGTH"=>"0","rack.test"=>true,"REMOTE_ADDR"=>"127.0.0.1","HTTP_HOST"=>"example.org","HTTP_COOKIE"=>"","ORIGINAL_FULLPATH"=>"/api/v1/tasks/all","ORIGINAL_SCRIPT_NAME"=>"","action_dispatch.routes"=>#<ActionDispatch::Routing::RouteSet:0x00000005183788>,"action_dispatch.parameter_filter"=>[:password],"action_dispatch.redirect_filter"=>[],"action_dispatch.secret_token"=>"21a9fdaa0b07f7e418e9a7c838e124fd1f3fbd2b65fb1520f8d38e10d643ce33cc46995f359bd38bfbc9335905fbdeb8af6879ebae131d3fe76f69a9619f6b37","action_dispatch.secret_key_base"=>"a021d8b08999858216f202033b8124c4c8ec34590c3c62bec68e08e37526176c64d3d396b1e9109f74966b22056a65f557d6fd761093992d5641eb8a741bfc39","action_dispatch.show_exceptions"=>false,"action_dispatch.show_detailed_exceptions"=>true,"action_dispatch.logger"=>#<ActiveSupport::Logger:0x00000005103e70 @progname=nil,@level=0,@default_formatter=#<Logger::Formatter:0x00000005103df8 @datetime_format=nil>,@formatter=#<ActiveSupport::Logger::SimpleFormatter:0x00000005c487b8 @datetime_format=nil>,@logdev=#<Logger::LogDevice:0x00000005103da8 @shift_size=nil,@shift_age=nil,@filename=nil,@dev=#<File:/home/sergey/Desktop/git2/bo.crmtronic.com/log/test.log>,@mutex=#<Logger::LogDevice::LogDeviceMutex:0x00000005103d80 @mon_owner=nil,@mon_count=0,@mon_mutex=#<Mutex:0x00000005103d30>>>>,"action_dispatch.backtrace_cleaner"=>#<Rails::BacktraceCleaner:0x000000050f0d48 @filters=[#<Proc:0x000000050f0c80@/home/sergey/.rvm/gems/ruby-2.1.2/gems/railties-4.1.1/lib/rails/backtrace_cleaner.rb:10>,#<Proc:0x000000050f0c30@/home/sergey/.rvm/gems/ruby-2.1.2/gems/railties-4.1.1/lib/rails/backtrace_cleaner.rb:11>,#<Proc:0x000000050f0c08@/home/sergey/.rvm/gems/ruby-2.1.2/gems/railties-4.1.1/lib/rails/backtrace_cleaner.rb:12>,#<Proc:0x000000050f03c0@/home/sergey/.rvm/gems/ruby-2.1.2/gems/railties-4.1.1/lib/rails/backtrace_cleaner.rb:24>],@silencers=[#<Proc:0x000000050f0398@/home/sergey/.rvm/gems/ruby-2.1.2/gems/railties-4.1.1/lib/rails/backtrace_cleaner.rb:15>]>,"action_dispatch.key_generator"=>#<ActiveSupport::CachingKeyGenerator:0x000000055a0230 @key_generator=#<ActiveSupport::KeyGenerator:0x000000055a0280 @secret="a021d8b08999858216f202033b8124c4c8ec34590c3c62bec68e08e37526176c64d3d396b1e9109f74966b22056a65f557d6fd761093992d5641eb8a741bfc39",@iterations=1000>,@cache_keys=#<ThreadSafe::Cache:0x000000055a0208 @backend={"encrypted cookie64"=>"xD6QxF1xD9xA6wx18x80xB1,xE3xB6x8EQxC0'.-xE5xA3x80$xD8xADxD8nxDDx84xCEDxE4qxDAxC8WxB5xE7mxE8axC6xD8rxA0xA1QxC5ugxD5Rx9Cx96xE7vIZCxACxEBx92hbx89","signed encrypted cookie64"=>" xA2xAFtxADr_x1Ax87qx00xC0x8DO2xEFx1ElZ5xB6xBASx9Emx16crMxECxC5xEFxD4x8ExDCCxDBxA5KxE88xCFlx1Fx86ex99xC0xD0xEFxB9:xAAxE2YxB9hxF3xB4Mzx9E] "},@default_proc=nil>>,"action_dispatch.http_auth_salt"=>"http authentication","action_dispatch.signed_cookie_salt"=>"signed cookie","action_dispatch.encrypted_cookie_salt"=>"encrypted cookie","action_dispatch.encrypted_signed_cookie_salt"=>"signed encrypted cookie","action_dispatch.cookies_serializer"=>:json,"ROUTES_42736580_SCRIPT_NAME"=>"","action_dispatch.request_id"=>"335f502d-c3be-44af-9463-89f75d6edc5a","action_dispatch.remote_ip"=>#<ActionDispatch::RemoteIp::GetIp:0x0000000558bcb8 @env={...},@check_ip=true,@proxies=/       ^127.0.0.1$               | # localhost IPv4       ^::1$                        | # localhost IPv6       ^fc00:                        | # private IPv6 range fc00       ^10.                         | # private IPv4 range 10.x.x.x       ^172.(1[6-9]|2[0-9]|3[0-1]).| # private IPv4 range 172.16.0.0 .. 172.31.255.255       ^192.168.                     # private IPv4 range 192.168.x.x     /x,@ip="127.0.0.1">,"rack.session"=>#<ActionDispatch::Request::Session:0x558b8f8 not yet loaded>,"rack.session.options"=>#<ActionDispatch::Request::Session::Options:0x0000000558b740 @by=#<ActionDispatch::Session::CookieStore:0x000000055603b0 @app=#<ActionDispatch::Flash:0x000000055603d8 @app=#<ActionDispatch::ParamsParser:0x00000005560568 @app=#<Rack::Head:0x000000055605e0 @app=#<Rack::ConditionalGet:0x00000005560658 @app=#<Rack::ETag:0x000000055606a8 @app=#<Warden::Manager:0x000000055608b0 @config={:default_scope=>:user,:scope_defaults=>{},:default_strategies=>{:user=>[:rememberable,:database_authenticatable]},:intercept_401=>false,:failure_app=>#<Devise::Delegator:0x00000001a05b08>},@app=#<ActionDispatch::Routing::RouteSet:0x00000005183788>>,@cache_control="max-age=0,private,must-revalidate",@no_cache_control="no-cache">>>,@parsers={#<Mime::Type:0x000000019a2710 @synonyms=["text/x-json","application/jsonrequest"],@symbol=:json,@string="application/json">=>:json}>>,@default_options={:path=>"/",:domain=>nil,:expire_after=>nil,:secure=>false,:httponly=>true,:defer=>false,:renew=>false},@key="_bo_crmtronic_com_session",@cookie_only=true>,@env={...},@delegate={:path=>"/",:renew=>false,:id=>nil}>,"warden"=>Warden::Proxy:44849780 @config={:default_scope=>:user,"action_dispatch.request.path_parameters"=>{:format=>"json",:controller=>"api/v1/tasks",:action=>"all"},"action_controller.instance"=>#<Api::V1::TasksController:0x000000050f1d60 @_action_has_layout=true,@_routes=nil,@_headers={"Content-Type"=>"text/html"},@_status=200,@_request=#<ActionDispatch::Request:0x000000050f1b58 @env={...},@symbolized_path_params=nil,@filtered_parameters={"format"=>"json","controller"=>"api/v1/tasks","action"=>"all"},@filtered_env=nil,@filtered_path=nil,@protocol=nil,@port=nil,@method="GET",@request_method="GET",@remote_ip=nil,@original_fullpath=nil,@fullpath="/api/v1/tasks/all",@ip=nil,@uuid=nil>,@_response=#<ActionDispatch::Response:0x000000050f1ab8 @mon_owner=nil,@mon_mutex=#<Mutex:0x000000050f19f0>,@stream=#<ActionDispatch::Response::Buffer:0x000000050f1810 @response=#<ActionDispatch::Response:0x000000050f1ab8 ...>,@buf=[],@closed=false>,@header={"X-Frame-Options"=>"SAMEORIGIN","X-XSS-Protection"=>"1; mode=block","X-Content-Type-Options"=>"nosniff"},@status=200,@sending_file=false,@blank=false,@cv=#<MonitorMixin::ConditionVariable:0x000000050f17c0 @monitor=#<ActionDispatch::Response:0x000000050f1ab8 ...>,@cond=#<Thread::ConditionVariable:0x000000050f1770>>,@committed=false,@sending=false,@sent=false,@content_type=nil,@charset=nil,@cache_control={},@etag=nil,@request=#<ActionDispatch::Request:0x000000050f1b58 @env={...},@uuid=nil>>,@_env={...},@_prefixes=["api/v1/tasks","application"],@_lookup_context=#<ActionView::LookupContext:0x000000050f12e8 @details_key=nil,@details={:locale=>[:en],:formats=>[:html,:text,:js,:css,:ics,:csv,:vcf,:png,:jpeg,:gif,:bmp,:tiff,:mpeg,:xml,:rss,:atom,:yaml,:multipart_form,:url_encoded_form,:json,:pdf,:zip],:variants=>[],:handlers=>[:erb,:builder,:raw,:ruby,:jbuilder,:coffee]},@skip_default_locale=false,@cache=true,@prefixes=["api/v1/tasks",@rendered_format=nil,@view_paths=#<ActionView::PathSet:0x000000050f11f8 @paths=[#<ActionView::OptimizedFileSystemResolver:0x00000001dc1cd8 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}",@cache=#<ActionView::Resolver::Cache:0x00000001dc1c60 @data=#<ActionView::Resolver::Cache::SmallCache:0x00000001dc1c38 @backend={},@default_proc=#<Proc:0x000000054a1398@/home/sergey/.rvm/gems/ruby-2.1.2/gems/actionview-4.1.1/lib/action_view/template/resolver.rb:48 (lambda)>>>,@path="/home/sergey/Desktop/git2/bo.crmtronic.com/app/views">,#<ActionView::OptimizedFileSystemResolver:0x00000001dc2c28 @pattern=":prefix/:action{.:locale,@cache=#<ActionView::Resolver::Cache:0x00000001dc2c00 @data=#<ActionView::Resolver::Cache::SmallCache:0x00000001dc2b38 @backend={},@path="/home/sergey/.rvm/gems/ruby-2.1.2/gems/devise-3.2.4/app/views">]>>,@_action_name="all",@_response_body=nil,@marked_for_same_origin_verification=true,@_config={},@_params={"format"=>"json","action"=>"all"}>,"action_dispatch.request.content_type"=>nil,"action_dispatch.request.request_parameters"=>{},"rack.request.query_string"=>"","rack.request.query_hash"=>{},"action_dispatch.request.query_parameters"=>{},"action_dispatch.request.parameters"=>{"format"=>"json","action_dispatch.request.formats"=>[#<Mime::Type:0x000000019a2710 @synonyms=["text/x-json",@string="application/json">],"rack.request.cookie_hash"=>{},"rack.request.cookie_string"=>"","action_dispatch.cookies"=>#<ActionDispatch::Cookies::CookieJar:0x000000050c1e30 @key_generator=#<ActiveSupport::CachingKeyGenerator:0x000000055a0230 @key_generator=#<ActiveSupport::KeyGenerator:0x000000055a0280 @secret="a021d8b08999858216f202033b8124c4c8ec34590c3c62bec68e08e37526176c64d3d396b1e9109f74966b22056a65f557d6fd761093992d5641eb8a741bfc39",@set_cookies={"request_method"=>{:value=>"GET",:path=>"/"}},@delete_cookies={},@host="example.org",@secure=false,@options={:signed_cookie_salt=>"signed cookie",:encrypted_cookie_salt=>"encrypted cookie",:encrypted_signed_cookie_salt=>"signed encrypted cookie",:secret_token=>"21a9fdaa0b07f7e418e9a7c838e124fd1f3fbd2b65fb1520f8d38e10d643ce33cc46995f359bd38bfbc9335905fbdeb8af6879ebae131d3fe76f69a9619f6b37",:secret_key_base=>"a021d8b08999858216f202033b8124c4c8ec34590c3c62bec68e08e37526176c64d3d396b1e9109f74966b22056a65f557d6fd761093992d5641eb8a741bfc39",:upgrade_legacy_signed_cookies=>true,:serializer=>:json},@cookies={"request_method"=>"GET"},@encrypted=#<ActionDispatch::Cookies::UpgradeLegacyEncryptedCookieJar:0x000000050c13b8 @parent_jar=#<ActionDispatch::Cookies::CookieJar:0x000000050c1e30 ...>,@encryptor=#<ActiveSupport::MessageEncryptor:0x000000050891c0 @secret="xD6QxF1xD9xA6wx18x80xB1,@sign_secret=" xA2xAFtxADr_x1Ax87qx00xC0x8DO2xEFx1ElZ5xB6xBASx9Emx16crMxECxC5xEFxD4x8ExDCCxDBxA5KxE88xCFlx1Fx86ex99xC0xD0xEFxB9:xAAxE2YxB9hxF3xB4Mzx9E] ",@cipher="aes-256-cbc",@verifier=#<ActiveSupport::MessageVerifier:0x00000005089120 @secret=" xA2xAFtxADr_x1Ax87qx00xC0x8DO2xEFx1ElZ5xB6xBASx9Emx16crMxECxC5xEFxD4x8ExDCCxDBxA5KxE88xCFlx1Fx86ex99xC0xD0xEFxB9:xAAxE2YxB9hxF3xB4Mzx9E] ",@digest="SHA1",@serializer=ActiveSupport::MessageEncryptor::NullSerializer>,@serializer=ActionDispatch::Cookies::NullSerializer>,@legacy_verifier=#<ActiveSupport::MessageVerifier:0x000000050890a8 @secret="21a9fdaa0b07f7e418e9a7c838e124fd1f3fbd2b65fb1520f8d38e10d643ce33cc46995f359bd38bfbc9335905fbdeb8af6879ebae131d3fe76f69a9619f6b37",@serializer=Marshal>>,@signed_or_encrypted=#<ActionDispatch::Cookies::UpgradeLegacyEncryptedCookieJar:0x000000050c13b8 @parent_jar=#<ActionDispatch::Cookies::CookieJar:0x000000050c1e30 ...>,@serializer=Marshal>>>,"action_dispatch.request.unsigned_session_cookie"=>{}}"

如何在Minitest中发送请求标头?

解决方法

您可以将其更改为:

get '/api/v1/tasks/all',{ "HTTP_AUTHORIZATION" => "Token token=1111" }

查看来源的示例:MiniTest Gem Test Files

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读