Module: NiceHttpManageResponse
- Included in:
- NiceHttp
- Defined in:
- lib/nice_http/manage_response.rb
Instance Method Summary collapse
-
#manage_response(resp, data) ⇒ Object
private method to manage Response input: resp data output: @response updated.
Instance Method Details
#manage_response(resp, data) ⇒ Object
private method to manage Response input: resp data output: @response updated
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/nice_http/manage_response.rb', line 11 def manage_response(resp, data) @finish_time = Time.now require "json" @prev_response = Hash.new() if @prev_response.nil? begin if @start_time.kind_of?(Time) @response[:time_elapsed_total] = @finish_time - @start_time else @response[:time_elapsed_total] = nil end if @start_time_net.kind_of?(Time) @response[:time_request] = @start_time_net @response[:time_response] = @finish_time @response[:time_elapsed] = @finish_time - @start_time_net @start_time_net = nil else @response[:time_elapsed] = nil end create_stats(resp) if @create_stats begin # this is to be able to access all keys as symbols on Ruby < 2.6.0 if RUBY_VERSION < '2.6.0' new_resp = Hash.new() resp.each { |key, value| if key.kind_of?(String) new_resp[key.to_sym] = value end } new_resp.each { |key, value| resp[key] = value } end rescue end #for mock_responses to be able to add outside of the header like content-type for example if resp.kind_of?(Hash) and !resp.has_key?(:header) resp[:header] = {} end #todo: check this. not sure if this is valid anymore since resp it will be a hash only when mock_response if resp.kind_of?(Hash) resp.each { |k, v| if k != :code and k != :message and k != :data and k != :'set-cookie' and k != :header resp[:header][k] = v end } resp[:header].each { |k, v| resp.delete(k) if resp.has_key?(k) } end method_s = caller[0].to_s().scan(/:in `(.*)'/).join if resp.header.kind_of?(Hash) and (resp.header["content-type"].to_s() == "application/x-deflate" or resp.header[:"content-type"].to_s() == "application/x-deflate") data = Zlib::Inflate.inflate(data) end encoding_response = "" if resp.header.kind_of?(Hash) and (resp.header["content-type"].to_s() != "" or resp.header[:"content-type"].to_s() != "") encoding_response = resp.header["content-type"].scan(/;charset=(.*)/i).join if resp.header.has_key?("content-type") encoding_response = resp.header[:"content-type"].scan(/;charset=(.*)/i).join if resp.header.has_key?(:"content-type") end if encoding_response.to_s() == "" encoding_response = "UTF-8" end if encoding_response.to_s() != "" and encoding_response.to_s().upcase != "UTF-8" data.encode!("UTF-8", encoding_response.to_s()) end if encoding_response != "" and encoding_response.to_s().upcase != "UTF-8" @response[:message] = resp..to_s().encode("UTF-8", encoding_response.to_s()) #todo: response data in here for example is convert into string, verify if that is correct or needs to maintain the original data type (hash, array...) resp.each { |key, val| @response[key.to_sym] = val.to_s().encode("UTF-8", encoding_response.to_s()) } else @response[:message] = resp. resp.each { |key, val| @response[key.to_sym] = val } end if !defined?(Net::HTTP::Post::Multipart) or (defined?(Net::HTTP::Post::Multipart) and !data.kind_of?(Net::HTTP::Post::Multipart)) @response[:data] = data else @response[:data] = "" end @response[:code] = resp.code = "\nRESPONSE: \n " + @response[:code].to_s() + ":" + @response[:message].to_s() if @debug or @prev_response[:'content-type'] != @response[:'content-type'] or @prev_response[:'content-length'] != @response[:'content-length'] or @prev_response[:data] != @response[:data] or @prev_response[:code] != @response[:code] or @prev_response[:message] != @response[:message] self.class.last_response = @response.each { |key, value| if value.to_s() != "" value_orig = value if key.kind_of?(Symbol) if key == :code or key == :data or key == :header or key == :message if key == :data and !@response[:'content-type'].to_s.include?("text/html") if key == :data and (!@response[:'content-type'].include?('text') and !@response[:'content-type'].include?('json') and !@response[:'content-type'].include?('xml') and !@response[:'content-type'].include?('charset=utf-8')) += "\n data: It's not text data so won't be in the logs." else begin JSON.parse(value_orig) data_s = JSON.pretty_generate(JSON.parse(value_orig)) rescue data_s = value_orig end self.class.last_response += "\n " + key.to_s() + ": '" + data_s.gsub("<", "<") + "'\n" if value_orig != value += "\n " + key.to_s() + ": '" + value.gsub("<", "<") + "'\n" else += "\n " + key.to_s() + ": '" + data_s.gsub("<", "<") + "'\n" end end else self.class.last_response += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'" if @debug += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'" end end else self.class.last_response += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'" += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'" end elsif !@response.include?(key.to_sym) self.class.last_response += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'" += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'" end end } self.class.captured << "#{self.class.last_request}\n#{self.class.last_response}" if self.class.capture else += "\n Same as the last response." self.class.captured << "#{self.class.last_request}\n#{}" if self.class.capture end @logger.info if @response.kind_of?(Hash) if @response.keys.include?(:requestid) @headers["requestId"] = @response[:requestid] self.class.request_id = @response[:requestid] @logger.info "requestId was found on the response header and it has been added to the headers for the next request" end end if resp[:'set-cookie'].to_s() != "" if resp.kind_of?(Hash) #mock_response = resp[:'set-cookie'].to_s().split(", ") else #Net::Http = resp.get_fields("set-cookie") end .each { || = .split("; ")[0].split("=") = .scan(/; path=([^;]+)/i).join @cookies[] = Hash.new() unless @cookies.keys.include?() @cookies[][[0]] = [1] } @logger.info "set-cookie added to Cookie header as required" if @headers.has_key?("X-CSRFToken") csrftoken = resp[:"set-cookie"].to_s().scan(/csrftoken=([\da-z]+);/).join if csrftoken.to_s() != "" @headers["X-CSRFToken"] = csrftoken @logger.info "X-CSRFToken exists on headers and has been overwritten" end else csrftoken = resp[:"set-cookie"].to_s().scan(/csrftoken=([\da-z]+);/).join if csrftoken.to_s() != "" @headers["X-CSRFToken"] = csrftoken @logger.info "X-CSRFToken added to header as required" end end end @prev_response = @response rescue Exception => stack @logger.fatal stack @logger.fatal "manage_response Error on method #{method_s} " end @start_time = nil end |