How to use method method of Header Package

Best Vcr_ruby code snippet using Header.method


Source:request.rb Github


Full Screen

...45 new({})46 end47 def initialize(env)48 super49 @method = nil50 @request_method = nil51 @remote_ip = nil52 @original_fullpath = nil53 @fullpath = nil54 @ip = nil55 end56 def commit_cookie_jar! # :nodoc:57 end58 PASS_NOT_FOUND = { # :nodoc:59 def self.action(_); self; end60 def; [404, {'X-Cascade' => 'pass'}, []]; end61 }62 def controller_class63 params = path_parameters64 if params.key?(:controller)65 controller_param = params[:controller].underscore66 params[:action] ||= 'index'67 const_name = "#{controller_param.camelize}Controller"68 ActiveSupport::Dependencies.constantize(const_name)69 else70 PASS_NOT_FOUND71 end72 end73 def key?(key)74 has_header? key75 end76 # List of HTTP request methods from the following RFCs:77 # Hypertext Transfer Protocol -- HTTP/1.1 ( # HTTP Extensions for Distributed Authoring -- WEBDAV ( # Versioning Extensions to WebDAV ( # Ordered Collections Protocol (WebDAV) ( # Web Distributed Authoring and Versioning (WebDAV) Access Control Protocol ( # Web Distributed Authoring and Versioning (WebDAV) SEARCH ( # Calendar Extensions to WebDAV ( # PATCH Method for HTTP ( RFC2616 = %w(OPTIONS GET HEAD POST PUT DELETE TRACE CONNECT)86 RFC2518 = %w(PROPFIND PROPPATCH MKCOL COPY MOVE LOCK UNLOCK)87 RFC3253 = %w(VERSION-CONTROL REPORT CHECKOUT CHECKIN UNCHECKOUT MKWORKSPACE UPDATE LABEL MERGE BASELINE-CONTROL MKACTIVITY)88 RFC3648 = %w(ORDERPATCH)89 RFC3744 = %w(ACL)90 RFC5323 = %w(SEARCH)91 RFC4791 = %w(MKCALENDAR)92 RFC5789 = %w(PATCH)93 HTTP_METHODS = RFC2616 + RFC2518 + RFC3253 + RFC3648 + RFC3744 + RFC5323 + RFC4791 + RFC578994 HTTP_METHOD_LOOKUP = {}95 # Populate the HTTP method lookup cache96 HTTP_METHODS.each { |method|97 HTTP_METHOD_LOOKUP[method] = method.underscore.to_sym98 }99 # Returns the HTTP \method that the application should see.100 # In the case where the \method was overridden by a middleware101 # (for instance, if a HEAD request was converted to a GET,102 # or if a _method parameter was used to determine the \method103 # the application should use), this \method returns the overridden104 # value, not the original.105 def request_method106 @request_method ||= check_method(super)107 end108 def routes # :nodoc:109 get_header("action_dispatch.routes".freeze)110 end111 def routes=(routes) # :nodoc:112 set_header("action_dispatch.routes".freeze, routes)113 end114 def engine_script_name(_routes) # :nodoc:115 get_header(_routes.env_key)116 end117 def engine_script_name=(name) # :nodoc:118 set_header(routes.env_key, name.dup)119 end120 def request_method=(request_method) #:nodoc:121 if check_method(request_method)122 @request_method = set_header("REQUEST_METHOD", request_method)123 end124 end125 def controller_instance # :nodoc:126 get_header('action_controller.instance'.freeze)127 end128 def controller_instance=(controller) # :nodoc:129 set_header('action_controller.instance'.freeze, controller)130 end131 def http_auth_salt132 get_header "action_dispatch.http_auth_salt"133 end134 def show_exceptions? # :nodoc:135 # We're treating `nil` as "unset", and we want the default setting to be136 # `true`. This logic should be extracted to `env_config` and calculated137 # once.138 !(get_header('action_dispatch.show_exceptions'.freeze) == false)139 end140 # Returns a symbol form of the #request_method141 def request_method_symbol142 HTTP_METHOD_LOOKUP[request_method]143 end144 # Returns the original value of the environment's REQUEST_METHOD,145 # even if it was overridden by middleware. See #request_method for146 # more information.147 def method148 @method ||= check_method(get_header("rack.methodoverride.original_method") || get_header('REQUEST_METHOD'))149 end150 # Returns a symbol form of the #method151 def method_symbol152 HTTP_METHOD_LOOKUP[method]153 end154 # Provides access to the request's HTTP headers, for example:155 #156 # request.headers["Content-Type"] # => "text/plain"157 def headers158 @headers ||= end160 # Returns a +String+ with the last requested path including their params.161 #162 # # get '/foo'163 # request.original_fullpath # => '/foo'164 #165 # # get '/foo?bar'166 # request.original_fullpath # => '/foo?bar'167 def original_fullpath168 @original_fullpath ||= (get_header("ORIGINAL_FULLPATH") || fullpath)169 end170 # Returns the +String+ full path including params of the last URL requested.171 #172 # # get "/articles"173 # request.fullpath # => "/articles"174 #175 # # get "/articles?page=2"176 # request.fullpath # => "/articles?page=2"177 def fullpath178 @fullpath ||= super179 end180 # Returns the original request URL as a +String+.181 #182 # # get "/articles?page=2"183 # request.original_url # => ""184 def original_url185 base_url + original_fullpath186 end187 # The +String+ MIME type of the request.188 #189 # # get "/articles"190 # request.media_type # => "application/x-www-form-urlencoded"191 def media_type192 content_mime_type.to_s193 end194 # Returns the content length of the request as an integer.195 def content_length196 super.to_i197 end198 # Returns true if the "X-Requested-With" header contains "XMLHttpRequest"199 # (case-insensitive), which may need to be manually added depending on the200 # choice of JavaScript libraries and frameworks.201 def xml_http_request?202 get_header('HTTP_X_REQUESTED_WITH') =~ /XMLHttpRequest/i203 end204 alias :xhr? :xml_http_request?205 # Returns the IP address of client as a +String+.206 def ip207 @ip ||= super208 end209 # Returns the IP address of client as a +String+,210 # usually set by the RemoteIp middleware.211 def remote_ip212 @remote_ip ||= (get_header("action_dispatch.remote_ip") || ip).to_s213 end214 def remote_ip=(remote_ip)215 set_header "action_dispatch.remote_ip".freeze, remote_ip216 end217 ACTION_DISPATCH_REQUEST_ID = "action_dispatch.request_id".freeze # :nodoc:218 # Returns the unique request id, which is based on either the X-Request-Id header that can219 # be generated by a firewall, load balancer, or web server or by the RequestId middleware220 # (which sets the action_dispatch.request_id environment variable).221 #222 # This unique ID is useful for tracing a request from end-to-end as part of logging or debugging.223 # This relies on the rack variable set by the ActionDispatch::RequestId middleware.224 def request_id225 get_header ACTION_DISPATCH_REQUEST_ID226 end227 def request_id=(id) # :nodoc:228 set_header ACTION_DISPATCH_REQUEST_ID, id229 end230 alias_method :uuid, :request_id231 # Returns the lowercase name of the HTTP server software.232 def server_software233 (get_header('SERVER_SOFTWARE') && /^([a-zA-Z]+)/ =~ get_header('SERVER_SOFTWARE')) ? $1.downcase : nil234 end235 # Read the request \body. This is useful for web services that need to236 # work with raw requests directly.237 def raw_post238 unless has_header? 'RAW_POST_DATA'239 raw_post_body = body240 set_header('RAW_POST_DATA', raw_post_body.rewind if raw_post_body.respond_to?(:rewind)242 end243 get_header 'RAW_POST_DATA'244 end245 # The request body is an IO input stream. If the RAW_POST_DATA environment246 # variable is already set, wrap it in a StringIO.247 def body248 if raw_post = get_header('RAW_POST_DATA')249 raw_post.force_encoding(Encoding::BINARY)250 else252 body_stream253 end254 end255 # Determine whether the request body contains form-data by checking256 # the request Content-Type for one of the media-types:257 # "application/x-www-form-urlencoded" or "multipart/form-data". The258 # list of form-data media types can be modified through the259 # +FORM_DATA_MEDIA_TYPES+ array.260 #261 # A request body is not assumed to contain form-data when no262 # Content-Type header is provided and the request_method is POST.263 def form_data?264 FORM_DATA_MEDIA_TYPES.include?(media_type)265 end266 def body_stream #:nodoc:267 get_header('rack.input')268 end269 # TODO This should be broken apart into AD::Request::Session and probably270 # be included by the session middleware.271 def reset_session272 if session && session.respond_to?(:destroy)273 session.destroy274 else275 self.session = {}276 end277 end278 def session=(session) #:nodoc:279 Session.set self, session280 end281 def session_options=(options)282 Session::Options.set self, options283 end284 # Override Rack's GET method to support indifferent access285 def GET286 fetch_header("action_dispatch.request.query_parameters") do |k|287 rack_query_params = super || {}288 # Check for non UTF-8 parameter values, which would cause errors later289 Request::Utils.check_param_encoding(rack_query_params)290 set_header k, Request::Utils.normalize_encode_params(rack_query_params)291 end292 rescue Rack::Utils::ParameterTypeError, Rack::Utils::InvalidParameterError => e293 raise"Invalid query parameters: #{e.message}")294 end295 alias :query_parameters :GET296 # Override Rack's POST method to support indifferent access297 def POST298 fetch_header("action_dispatch.request.request_parameters") do299 pr = parse_formatted_parameters(params_parsers) do |params|300 super || {}301 end302 self.request_parameters = Request::Utils.normalize_encode_params(pr)303 end304 rescue ParamsParser::ParseError # one of the parse strategies blew up305 self.request_parameters = Request::Utils.normalize_encode_params(super || {})306 raise307 rescue Rack::Utils::ParameterTypeError, Rack::Utils::InvalidParameterError => e308 raise"Invalid request parameters: #{e.message}")309 end310 alias :request_parameters :POST311 # Returns the authorization header regardless of whether it was specified directly or through one of the312 # proxy alternatives.313 def authorization314 get_header('HTTP_AUTHORIZATION') ||315 get_header('X-HTTP_AUTHORIZATION') ||316 get_header('X_HTTP_AUTHORIZATION') ||317 get_header('REDIRECT_X_HTTP_AUTHORIZATION')318 end319 # True if the request came from localhost,, or ::1.320 def local?321 LOCALHOST =~ remote_addr && LOCALHOST =~ remote_ip322 end323 def request_parameters=(params)324 raise if params.nil?325 set_header("action_dispatch.request.request_parameters".freeze, params)326 end327 def logger328 get_header("action_dispatch.logger".freeze)329 end330 def commit_flash331 end332 def ssl?333 super || scheme == 'wss'.freeze334 end335 private336 def check_method(name)337 HTTP_METHOD_LOOKUP[name] || raise(ActionController::UnknownHttpMethod, "#{name}, accepted HTTP methods are #{HTTP_METHODS[0...-1].join(', ')}, and #{HTTP_METHODS[-1]}")338 name339 end340 end341end...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?