无法在Minitest中发送请求标头

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法在Minitest中发送请求标头相关的知识,希望对你有一定的参考价值。

我正在尝试使用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"=>"\xD6Q\xF1\xD9\xA6w\x18\x80\xB1,\xE3\xB6\x8EQ\xC0'.-\xE5\xA3\x80$\xD8\xAD\xD8\n\xDD\x84\xCED\xE4q\xDA\xC8W\xB5\xE7m\xE8a\xC6\xD8\r\xA0\xA1Q\xC5ug\xD5R\x9C\x96\xE7vIZC\xAC\xEB\x92h\b\x89", "signed encrypted cookie64"=>" \xA2\xAF\t\xADr_\x1A\x87q\x00\xC0\x8DO2\xEF\x1ElZ5\xB6\xBAS\x9Em\x16crM\xEC\xC5\xEF\xD4\x8E\xDCC\xDB\xA5K\xE88\xCFl\x1F\x86\e\x99\xC0\xD0\xEF\xB9:\xAA\xE2Y\xB9h\xF3\xB4Mz\x9E] ", @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=>"/", :domain=>nil, :expire_after=>nil, :secure=>false, :httponly=>true, :defer=>false, :renew=>false, :id=>nil>, "warden"=>Warden::Proxy:44849780 @config=:default_scope=>:user, :scope_defaults=>, :default_strategies=>:user=>[:rememberable, :database_authenticatable], :intercept_401=>false, :failure_app=>#<Devise::Delegator:0x00000001a05b08>, "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_count=0, @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=..., @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>>, @_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", "application"], @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,.:formats,+:variants,.:handlers,", @cache=#<ActionView::Resolver::Cache:0x00000001dc2c00 @data=#<ActionView::Resolver::Cache::SmallCache:0x00000001dc2b38 @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/.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", "controller"=>"api/v1/tasks", "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", "controller"=>"api/v1/tasks", "action"=>"all", "action_dispatch.request.formats"=>[#<Mime::Type:0x000000019a2710 @synonyms=["text/x-json", "application/jsonrequest"], @symbol=: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", @iterations=1000>, @cache_keys=#<ThreadSafe::Cache:0x000000055a0208 @backend="encrypted cookie64"=>"\xD6Q\xF1\xD9\xA6w\x18\x80\xB1,\xE3\xB6\x8EQ\xC0'.-\xE5\xA3\x80$\xD8\xAD\xD8\n\xDD\x84\xCED\xE4q\xDA\xC8W\xB5\xE7m\xE8a\xC6\xD8\r\xA0\xA1Q\xC5ug\xD5R\x9C\x96\xE7vIZC\xAC\xEB\x92h\b\x89", "signed encrypted cookie64"=>" \xA2\xAF\t\xADr_\x1A\x87q\x00\xC0\x8DO2\xEF\x1ElZ5\xB6\xBAS\x9Em\x16crM\xEC\xC5\xEF\xD4\x8E\xDCC\xDB\xA5K\xE88\xCFl\x1F\x86\e\x99\xC0\xD0\xEF\xB9:\xAA\xE2Y\xB9h\xF3\xB4Mz\x9E] ", @default_proc=nil>>, @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", @committed=false, @encrypted=#<ActionDispatch::Cookies::UpgradeLegacyEncryptedCookieJar:0x000000050c13b8 @parent_jar=#<ActionDispatch::Cookies::CookieJar:0x000000050c1e30 ...>, @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, @encryptor=#<ActiveSupport::MessageEncryptor:0x000000050891c0 @secret="\xD6Q\xF1\xD9\xA6w\x18\x80\xB1,\xE3\xB6\x8EQ\xC0'.-\xE5\xA3\x80$\xD8\xAD\xD8\n\xDD\x84\xCED\xE4q\xDA\xC8W\xB5\xE7m\xE8a\xC6\xD8\r\xA0\xA1Q\xC5ug\xD5R\x9C\x96\xE7vIZC\xAC\xEB\x92h\b\x89", @sign_secret=" \xA2\xAF\t\xADr_\x1A\x87q\x00\xC0\x8DO2\xEF\x1ElZ5\xB6\xBAS\x9Em\x16crM\xEC\xC5\xEF\xD4\x8E\xDCC\xDB\xA5K\xE88\xCFl\x1F\x86\e\x99\xC0\xD0\xEF\xB9:\xAA\xE2Y\xB9h\xF3\xB4Mz\x9E] ", @cipher="aes-256-cbc", @verifier=#<ActiveSupport::MessageVerifier:0x00000005089120 @secret=" \xA2\xAF\t\xADr_\x1A\x87q\x00\xC0\x8DO2\xEF\x1ElZ5\xB6\xBAS\x9Em\x16crM\xEC\xC5\xEF\xD4\x8E\xDCC\xDB\xA5K\xE88\xCFl\x1F\x86\e\x99\xC0\xD0\xEF\xB9:\xAA\xE2Y\xB9h\xF3\xB4Mz\x9E] ", @digest="SHA1", @serializer=ActiveSupport::MessageEncryptor::NullSerializer>, @serializer=ActionDispatch::Cookies::NullSerializer>, @legacy_verifier=#<ActiveSupport::MessageVerifier:0x000000050890a8 @secret="21a9fdaa0b07f7e418e9a7c838e124fd1f3fbd2b65fb1520f8d38e10d643ce33cc46995f359bd38bfbc9335905fbdeb8af6879ebae131d3fe76f69a9619f6b37", @digest="SHA1", @serializer=Marshal>>, @signed_or_encrypted=#<ActionDispatch::Cookies::UpgradeLegacyEncryptedCookieJar:0x000000050c13b8 @parent_jar=#<ActionDispatch::Cookies::CookieJar:0x000000050c1e30 ...>, @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, @encryptor=#<ActiveSupport::MessageEncryptor:0x000000050891c0 @secret="\xD6Q\xF1\xD9\xA6w\x18\x80\xB1,\xE3\xB6\x8EQ\xC0'.-\xE5\xA3\x80$\xD8\xAD\xD8\n\xDD\x84\xCED\xE4q\xDA\xC8W\xB5\xE7m\xE8a\xC6\xD8\r\xA0\xA1Q\xC5ug\xD5R\x9C\x96\xE7vIZC\xAC\xEB\x92h\b\x89", @sign_secret=" \xA2\xAF\t\xADr_\x1A\x87q\x00\xC0\x8DO2\xEF\x1ElZ5\xB6\xBAS\x9Em\x16crM\xEC\xC5\xEF\xD4\x8E\xDCC\xDB\xA5K\xE88\xCFl\x1F\x86\e\x99\xC0\xD0\xEF\xB9:\xAA\xE2Y\xB9h\xF3\xB4Mz\x9E] ", @cipher="aes-256-cbc", @verifier=#<ActiveSupport::MessageVerifier:0x00000005089120 @secret=" \xA2\xAF\t\xADr_\x1A\x87q\x00\xC0\x8DO2\xEF\x1ElZ5\xB6\xBAS\x9Em\x16crM\xEC\xC5\xEF\xD4\x8E\xDCC\xDB\xA5K\xE88\xCFl\x1F\x86\e\x99\xC0\xD0\xEF\xB9:\xAA\xE2Y\xB9h\xF3\xB4Mz\x9E] ", @digest="SHA1", @serializer=ActiveSupport::MessageEncryptor::NullSerializer>, @serializer=ActionDispatch::Cookies::NullSerializer>, @legacy_verifier=#<ActiveSupport::MessageVerifier:0x000000050890a8 @secret="21a9fdaa0b07f7e418e9a7c838e124fd1f3fbd2b65fb1520f8d38e10d643ce33cc46995f359bd38bfbc9335905fbdeb8af6879ebae131d3fe76f69a9619f6b37", @digest="SHA1", @serializer=Marshal>>>, "action_dispatch.request.unsigned_session_cookie"=>"

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

