How to use get_header method of Header Package

Best Vcr_ruby code snippet using Header.get_header


Source:request.rb Github


Full Screen

...37 def has_header?(name)38 @env.key? name39 end40 # Get a request specific value for `name`.41 def get_header(name)42 @env[name]43 end44 # If a block is given, it yields to the block if the value hasn't been set45 # on the request.46 def fetch_header(name, &block)47 @env.fetch(name, &block)48 end49 # Loops through each key / value pair in the request specific data.50 def each_header(&block)51 @env.each(&block)52 end53 # Set a request specific value for `name` to `v`54 def set_header(name, v)55 @env[name] = v56 end57 # Add a header that may have multiple values.58 #59 # Example:60 # request.add_header 'Accept', 'image/png'61 # request.add_header 'Accept', '*/*'62 #63 # assert_equal 'image/png,*/*', request.get_header('Accept')64 #65 # def add_header key, v67 if v.nil?68 get_header key69 elsif has_header? key70 set_header key, "#{get_header key},#{v}"71 else72 set_header key, v73 end74 end75 # Delete a request specific value for `name`.76 def delete_header(name)77 @env.delete name78 end79 def initialize_copy(other)80 @env = other.env.dup81 end82 end83 module Helpers84 # The set of form-data media-types. Requests that do not indicate85 # one of the media types presents in this list will not be eligible86 # for form-data / param parsing.87 FORM_DATA_MEDIA_TYPES = [88 'application/x-www-form-urlencoded',89 'multipart/form-data'90 ]91 # The set of media-types. Requests that do not indicate92 # one of the media types presents in this list will not be eligible93 # for param parsing like soap attachments or generic multiparts94 PARSEABLE_DATA_MEDIA_TYPES = [95 'multipart/related',96 'multipart/mixed'97 ]98 # Default ports depending on scheme. Used to decide whether or not99 # to include the port in a generated URI.100 DEFAULT_PORTS = { 'http' => 80, 'https' => 443, 'coffee' => 80 }101 HTTP_X_FORWARDED_SCHEME = 'HTTP_X_FORWARDED_SCHEME'.freeze102 HTTP_X_FORWARDED_PROTO = 'HTTP_X_FORWARDED_PROTO'.freeze103 HTTP_X_FORWARDED_HOST = 'HTTP_X_FORWARDED_HOST'.freeze104 HTTP_X_FORWARDED_PORT = 'HTTP_X_FORWARDED_PORT'.freeze105 HTTP_X_FORWARDED_SSL = 'HTTP_X_FORWARDED_SSL'.freeze106 def body; get_header(RACK_INPUT) end107 def script_name; get_header(SCRIPT_NAME).to_s end108 def script_name=(s); set_header(SCRIPT_NAME, s.to_s) end109 def path_info; get_header(PATH_INFO).to_s end110 def path_info=(s); set_header(PATH_INFO, s.to_s) end111 def request_method; get_header(REQUEST_METHOD) end112 def query_string; get_header(QUERY_STRING).to_s end113 def content_length; get_header('CONTENT_LENGTH') end114 def logger; get_header(RACK_LOGGER) end115 def user_agent; get_header('HTTP_USER_AGENT') end116 def multithread?; get_header(RACK_MULTITHREAD) end117 # the referer of the client118 def referer; get_header('HTTP_REFERER') end119 alias referrer referer120 def session121 fetch_header(RACK_SESSION) do |k|122 set_header RACK_SESSION, default_session123 end124 end125 def session_options126 fetch_header(RACK_SESSION_OPTIONS) do |k|127 set_header RACK_SESSION_OPTIONS, {}128 end129 end130 # Checks the HTTP request method (or verb) to see if it was of type DELETE131 def delete?; request_method == DELETE end132 # Checks the HTTP request method (or verb) to see if it was of type GET133 def get?; request_method == GET end134 # Checks the HTTP request method (or verb) to see if it was of type HEAD135 def head?; request_method == HEAD end136 # Checks the HTTP request method (or verb) to see if it was of type OPTIONS137 def options?; request_method == OPTIONS end138 # Checks the HTTP request method (or verb) to see if it was of type LINK139 def link?; request_method == LINK end140 # Checks the HTTP request method (or verb) to see if it was of type PATCH141 def patch?; request_method == PATCH end142 # Checks the HTTP request method (or verb) to see if it was of type POST143 def post?; request_method == POST end144 # Checks the HTTP request method (or verb) to see if it was of type PUT145 def put?; request_method == PUT end146 # Checks the HTTP request method (or verb) to see if it was of type TRACE147 def trace?; request_method == TRACE end148 # Checks the HTTP request method (or verb) to see if it was of type UNLINK149 def unlink?; request_method == UNLINK end150 def scheme151 if get_header(HTTPS) == 'on'152 'https'153 elsif get_header(HTTP_X_FORWARDED_SSL) == 'on'154 'https'155 elsif get_header(HTTP_X_FORWARDED_SCHEME)156 get_header(HTTP_X_FORWARDED_SCHEME)157 elsif get_header(HTTP_X_FORWARDED_PROTO)158 get_header(HTTP_X_FORWARDED_PROTO).split(',')[0]159 else160 get_header(RACK_URL_SCHEME)161 end162 end163 def authority164 get_header(SERVER_NAME) + ':' + get_header(SERVER_PORT)165 end166 def cookies167 hash = fetch_header(RACK_REQUEST_COOKIE_HASH) do |k|168 set_header(k, {})169 end170 string = get_header HTTP_COOKIE171 return hash if string == get_header(RACK_REQUEST_COOKIE_STRING)172 hash.replace Utils.parse_cookies_header get_header HTTP_COOKIE173 set_header(RACK_REQUEST_COOKIE_STRING, string)174 hash175 end176 def content_type177 content_type = get_header('CONTENT_TYPE')178 content_type.nil? || content_type.empty? ? nil : content_type179 end180 def xhr?181 get_header("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest"182 end183 def host_with_port184 if forwarded = get_header(HTTP_X_FORWARDED_HOST)185 forwarded.split(/,\s?/).last186 else187 get_header(HTTP_HOST) || "#{get_header(SERVER_NAME) || get_header(SERVER_ADDR)}:#{get_header(SERVER_PORT)}"188 end189 end190 def host191 # Remove port number.192 host_with_port.to_s.sub(/:\d+\z/, '')193 end194 def port195 if port = host_with_port.split(/:/)[1]196 port.to_i197 elsif port = get_header(HTTP_X_FORWARDED_PORT)198 port.to_i199 elsif has_header?(HTTP_X_FORWARDED_HOST)200 DEFAULT_PORTS[scheme]201 elsif has_header?(HTTP_X_FORWARDED_PROTO)202 DEFAULT_PORTS[get_header(HTTP_X_FORWARDED_PROTO).split(',')[0]]203 else204 get_header(SERVER_PORT).to_i205 end206 end207 def ssl?208 scheme == 'https'209 end210 def ip211 remote_addrs = split_ip_addresses(get_header('REMOTE_ADDR'))212 remote_addrs = reject_trusted_ip_addresses(remote_addrs)213 return remote_addrs.first if remote_addrs.any?214 forwarded_ips = split_ip_addresses(get_header('HTTP_X_FORWARDED_FOR'))215 return reject_trusted_ip_addresses(forwarded_ips).last || get_header("REMOTE_ADDR")216 end217 # The media type (type/subtype) portion of the CONTENT_TYPE header218 # without any media type parameters. e.g., when CONTENT_TYPE is219 # "text/plain;charset=utf-8", the media-type is "text/plain".220 #221 # For more information on the use of media types in HTTP, see:222 # def media_type224 MediaType.type(content_type)225 end226 # The media type parameters provided in CONTENT_TYPE as a Hash, or227 # an empty Hash if no CONTENT_TYPE or media-type parameters were228 # provided. e.g., when the CONTENT_TYPE is "text/plain;charset=utf-8",229 # this method responds with the following Hash:230 # { 'charset' => 'utf-8' }231 def media_type_params232 MediaType.params(content_type)233 end234 # The character set of the request body if a "charset" media type235 # parameter was given, or nil if no "charset" was specified. Note236 # that, per RFC2616, text/* media types that specify no explicit237 # charset are to be considered ISO-8859-1.238 def content_charset239 media_type_params['charset']240 end241 # Determine whether the request body contains form-data by checking242 # the request Content-Type for one of the media-types:243 # "application/x-www-form-urlencoded" or "multipart/form-data". The244 # list of form-data media types can be modified through the245 # +FORM_DATA_MEDIA_TYPES+ array.246 #247 # A request body is also assumed to contain form-data when no248 # Content-Type header is provided and the request_method is POST.249 def form_data?250 type = media_type251 meth = get_header(RACK_METHODOVERRIDE_ORIGINAL_METHOD) || get_header(REQUEST_METHOD)252 (meth == POST && type.nil?) || FORM_DATA_MEDIA_TYPES.include?(type)253 end254 # Determine whether the request body contains data by checking255 # the request media_type against registered parse-data media-types256 def parseable_data?257 PARSEABLE_DATA_MEDIA_TYPES.include?(media_type)258 end259 # Returns the data received in the query string.260 def GET261 if get_header(RACK_REQUEST_QUERY_STRING) == query_string262 get_header(RACK_REQUEST_QUERY_HASH)263 else264 query_hash = parse_query(query_string, '&;')265 set_header(RACK_REQUEST_QUERY_STRING, query_string)266 set_header(RACK_REQUEST_QUERY_HASH, query_hash)267 end268 end269 # Returns the data received in the request body.270 #271 # This method support both application/x-www-form-urlencoded and272 # multipart/form-data.273 def POST274 if get_header(RACK_INPUT).nil?275 raise "Missing rack.input"276 elsif get_header(RACK_REQUEST_FORM_INPUT) == get_header(RACK_INPUT)277 get_header(RACK_REQUEST_FORM_HASH)278 elsif form_data? || parseable_data?279 unless set_header(RACK_REQUEST_FORM_HASH, parse_multipart)280 form_vars = get_header(RACK_INPUT).read281 # Fix for Safari Ajax postings that always append \0282 # form_vars.sub!(/\0\z/, '') # performance replacement:283 form_vars.slice!(-1) if form_vars[-1] == ?\0284 set_header RACK_REQUEST_FORM_VARS, form_vars285 set_header RACK_REQUEST_FORM_HASH, parse_query(form_vars, '&')286 get_header(RACK_INPUT).rewind287 end288 set_header RACK_REQUEST_FORM_INPUT, get_header(RACK_INPUT)289 get_header RACK_REQUEST_FORM_HASH290 else291 {}292 end293 end294 # The union of GET and POST data.295 #296 # Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params.297 def params298 self.GET.merge(self.POST)299 rescue EOFError300 self.GET.dup301 end302 # Destructively update a parameter, whether it's in GET and/or POST. Returns nil.303 #304 # The parameter is updated wherever it was previous defined, so GET, POST, or both. If it wasn't previously defined, it's inserted into GET.305 #306 # <tt>env['rack.input']</tt> is not touched.307 def update_param(k, v)308 found = false309 if self.GET.has_key?(k)310 found = true311 self.GET[k] = v312 end313 if self.POST.has_key?(k)314 found = true315 self.POST[k] = v316 end317 unless found318 self.GET[k] = v319 end320 end321 # Destructively delete a parameter, whether it's in GET or POST. Returns the value of the deleted parameter.322 #323 # If the parameter is in both GET and POST, the POST value takes precedence since that's how #params works.324 #325 # <tt>env['rack.input']</tt> is not touched.326 def delete_param(k)327 [ self.POST.delete(k), self.GET.delete(k) ].compact.first328 end329 def base_url330 url = "#{scheme}://#{host}"331 url << ":#{port}" if port != DEFAULT_PORTS[scheme]332 url333 end334 # Tries to return a remake of the original request URL as a string.335 def url336 base_url + fullpath337 end338 def path339 script_name + path_info340 end341 def fullpath342 query_string.empty? ? path : "#{path}?#{query_string}"343 end344 def accept_encoding345 parse_http_accept_header(get_header("HTTP_ACCEPT_ENCODING"))346 end347 def accept_language348 parse_http_accept_header(get_header("HTTP_ACCEPT_LANGUAGE"))349 end350 def trusted_proxy?(ip)351 ip =~ /\A127\.0\.0\.1\Z|\A(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.|\A::1\Z|\Afd[0-9a-f]{2}:.+|\Alocalhost\Z|\Aunix\Z|\Aunix:/i352 end353 # shortcut for <tt>request.params[key]</tt>354 def [](key)355 if $VERBOSE356 warn("Request#[] is deprecated and will be removed in a future version of Rack. Please use request.params[] instead")357 end358 params[key.to_s]359 end360 # shortcut for <tt>request.params[key] = value</tt>361 #362 # Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params....

Full Screen

Full Screen


Using AI Code Generation


Full Screen

1uri = URI.parse('')2response = Net::HTTP.get_response(uri)3puts'content-type')4 def initialize(response)5 def get_header(header)

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?