Module: TShield::Controllers::Requests::Helpers
- Included in:
- Server
- Defined in:
- lib/tshield/controllers/requests.rb
Overview
Requests Handler Helpers
Class Method Summary collapse
Instance Method Summary collapse
- #add_headers(options, path) ⇒ Object
- #configuration ⇒ Object
- #delay(path) ⇒ Object
- #domain(path) ⇒ Object
- #treat(params, request, _response) ⇒ Object
- #treat_headers_by_domain(options, path) ⇒ Object
Class Method Details
.build_headers(request) ⇒ Object
32 33 34 35 36 |
# File 'lib/tshield/controllers/requests.rb', line 32 def self.build_headers(request) headers = request.env.select { |key, _value| key =~ /HTTP/ } headers['Content-Type'] = request.content_type || 'application/json' headers end |
Instance Method Details
#add_headers(options, path) ⇒ Object
99 100 101 102 103 |
# File 'lib/tshield/controllers/requests.rb', line 99 def add_headers(, path) (configuration.get_headers(domain(path)) || {}).each do |source, destiny| [:headers][destiny] = request.env[source] if request.env[source] end end |
#configuration ⇒ Object
110 111 112 |
# File 'lib/tshield/controllers/requests.rb', line 110 def configuration @configuration ||= TShield::Configuration.singleton end |
#delay(path) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/tshield/controllers/requests.rb', line 118 def delay(path) begin delay_in_seconds = configuration.get_delay(domain(path), path) || 0 logger.info("Response with delay of #{delay_in_seconds} seconds") sleep delay_in_seconds rescue ConfigurationNotFoundError logger.debug('No delay configured') end end |
#domain(path) ⇒ Object
114 115 116 |
# File 'lib/tshield/controllers/requests.rb', line 114 def domain(path) @domain ||= configuration.get_domain_for(path) end |
#treat(params, request, _response) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/tshield/controllers/requests.rb', line 38 def treat(params, request, _response) path = params.fetch('captures', [])[0] callid = "#{path}?#{request.env['QUERY_STRING']}" method = request.request_method request_content_type = request.content_type session_name = TShield::Controllers::Helpers::SessionHelpers.current_session_name(request) secondary_sessions = TShield::Controllers::Helpers::SessionHelpers.secondary_sessions(request) session_call = TShield::Controllers::Helpers::SessionHelpers .current_session_call(request, callid, method) = { method: method, headers: Helpers.build_headers(request), raw_query: request.env['QUERY_STRING'], session: session_name, secondary_sessions: secondary_sessions, call: session_call, ip: request.ip } if %w[POST PUT PATCH].include? method result = request.body.read.encode('UTF-8', invalid: :replace, undef: :replace, replace: '') [:body] = result end api_response = TShield::RequestMatching.new(path, .clone).match_request unless api_response begin treat_headers_by_domain(, path) add_headers(, path) api_response ||= TShield::RequestVCR.new(path, .clone).vcr_response api_response.headers.reject! do |key, _v| configuration.get_excluded_headers(domain(path)).include?(key) end rescue ConfigurationNotFoundError => e logger.error("Error on recover configuration for #{path}") status 500 body({tshield: e }.to_json) return end end logger.info( "original=#{api_response.original} method=#{method} path=#{path} "\ "content-type=#{request_content_type} "\ "session=#{session_name} call=#{session_call}" ) TShield::Controllers::Helpers::SessionHelpers.update_session_call(request, callid, method) delay(path) status api_response.status headers api_response.headers body api_response.body end |
#treat_headers_by_domain(options, path) ⇒ Object
105 106 107 108 |
# File 'lib/tshield/controllers/requests.rb', line 105 def treat_headers_by_domain(, path) @send_header_content_type = configuration.send_header_content_type(domain(path)) [:headers].delete('Content-Type') unless @send_header_content_type end |