nice_http 1.5.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,181 +1,181 @@
1
- module NiceHttpManageResponse
2
-
3
- ######################################################
4
- # private method to manage Response
5
- # input:
6
- # resp
7
- # data
8
- # output:
9
- # @response updated
10
- ######################################################
11
- def manage_response(resp, data)
12
- require "json"
13
- begin
14
- if @start_time.kind_of?(Time)
15
- @response[:time_elapsed_total] = Time.now - @start_time
16
- @start_time = nil
17
- else
18
- @response[:time_elapsed_total] = nil
19
- end
20
- if @start_time_net.kind_of?(Time)
21
- @response[:time_elapsed] = Time.now - @start_time_net
22
- @start_time_net = nil
23
- else
24
- @response[:time_elapsed] = nil
25
- end
26
- begin
27
- # this is to be able to access all keys as symbols
28
- new_resp = Hash.new()
29
- resp.each { |key, value|
30
- if key.kind_of?(String)
31
- new_resp[key.to_sym] = value
32
- end
33
- }
34
- new_resp.each { |key, value|
35
- resp[key] = value
36
- }
37
- rescue
38
- end
39
- #for mock_responses to be able to add outside of the header like content-type for example
40
- if resp.kind_of?(Hash) and !resp.has_key?(:header)
41
- resp[:header] = {}
42
- end
43
-
44
- #todo: check this. not sure if this is valid anymore since resp it will be a hash only when mock_response
45
- if resp.kind_of?(Hash)
46
- resp.each { |k, v|
47
- if k != :code and k != :message and k != :data and k != :'set-cookie' and k != :header
48
- resp[:header][k] = v
49
- end
50
- }
51
- resp[:header].each { |k, v|
52
- resp.delete(k) if resp.has_key?(k)
53
- }
54
- end
55
-
56
- method_s = caller[0].to_s().scan(/:in `(.*)'/).join
57
- 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")
58
- data = Zlib::Inflate.inflate(data)
59
- end
60
- encoding_response = ""
61
- if resp.header.kind_of?(Hash) and (resp.header["content-type"].to_s() != "" or resp.header[:"content-type"].to_s() != "")
62
- encoding_response = resp.header["content-type"].scan(/;charset=(.*)/i).join if resp.header.has_key?("content-type")
63
- encoding_response = resp.header[:"content-type"].scan(/;charset=(.*)/i).join if resp.header.has_key?(:"content-type")
64
- end
65
- if encoding_response.to_s() == ""
66
- encoding_response = "UTF-8"
67
- end
68
-
69
- if encoding_response.to_s() != "" and encoding_response.to_s().upcase != "UTF-8"
70
- data.encode!("UTF-8", encoding_response.to_s())
71
- end
72
-
73
- if encoding_response != "" and encoding_response.to_s().upcase != "UTF-8"
74
- @response[:message] = resp.message.to_s().encode("UTF-8", encoding_response.to_s())
75
- #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...)
76
- resp.each { |key, val| @response[key.to_sym] = val.to_s().encode("UTF-8", encoding_response.to_s()) }
77
- else
78
- @response[:message] = resp.message
79
- resp.each { |key, val|
80
- @response[key.to_sym] = val
81
- }
82
- end
83
-
84
- if !defined?(Net::HTTP::Post::Multipart) or (defined?(Net::HTTP::Post::Multipart) and !data.kind_of?(Net::HTTP::Post::Multipart))
85
- @response[:data] = data
86
- else
87
- @response[:data] = ""
88
- end
89
-
90
- @response[:code] = resp.code
91
-
92
- unless @response.nil?
93
- message = "\nRESPONSE: \n" + @response[:code].to_s() + ":" + @response[:message].to_s()
94
- #if @debug
95
- self.class.last_response = message if @debug
96
- @response.each { |key, value|
97
- if value.to_s() != ""
98
- value_orig = value
99
- if key.kind_of?(Symbol)
100
- if key == :code or key == :data or key == :header or key == :message
101
- if key == :data and !@response[:'content-type'].to_s.include?('text/html')
102
- begin
103
- JSON.parse(value_orig)
104
- data_s = JSON.pretty_generate(JSON.parse(value_orig))
105
- rescue
106
- data_s = value_orig
107
- end
108
- if @debug
109
- self.class.last_response += "\nresponse." + key.to_s() + " = '" + data_s.gsub("<", "&lt;") + "'\n"
110
- end
111
- if value_orig != value
112
- message += "\nresponse." + key.to_s() + " = '" + value.gsub("<", "&lt;") + "'\n"
113
- else
114
- message += "\nresponse." + key.to_s() + " = '" + data_s.gsub("<", "&lt;") + "'\n"
115
- end
116
- else
117
- if @debug
118
- self.class.last_response += "\nresponse." + key.to_s() + " = '" + value.to_s().gsub("<", "&lt;") + "'"
119
- message += "\nresponse." + key.to_s() + " = '" + value.to_s().gsub("<", "&lt;") + "'"
120
- end
121
- end
122
- else
123
- if @debug
124
- self.class.last_response += "\nresponse[:" + key.to_s() + "] = '" + value.to_s().gsub("<", "&lt;") + "'"
125
- end
126
- message += "\nresponse[:" + key.to_s() + "] = '" + value.to_s().gsub("<", "&lt;") + "'"
127
- end
128
- elsif !@response.include?(key.to_sym)
129
- if @debug
130
- self.class.last_response += "\nresponse['" + key.to_s() + "'] = '" + value.to_s().gsub("<", "&lt;") + "'"
131
- end
132
- message += "\nresponse['" + key.to_s() + "'] = '" + value.to_s().gsub("<", "&lt;") + "'"
133
- end
134
- end
135
- }
136
- #end
137
- @logger.info message
138
- if @response.kind_of?(Hash)
139
- if @response.keys.include?(:requestid)
140
- @headers["requestId"] = @response[:requestid]
141
- self.class.request_id = @response[:requestid]
142
- @logger.info "requestId was found on the response header and it has been added to the headers for the next request"
143
- end
144
- end
145
- end
146
-
147
- if resp[:'set-cookie'].to_s() != ""
148
- if resp.kind_of?(Hash) #mock_response
149
- cookies_to_set = resp[:'set-cookie'].to_s().split(", ")
150
- else #Net::Http
151
- cookies_to_set = resp.get_fields("set-cookie")
152
- end
153
- cookies_to_set.each { |cookie|
154
- cookie_pair = cookie.split("; ")[0].split("=")
155
- cookie_path = cookie.scan(/; path=([^;]+)/i).join
156
- @cookies[cookie_path] = Hash.new() unless @cookies.keys.include?(cookie_path)
157
- @cookies[cookie_path][cookie_pair[0]] = cookie_pair[1]
158
- }
159
-
160
- @logger.info "set-cookie added to Cookie header as required"
161
-
162
- if @headers.has_key?("X-CSRFToken")
163
- csrftoken = resp[:"set-cookie"].to_s().scan(/csrftoken=([\da-z]+);/).join
164
- if csrftoken.to_s() != ""
165
- @headers["X-CSRFToken"] = csrftoken
166
- @logger.info "X-CSRFToken exists on headers and has been overwritten"
167
- end
168
- else
169
- csrftoken = resp[:"set-cookie"].to_s().scan(/csrftoken=([\da-z]+);/).join
170
- if csrftoken.to_s() != ""
171
- @headers["X-CSRFToken"] = csrftoken
172
- @logger.info "X-CSRFToken added to header as required"
173
- end
174
- end
175
- end
176
- rescue Exception => stack
177
- @logger.fatal stack
178
- @logger.fatal "manage_response Error on method #{method_s} "
179
- end
180
- end
1
+ module NiceHttpManageResponse
2
+
3
+ ######################################################
4
+ # private method to manage Response
5
+ # input:
6
+ # resp
7
+ # data
8
+ # output:
9
+ # @response updated
10
+ ######################################################
11
+ def manage_response(resp, data)
12
+ require "json"
13
+ begin
14
+ if @start_time.kind_of?(Time)
15
+ @response[:time_elapsed_total] = Time.now - @start_time
16
+ @start_time = nil
17
+ else
18
+ @response[:time_elapsed_total] = nil
19
+ end
20
+ if @start_time_net.kind_of?(Time)
21
+ @response[:time_elapsed] = Time.now - @start_time_net
22
+ @start_time_net = nil
23
+ else
24
+ @response[:time_elapsed] = nil
25
+ end
26
+ begin
27
+ # this is to be able to access all keys as symbols
28
+ new_resp = Hash.new()
29
+ resp.each { |key, value|
30
+ if key.kind_of?(String)
31
+ new_resp[key.to_sym] = value
32
+ end
33
+ }
34
+ new_resp.each { |key, value|
35
+ resp[key] = value
36
+ }
37
+ rescue
38
+ end
39
+ #for mock_responses to be able to add outside of the header like content-type for example
40
+ if resp.kind_of?(Hash) and !resp.has_key?(:header)
41
+ resp[:header] = {}
42
+ end
43
+
44
+ #todo: check this. not sure if this is valid anymore since resp it will be a hash only when mock_response
45
+ if resp.kind_of?(Hash)
46
+ resp.each { |k, v|
47
+ if k != :code and k != :message and k != :data and k != :'set-cookie' and k != :header
48
+ resp[:header][k] = v
49
+ end
50
+ }
51
+ resp[:header].each { |k, v|
52
+ resp.delete(k) if resp.has_key?(k)
53
+ }
54
+ end
55
+
56
+ method_s = caller[0].to_s().scan(/:in `(.*)'/).join
57
+ 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")
58
+ data = Zlib::Inflate.inflate(data)
59
+ end
60
+ encoding_response = ""
61
+ if resp.header.kind_of?(Hash) and (resp.header["content-type"].to_s() != "" or resp.header[:"content-type"].to_s() != "")
62
+ encoding_response = resp.header["content-type"].scan(/;charset=(.*)/i).join if resp.header.has_key?("content-type")
63
+ encoding_response = resp.header[:"content-type"].scan(/;charset=(.*)/i).join if resp.header.has_key?(:"content-type")
64
+ end
65
+ if encoding_response.to_s() == ""
66
+ encoding_response = "UTF-8"
67
+ end
68
+
69
+ if encoding_response.to_s() != "" and encoding_response.to_s().upcase != "UTF-8"
70
+ data.encode!("UTF-8", encoding_response.to_s())
71
+ end
72
+
73
+ if encoding_response != "" and encoding_response.to_s().upcase != "UTF-8"
74
+ @response[:message] = resp.message.to_s().encode("UTF-8", encoding_response.to_s())
75
+ #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...)
76
+ resp.each { |key, val| @response[key.to_sym] = val.to_s().encode("UTF-8", encoding_response.to_s()) }
77
+ else
78
+ @response[:message] = resp.message
79
+ resp.each { |key, val|
80
+ @response[key.to_sym] = val
81
+ }
82
+ end
83
+
84
+ if !defined?(Net::HTTP::Post::Multipart) or (defined?(Net::HTTP::Post::Multipart) and !data.kind_of?(Net::HTTP::Post::Multipart))
85
+ @response[:data] = data
86
+ else
87
+ @response[:data] = ""
88
+ end
89
+
90
+ @response[:code] = resp.code
91
+
92
+ unless @response.nil?
93
+ message = "\nRESPONSE: \n" + @response[:code].to_s() + ":" + @response[:message].to_s()
94
+ #if @debug
95
+ self.class.last_response = message if @debug
96
+ @response.each { |key, value|
97
+ if value.to_s() != ""
98
+ value_orig = value
99
+ if key.kind_of?(Symbol)
100
+ if key == :code or key == :data or key == :header or key == :message
101
+ if key == :data and !@response[:'content-type'].to_s.include?('text/html')
102
+ begin
103
+ JSON.parse(value_orig)
104
+ data_s = JSON.pretty_generate(JSON.parse(value_orig))
105
+ rescue
106
+ data_s = value_orig
107
+ end
108
+ if @debug
109
+ self.class.last_response += "\nresponse." + key.to_s() + " = '" + data_s.gsub("<", "&lt;") + "'\n"
110
+ end
111
+ if value_orig != value
112
+ message += "\nresponse." + key.to_s() + " = '" + value.gsub("<", "&lt;") + "'\n"
113
+ else
114
+ message += "\nresponse." + key.to_s() + " = '" + data_s.gsub("<", "&lt;") + "'\n"
115
+ end
116
+ else
117
+ if @debug
118
+ self.class.last_response += "\nresponse." + key.to_s() + " = '" + value.to_s().gsub("<", "&lt;") + "'"
119
+ message += "\nresponse." + key.to_s() + " = '" + value.to_s().gsub("<", "&lt;") + "'"
120
+ end
121
+ end
122
+ else
123
+ if @debug
124
+ self.class.last_response += "\nresponse[:" + key.to_s() + "] = '" + value.to_s().gsub("<", "&lt;") + "'"
125
+ end
126
+ message += "\nresponse[:" + key.to_s() + "] = '" + value.to_s().gsub("<", "&lt;") + "'"
127
+ end
128
+ elsif !@response.include?(key.to_sym)
129
+ if @debug
130
+ self.class.last_response += "\nresponse['" + key.to_s() + "'] = '" + value.to_s().gsub("<", "&lt;") + "'"
131
+ end
132
+ message += "\nresponse['" + key.to_s() + "'] = '" + value.to_s().gsub("<", "&lt;") + "'"
133
+ end
134
+ end
135
+ }
136
+ #end
137
+ @logger.info message
138
+ if @response.kind_of?(Hash)
139
+ if @response.keys.include?(:requestid)
140
+ @headers["requestId"] = @response[:requestid]
141
+ self.class.request_id = @response[:requestid]
142
+ @logger.info "requestId was found on the response header and it has been added to the headers for the next request"
143
+ end
144
+ end
145
+ end
146
+
147
+ if resp[:'set-cookie'].to_s() != ""
148
+ if resp.kind_of?(Hash) #mock_response
149
+ cookies_to_set = resp[:'set-cookie'].to_s().split(", ")
150
+ else #Net::Http
151
+ cookies_to_set = resp.get_fields("set-cookie")
152
+ end
153
+ cookies_to_set.each { |cookie|
154
+ cookie_pair = cookie.split("; ")[0].split("=")
155
+ cookie_path = cookie.scan(/; path=([^;]+)/i).join
156
+ @cookies[cookie_path] = Hash.new() unless @cookies.keys.include?(cookie_path)
157
+ @cookies[cookie_path][cookie_pair[0]] = cookie_pair[1]
158
+ }
159
+
160
+ @logger.info "set-cookie added to Cookie header as required"
161
+
162
+ if @headers.has_key?("X-CSRFToken")
163
+ csrftoken = resp[:"set-cookie"].to_s().scan(/csrftoken=([\da-z]+);/).join
164
+ if csrftoken.to_s() != ""
165
+ @headers["X-CSRFToken"] = csrftoken
166
+ @logger.info "X-CSRFToken exists on headers and has been overwritten"
167
+ end
168
+ else
169
+ csrftoken = resp[:"set-cookie"].to_s().scan(/csrftoken=([\da-z]+);/).join
170
+ if csrftoken.to_s() != ""
171
+ @headers["X-CSRFToken"] = csrftoken
172
+ @logger.info "X-CSRFToken added to header as required"
173
+ end
174
+ end
175
+ end
176
+ rescue Exception => stack
177
+ @logger.fatal stack
178
+ @logger.fatal "manage_response Error on method #{method_s} "
179
+ end
180
+ end
181
181
  end
@@ -1,109 +1,109 @@
1
- module NiceHttpUtils
2
- ##################################################
3
- # get a value of xml tag
4
- # input:
5
- # tag_name
6
- # xml_string
7
- # take_off_prefix: boolean (optional). true, false(default)
8
- # output:
9
- # the value or an array of all values found with this tag_name
10
- ####################################################
11
- def self.get_value_xml_tag(tag_name, xml_string, take_off_prefix = false)
12
- return nil if xml_string.nil?
13
- xml_string2 = xml_string.dup()
14
- if take_off_prefix
15
- i = tag_name.index(":")
16
- if !i.nil?
17
- tag_name = tag_name[i + 1..tag_name.length]
18
- end
19
- end
20
-
21
- ret = Array.new()
22
- if xml_string2.to_s() != ""
23
- if take_off_prefix
24
- xml_string2.gsub!(/<[a-zA-Z0-9]+:#{tag_name} [^>]*>/i, "<" + tag_name + ">")
25
- xml_string2.gsub!(/<\/[a-zA-Z0-9]+:#{tag_name}>/i, "</" + tag_name + ">")
26
- xml_string2.gsub!(/<[a-zA-Z0-9]+:#{tag_name}>/i, "<" + tag_name + ">")
27
- end
28
-
29
- xml_string2.gsub!(/<#{tag_name} [^>]*>/i, "<" + tag_name + ">")
30
-
31
- tag1 = "<" + tag_name + ">"
32
- tag2 = "</" + tag_name + ">"
33
-
34
- x = xml_string2.index(tag1)
35
- if !x.nil?
36
- x += tag1.size
37
- begin
38
- y = xml_string2.index(tag2)
39
- if y.nil?
40
- ret.push("")
41
- x = nil
42
- else
43
- y -= 1
44
- value = xml_string2[x..y]
45
- ret.push(value)
46
- xml_string2 = xml_string2[y + tag2.size..xml_string2.length]
47
- x = xml_string2.index(tag1)
48
- if !x.nil?
49
- x += tag1.size
50
- end
51
- end
52
- end while !x.nil?
53
- else
54
- ret.push("")
55
- end
56
- else
57
- ret.push("")
58
- end
59
- if ret.size == 1
60
- return ret[0].to_s()
61
- else
62
- return ret
63
- end
64
- end
65
-
66
- ##################################################
67
- # set a value on xml tag
68
- # input:
69
- # tag_name
70
- # xml_string
71
- # value
72
- # take_off_prefix: boolean (optional). true, false(default)
73
- # output:
74
- # xml_string with the new value
75
- ####################################################
76
- def self.set_value_xml_tag(tag_name, xml_string, value, take_off_prefix = false)
77
- tag_name = tag_name.to_s
78
- if take_off_prefix
79
- i = tag_name.index(":")
80
- tag_name = tag_name[i + 1..tag_name.length] unless i.nil?
81
- end
82
- if xml_string.to_s != ""
83
- if take_off_prefix
84
- old_value = NiceHttpUtils.get_value_xml_tag(tag_name, xml_string.dup, true)
85
- xml_string.gsub!(/:#{tag_name}>#{Regexp.escape(old_value)}<\//i, ":" + tag_name + ">" + value + "</")
86
- xml_string.gsub!(/<#{tag_name}>#{Regexp.escape(old_value)}<\//i, "<" + tag_name + ">" + value + "</")
87
- else
88
- xml_string.gsub!(/<#{tag_name}>.*<\/#{tag_name}>/i, "<" + tag_name + ">" + value + "</" + tag_name + ">")
89
- end
90
- return xml_string
91
- else
92
- return ""
93
- end
94
- end
95
-
96
- ##################################################
97
- # returns the seed for Basic authentication
98
- # input:
99
- # user
100
- # password
101
- # output:
102
- # seed string to be used on Authorization key header on a get request
103
- ####################################################
104
- def self.basic_authentication(user:, password:)
105
- require "base64"
106
- seed = "Basic " + Base64.encode64(user + ":" + password)
107
- return seed
108
- end
109
- end
1
+ module NiceHttpUtils
2
+ ##################################################
3
+ # get a value of xml tag
4
+ # input:
5
+ # tag_name
6
+ # xml_string
7
+ # take_off_prefix: boolean (optional). true, false(default)
8
+ # output:
9
+ # the value or an array of all values found with this tag_name
10
+ ####################################################
11
+ def self.get_value_xml_tag(tag_name, xml_string, take_off_prefix = false)
12
+ return nil if xml_string.nil?
13
+ xml_string2 = xml_string.dup()
14
+ if take_off_prefix
15
+ i = tag_name.index(":")
16
+ if !i.nil?
17
+ tag_name = tag_name[i + 1..tag_name.length]
18
+ end
19
+ end
20
+
21
+ ret = Array.new()
22
+ if xml_string2.to_s() != ""
23
+ if take_off_prefix
24
+ xml_string2.gsub!(/<[a-zA-Z0-9]+:#{tag_name} [^>]*>/i, "<" + tag_name + ">")
25
+ xml_string2.gsub!(/<\/[a-zA-Z0-9]+:#{tag_name}>/i, "</" + tag_name + ">")
26
+ xml_string2.gsub!(/<[a-zA-Z0-9]+:#{tag_name}>/i, "<" + tag_name + ">")
27
+ end
28
+
29
+ xml_string2.gsub!(/<#{tag_name} [^>]*>/i, "<" + tag_name + ">")
30
+
31
+ tag1 = "<" + tag_name + ">"
32
+ tag2 = "</" + tag_name + ">"
33
+
34
+ x = xml_string2.index(tag1)
35
+ if !x.nil?
36
+ x += tag1.size
37
+ begin
38
+ y = xml_string2.index(tag2)
39
+ if y.nil?
40
+ ret.push("")
41
+ x = nil
42
+ else
43
+ y -= 1
44
+ value = xml_string2[x..y]
45
+ ret.push(value)
46
+ xml_string2 = xml_string2[y + tag2.size..xml_string2.length]
47
+ x = xml_string2.index(tag1)
48
+ if !x.nil?
49
+ x += tag1.size
50
+ end
51
+ end
52
+ end while !x.nil?
53
+ else
54
+ ret.push("")
55
+ end
56
+ else
57
+ ret.push("")
58
+ end
59
+ if ret.size == 1
60
+ return ret[0].to_s()
61
+ else
62
+ return ret
63
+ end
64
+ end
65
+
66
+ ##################################################
67
+ # set a value on xml tag
68
+ # input:
69
+ # tag_name
70
+ # xml_string
71
+ # value
72
+ # take_off_prefix: boolean (optional). true, false(default)
73
+ # output:
74
+ # xml_string with the new value
75
+ ####################################################
76
+ def self.set_value_xml_tag(tag_name, xml_string, value, take_off_prefix = false)
77
+ tag_name = tag_name.to_s
78
+ if take_off_prefix
79
+ i = tag_name.index(":")
80
+ tag_name = tag_name[i + 1..tag_name.length] unless i.nil?
81
+ end
82
+ if xml_string.to_s != ""
83
+ if take_off_prefix
84
+ old_value = NiceHttpUtils.get_value_xml_tag(tag_name, xml_string.dup, true)
85
+ xml_string.gsub!(/:#{tag_name}>#{Regexp.escape(old_value)}<\//i, ":" + tag_name + ">" + value + "</")
86
+ xml_string.gsub!(/<#{tag_name}>#{Regexp.escape(old_value)}<\//i, "<" + tag_name + ">" + value + "</")
87
+ else
88
+ xml_string.gsub!(/<#{tag_name}>.*<\/#{tag_name}>/i, "<" + tag_name + ">" + value + "</" + tag_name + ">")
89
+ end
90
+ return xml_string
91
+ else
92
+ return ""
93
+ end
94
+ end
95
+
96
+ ##################################################
97
+ # returns the seed for Basic authentication
98
+ # input:
99
+ # user
100
+ # password
101
+ # output:
102
+ # seed string to be used on Authorization key header on a get request
103
+ ####################################################
104
+ def self.basic_authentication(user:, password:)
105
+ require "base64"
106
+ seed = "Basic " + Base64.encode64(user + ":" + password)
107
+ return seed
108
+ end
109
+ end