答案

您可以将其更改为:

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

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

另一答案

对于Rails 5 / 5.1,请注意弃用警告:

# DEPRECATION WARNING: Using positional arguments in integration tests has been deprecated, in favor of keyword arguments, and will be removed in Rails 5.1    
# Deprecated style:
get "/profile",  id: 1 ,  "X-Extra-Header" => "123" 

# New keyword style:
get "/profile", params:  id: 1 , headers:  "X-Extra-Header" => "123" 

所以你可以像这样使用headers:关键字参数

get v1_tasks_url, headers:  "HTTP_AUTHORIZATION" => "Token token=1111" 
另一答案
get your_path, headers:  'Authorization': 'Bearer TOKEN' 

以上是关于无法在Minitest中发送请求标头的主要内容,如果未能解决你的问题,请参考以下文章

使用原始请求标头 C# 在请求正文中发送参数和文件

我无法在 Angular 4 上向 laravel api 发送带有标头的 http 请求

无法向 Apache 服务器发出 http2 请求,尽管服务器在响应标头中发送“升级:h2”

更改 ajax 请求的 response.statuscode 会引发错误“发送 HTTP 标头后服务器无法设置状态”

来自 IE 或 Firefox 的 CORS 请求中没有 Referer 标头

Angular 7未根据请求发送正确的标头