rb-net_http-client 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,58 +3,71 @@
3
3
  require 'json'
4
4
  require 'nokogiri'
5
5
  require 'ostruct'
6
- require_relative 'schema'
7
6
  require_relative '../core/core'
8
7
  require_relative '../core/utilities'
9
8
 
10
9
  module NetHTTP
11
10
  class Response
12
- attr_reader :logger,
11
+ attr_reader :code,
12
+ :body,
13
+ :body_obj,
14
+ :body_os,
15
+ :headers,
16
+ :headers_hash,
17
+ :headers_os,
18
+ :logger,
13
19
  :response
14
20
 
15
21
  def initialize(opts = {})
16
22
  send('logger=', opts[:logger])
17
- puts opts[:response].class
18
- Core.schema_validation(opts, NetHTTP::Response::Schema, logger) unless opts[:enforce_schema_validation] == false
19
23
  @response = opts[:response]
24
+ send('response=', opts[:response])
25
+ send('code=')
26
+ send('headers=')
27
+ send('headers_hash=')
28
+ send('headers_os=')
29
+ send('body=')
30
+ send('body_obj=')
31
+ send('body_os=')
20
32
  log_response
21
33
  end
22
34
 
23
35
  alias resp response
24
36
 
25
- def body
26
- response.body
37
+ def body=
38
+ @body = response.body
27
39
  end
28
40
 
29
41
  alias resp_body body
30
42
  alias response_body body
31
43
 
32
- def body_hash
44
+ def body_obj=
45
+ @body_obj = {}
33
46
  begin
34
- return NetHTTP::Core::Utilities.json_2_hash(body, 'symbol', logger) if valid_json?
35
- rescue JSON::ParserError => err
36
- logger.debug(err)
47
+ @body_obj = NetHTTP::Core::Utilities.json_2_hash(body, 'symbol', logger) if valid_json?
48
+ rescue JSON::ParserError => error
49
+ logger.debug(error)
37
50
  end
38
51
  begin
39
- return NetHTTP::Core::Utilities.xml_2_hash(body, 'symbol', logger) if valid_xml?
40
- rescue Nokogiri::XML::SyntaxError => err
41
- logger.debug(err)
52
+ @body_obj = NetHTTP::Core::Utilities.xml_2_hash(body, 'symbol', logger) if valid_xml?
53
+ rescue Nokogiri::XML::SyntaxError => error
54
+ logger.debug(error)
42
55
  end
43
56
  begin
44
57
  # TODO: revisit this if necessary
45
- return NetHTTP::Core::Utilities.xml_2_hash(body, 'symbol', logger) if valid_html?
46
- rescue Nokogiri::XML::SyntaxError => err
47
- logger.debug(err)
58
+ @body_obj = NetHTTP::Core::Utilities.xml_2_hash(body, 'symbol', logger) if valid_html?
59
+ rescue Nokogiri::XML::SyntaxError => error
60
+ logger.debug(error)
48
61
  end
49
-
50
- {}
51
62
  end
52
63
 
53
- alias resp_body_hash body_hash
54
- alias response_body_hash body_hash
64
+ # TODO: remove this eventually
65
+ alias body_hash body_obj
66
+ alias resp_body_obj body_obj
67
+ alias response_body_obj body_obj
55
68
 
56
- def body_os
57
- JSON.parse(body_hash.to_json, object_class: OpenStruct)
69
+ def body_os=
70
+ @body_os = JSON.parse(body_obj.to_json, object_class: OpenStruct)
58
71
  end
59
72
 
60
73
  alias resp_body_os body_os
@@ -63,24 +76,25 @@ module NetHTTP
63
76
  alias resp_body_open_struct body_os
64
77
  alias response_body_open_struct body_os
65
78
 
66
- def code
67
- response.code
79
+ def code=
80
+ @code = response.code
68
81
  end
69
82
 
70
83
  alias resp_code code
71
84
  alias response_code code
72
85
 
73
- def headers
74
- response_headers = {}
86
+ def headers=
87
+ @headers = {}
75
88
  response.to_hash.each do |key, value|
76
- response_headers[key] = value.flatten[0].to_s
89
+ @headers[key] = value.flatten[0].to_s
77
90
  end
78
91
  end
79
92
 
80
93
  alias resp_headers headers
81
94
  alias response_headers headers
82
95
 
83
- def headers_hash
96
+ def headers_hash=
97
+ @headers_hash = {}
84
98
  NetHTTP::Core::Utilities.convert_hash_keys(
85
99
  object: headers,
86
100
  format: 'snake',
@@ -91,8 +105,8 @@ module NetHTTP
91
105
  alias resp_headers_hash headers_hash
92
106
  alias response_headers_hash headers_hash
93
107
 
94
- def headers_os
95
- JSON.parse(headers_hash.to_json, object_class: OpenStruct)
108
+ def headers_os=
109
+ @headers_os = JSON.parse(headers_hash.to_json, object_class: OpenStruct)
96
110
  end
97
111
 
98
112
  alias resp_headers_os headers_os
@@ -101,6 +115,10 @@ module NetHTTP
101
115
  alias resp_headers_struct headers_os
102
116
  alias response_headers_open_struct headers_os
103
117
 
118
+ def response=(response)
119
+ @response = response
120
+ end
121
+
104
122
  def valid_json?
105
123
  NetHTTP::Core::Utilities.valid_json?(body, logger)
106
124
  end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NetHTTP
4
- VERSION = '1.0.1'
4
+ VERSION = '1.0.2'
5
5
  end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe 'NetHTTP.client' do
6
+ it 'returns a valid NetHTTP client instance' do
7
+ ENV['http_proxy'] = nil
8
+ ENV['https_proxy'] = nil
9
+ ENV['HTTP_PROXY'] = nil
10
+ ENV['HTTPS_PROXY'] = nil
11
+
12
+ net_http_client = NetHTTP.client(
13
+ uri: 'https://jsonplaceholder.typicode.com/posts'
14
+ )
15
+
16
+ expect(net_http_client.class).to eq(NetHTTP::Client)
17
+ expect(net_http_client.logger.class).to eq(Logger)
18
+ expect(net_http_client.logger.level).to eq(Logger::INFO)
19
+ expect(net_http_client.uri.to_s).to eq('https://jsonplaceholder.typicode.com/posts')
20
+ expect(net_http_client.proxy_uri.to_s).to eq('')
21
+ expect(net_http_client.use_ssl).to eq(true)
22
+ expect(net_http_client.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
23
+ expect(net_http_client.open_timeout).to eq(60)
24
+ expect(net_http_client.read_timeout).to eq(60)
25
+ end
26
+
27
+ it 'returns a valid NetHTTP client instance' do
28
+ ENV['http_proxy'] = nil
29
+ ENV['https_proxy'] = nil
30
+ ENV['HTTP_PROXY'] = nil
31
+ ENV['HTTPS_PROXY'] = nil
32
+
33
+ net_http_client = NetHTTP::Client.new(
34
+ uri: 'https://jsonplaceholder.typicode.com/posts'
35
+ )
36
+
37
+ expect(net_http_client.class).to eq(NetHTTP::Client)
38
+ expect(net_http_client.logger.class).to eq(Logger)
39
+ expect(net_http_client.logger.level).to eq(Logger::INFO)
40
+ expect(net_http_client.uri.to_s).to eq('https://jsonplaceholder.typicode.com/posts')
41
+ expect(net_http_client.proxy_uri.to_s).to eq('')
42
+ expect(net_http_client.use_ssl).to eq(true)
43
+ expect(net_http_client.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
44
+ expect(net_http_client.open_timeout).to eq(60)
45
+ expect(net_http_client.read_timeout).to eq(60)
46
+ end
47
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe 'NetHTTP::Client ext' do
6
+ it 'returns a valid NetHTTP client instance with proxy_uri -> URI' do
7
+ net_http_client = NetHTTP.client(
8
+ uri: 'https://jsonplaceholder.typicode.com/posts',
9
+ proxy_uri: 'http://proxy.com:8888'
10
+ )
11
+
12
+ expect(net_http_client.class).to eq(NetHTTP::Client)
13
+ expect(net_http_client.proxy_uri.to_s).to eq('http://proxy.com:8888')
14
+ end
15
+
16
+ it 'returns a valid NetHTTP client instance with proxy_uri -> ""' do
17
+ expect {
18
+ NetHTTP.client(
19
+ uri: 'https://jsonplaceholder.typicode.com/posts',
20
+ proxy_uri: ''
21
+ )
22
+ }.to raise_error(RuntimeError, /NetHTTP::Core::SchemaValidationError/)
23
+ end
24
+
25
+ it 'returns a valid NetHTTP client instance with proxy_uri -> nil' do
26
+ expect {
27
+ NetHTTP.client(
28
+ uri: 'https://jsonplaceholder.typicode.com/posts',
29
+ proxy_uri: nil
30
+ )
31
+ }.to raise_error(RuntimeError, /NetHTTP::Core::SchemaValidationError/)
32
+ end
33
+
34
+ it 'returns a valid NetHTTP client instance with proxy_uri -> INVALID' do
35
+ expect {
36
+ NetHTTP.client(
37
+ uri: 'https://jsonplaceholder.typicode.com/posts',
38
+ proxy_uri: 12345
39
+ )
40
+ }.to raise_error(RuntimeError, /NetHTTP::Core::SchemaValidationError/)
41
+ end
42
+
43
+ it 'returns a valid NetHTTP client instance' do
44
+ ENV['http_proxy'] = nil
45
+ ENV['https_proxy'] = nil
46
+ ENV['HTTP_PROXY'] = nil
47
+ ENV['HTTPS_PROXY'] = nil
48
+
49
+ net_http_client = NetHTTP.client(
50
+ scheme: 'https',
51
+ host: 'jsonplaceholder.typicode.com',
52
+ port: 443,
53
+ path: '/posts'
54
+ )
55
+
56
+ expect(net_http_client.class).to eq(NetHTTP::Client)
57
+ expect(net_http_client.logger.class).to eq(Logger)
58
+ expect(net_http_client.logger.level).to eq(Logger::INFO)
59
+ expect(net_http_client.uri.to_s).to eq('https://jsonplaceholder.typicode.com/posts')
60
+ expect(net_http_client.proxy_uri.to_s).to eq('')
61
+ expect(net_http_client.use_ssl).to eq(true)
62
+ expect(net_http_client.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
63
+ expect(net_http_client.open_timeout).to eq(60)
64
+ expect(net_http_client.read_timeout).to eq(60)
65
+ end
66
+
67
+ it 'returns a valid NetHTTP client instance' do
68
+ ENV['http_proxy'] = nil
69
+ ENV['https_proxy'] = nil
70
+ ENV['HTTP_PROXY'] = nil
71
+ ENV['HTTPS_PROXY'] = nil
72
+
73
+ net_http_client = NetHTTP.client(
74
+ scheme: 'https',
75
+ host: 'jsonplaceholder.typicode.com',
76
+ port: 443,
77
+ path: '/posts'
78
+ )
79
+
80
+ expect(net_http_client.class).to eq(NetHTTP::Client)
81
+ expect(net_http_client.logger.class).to eq(Logger)
82
+ expect(net_http_client.logger.level).to eq(Logger::INFO)
83
+ expect(net_http_client.uri.to_s).to eq('https://jsonplaceholder.typicode.com/posts')
84
+ expect(net_http_client.proxy_uri.to_s).to eq('')
85
+ expect(net_http_client.use_ssl).to eq(true)
86
+ expect(net_http_client.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
87
+ expect(net_http_client.open_timeout).to eq(60)
88
+ expect(net_http_client.read_timeout).to eq(60)
89
+ end
90
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe 'NetHTTP::Core' do
6
+ it 'successful schema_validation' do
7
+ logger = NetHTTP::Core.get_logger
8
+
9
+ results = NetHTTP::Core.schema_validation(
10
+ {
11
+ uri: 'https://www.google.com'
12
+ },
13
+ NetHTTP::Client::Schema,
14
+ logger
15
+ )
16
+
17
+ expect(results).to eq(nil)
18
+ end
19
+
20
+ it 'failed schema_validation' do
21
+ logger = NetHTTP::Core.get_logger
22
+
23
+ expect {
24
+ NetHTTP::Core.schema_validation(
25
+ {
26
+ uri: nil
27
+ },
28
+ NetHTTP::Client::Schema,
29
+ logger
30
+ )
31
+ }.to raise_error(RuntimeError)
32
+ end
33
+
34
+ it 'failed schema_validation -> invalid schema' do
35
+ logger = NetHTTP::Core.get_logger
36
+
37
+ expect {
38
+ NetHTTP::Core.schema_validation(
39
+ {
40
+ uri: 'https://www.google.com'
41
+ },
42
+ 'Invalid::Schema',
43
+ logger
44
+ )
45
+ }.to raise_error(NoMethodError, "undefined method `call' for \"Invalid::Schema\":String")
46
+ end
47
+ end
@@ -0,0 +1,230 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe 'NetHTTP::Core::Utilities' do
6
+ it 'constructs valid URI' do
7
+ uri = {
8
+ scheme: 'https',
9
+ host: 'www.google.com',
10
+ port: 443,
11
+ path: '/search',
12
+ query: 'q=ruby+uri'
13
+ }
14
+
15
+ uri = NetHTTP::Core::Utilities.construct_uri(uri)
16
+ expect(uri).to eq('https://www.google.com/search?q=ruby+uri')
17
+ expect(uri.class).to eq(String)
18
+
19
+ parsed_uri = URI(uri)
20
+ expect(parsed_uri.scheme).to eq('https')
21
+ expect(parsed_uri.host).to eq('www.google.com')
22
+ expect(parsed_uri.port).to eq(443)
23
+ expect(parsed_uri.path).to eq('/search')
24
+ expect(parsed_uri.query).to eq('q=ruby+uri')
25
+ end
26
+
27
+ it 'constructs valid URI when uri[:scheme] is missing' do
28
+ uri = {
29
+ host: 'www.google.com',
30
+ port: 443,
31
+ path: '/search',
32
+ query: 'q=ruby+uri'
33
+ }
34
+
35
+ uri = NetHTTP::Core::Utilities.construct_uri(uri)
36
+ expect(uri).to eq('https://www.google.com/search?q=ruby+uri')
37
+ expect(uri.class).to eq(String)
38
+
39
+ parsed_uri = URI(uri)
40
+ expect(parsed_uri.scheme).to eq('https')
41
+ expect(parsed_uri.host).to eq('www.google.com')
42
+ expect(parsed_uri.port).to eq(443)
43
+ expect(parsed_uri.path).to eq('/search')
44
+ expect(parsed_uri.query).to eq('q=ruby+uri')
45
+ end
46
+
47
+ it 'constructs valid URI when uri[:port] is missing' do
48
+ uri = {
49
+ scheme: 'https',
50
+ host: 'www.google.com',
51
+ path: '/search',
52
+ query: 'q=ruby+uri'
53
+ }
54
+
55
+ uri = NetHTTP::Core::Utilities.construct_uri(uri)
56
+ expect(uri).to eq('https://www.google.com/search?q=ruby+uri')
57
+ expect(uri.class).to eq(String)
58
+
59
+ parsed_uri = URI(uri)
60
+ expect(parsed_uri.scheme).to eq('https')
61
+ expect(parsed_uri.host).to eq('www.google.com')
62
+ expect(parsed_uri.port).to eq(443)
63
+ expect(parsed_uri.path).to eq('/search')
64
+ expect(parsed_uri.query).to eq('q=ruby+uri')
65
+ end
66
+
67
+ it 'constructs valid URI when uri[:scheme] is missing' do
68
+ uri = {
69
+ host: 'www.google.com',
70
+ port: 80,
71
+ path: '/search',
72
+ query: 'q=ruby+uri'
73
+ }
74
+
75
+ uri = NetHTTP::Core::Utilities.construct_uri(uri)
76
+ expect(uri).to eq('http://www.google.com/search?q=ruby+uri')
77
+ expect(uri.class).to eq(String)
78
+
79
+ parsed_uri = URI(uri)
80
+ expect(parsed_uri.scheme).to eq('http')
81
+ expect(parsed_uri.host).to eq('www.google.com')
82
+ expect(parsed_uri.port).to eq(80)
83
+ expect(parsed_uri.path).to eq('/search')
84
+ expect(parsed_uri.query).to eq('q=ruby+uri')
85
+ end
86
+
87
+ it 'constructs valid URI when uri[:port] is missing' do
88
+ uri = {
89
+ scheme: 'http',
90
+ host: 'www.google.com',
91
+ path: '/search',
92
+ query: 'q=ruby+uri'
93
+ }
94
+
95
+ uri = NetHTTP::Core::Utilities.construct_uri(uri)
96
+ expect(uri).to eq('http://www.google.com/search?q=ruby+uri')
97
+ expect(uri.class).to eq(String)
98
+
99
+ parsed_uri = URI(uri)
100
+ expect(parsed_uri.scheme).to eq('http')
101
+ expect(parsed_uri.host).to eq('www.google.com')
102
+ expect(parsed_uri.port).to eq(80)
103
+ expect(parsed_uri.path).to eq('/search')
104
+ expect(parsed_uri.query).to eq('q=ruby+uri')
105
+ end
106
+
107
+ it 'constructs valid URI when uri[:port], uri[:path], uri[:query] are missing' do
108
+ uri = {
109
+ scheme: 'https',
110
+ host: 'www.google.com'
111
+ }
112
+
113
+ uri = NetHTTP::Core::Utilities.construct_uri(uri)
114
+ expect(uri).to eq('https://www.google.com')
115
+ expect(uri.class).to eq(String)
116
+
117
+ parsed_uri = URI(uri)
118
+ expect(parsed_uri.scheme).to eq('https')
119
+ expect(parsed_uri.host).to eq('www.google.com')
120
+ expect(parsed_uri.port).to eq(443)
121
+ expect(parsed_uri.path).to eq('')
122
+ expect(parsed_uri.query).to eq(nil)
123
+ end
124
+
125
+ it 'construct uri returns nil if uri is empty' do
126
+ uri = {
127
+ }
128
+
129
+ uri = NetHTTP::Core::Utilities.construct_uri(uri)
130
+ expect(uri).to eq(nil)
131
+ end
132
+
133
+ it 'construct uri returns nil if uri[:host] is missing' do
134
+ uri = {
135
+ host: nil
136
+ }
137
+
138
+ uri = NetHTTP::Core::Utilities.construct_uri(uri)
139
+ expect(uri).to eq(nil)
140
+ end
141
+
142
+ it 'construct uri returns nil if uri[:scheme] && uri[:port] are missing' do
143
+ uri = {
144
+ host: 'www.google.com',
145
+ path: '/search'
146
+ }
147
+
148
+ uri = NetHTTP::Core::Utilities.construct_uri(uri)
149
+ expect(uri).to eq(nil)
150
+ end
151
+
152
+ it 'parses valid uri when no scheme is provided, only port => 443' do
153
+ uri = 'www.google.com:443'
154
+
155
+ parsed_uri = NetHTTP::Core::Utilities.parse_uri(uri)
156
+ expect(parsed_uri.scheme).to eq('https')
157
+ expect(parsed_uri.host).to eq('www.google.com')
158
+ expect(parsed_uri.port).to eq(443)
159
+ expect(parsed_uri.path).to eq('')
160
+ expect(parsed_uri.query).to eq(nil)
161
+ end
162
+
163
+ it 'parses valid uri when no scheme is provided, only port => 80' do
164
+ uri = 'www.google.com:80'
165
+
166
+ parsed_uri = NetHTTP::Core::Utilities.parse_uri(uri)
167
+ expect(parsed_uri.scheme).to eq('http')
168
+ expect(parsed_uri.host).to eq('www.google.com')
169
+ expect(parsed_uri.port).to eq(80)
170
+ expect(parsed_uri.path).to eq('')
171
+ expect(parsed_uri.query).to eq(nil)
172
+ end
173
+
174
+ it 'checks if valid xml document is valid XML' do
175
+ xml_doc = '''
176
+ <note>
177
+ <to>Tove</to>
178
+ <from>Jani</from>
179
+ <heading>Reminder</heading>
180
+ <body>Do not forget me this weekend!</body>
181
+ </note>
182
+ '''
183
+
184
+ valid_xml = NetHTTP::Core::Utilities.valid_xml?(xml_doc)
185
+ expect(valid_xml).to eq(true)
186
+ end
187
+
188
+ it 'checks if valid html document is valid HTML' do
189
+ html_doc = '''
190
+ <!DOCTYPE html>
191
+ <html>
192
+ <body>
193
+ <h1>My First Heading</h1>
194
+ <p>My first paragraph.</p>
195
+ </body>
196
+ </html>
197
+ '''
198
+
199
+ valid_html = NetHTTP::Core::Utilities.valid_html?(html_doc)
200
+ expect(valid_html).to eq(true)
201
+ end
202
+
203
+ it 'checks if invalid xml document is valid XML' do
204
+ xml_doc = '''
205
+ <note>
206
+ <to>Tove</to>
207
+ <from>Jani</from>
208
+ <heading>Reminder</heading>
209
+ <body>Do not forget me this weekend!</body>
210
+ '''
211
+
212
+ valid_xml = NetHTTP::Core::Utilities.valid_xml?(xml_doc)
213
+ expect(valid_xml).to eq(false)
214
+ end
215
+
216
+ it 'checks if invalid html document is valid HTML' do
217
+ html_doc = '''
218
+ <!DOCTYPE html>
219
+ <html>
220
+ <body>
221
+ <h1>My First Heading
222
+ <p>My first paragraph.
223
+ </body>
224
+ </html>
225
+ '''
226
+
227
+ valid_html = NetHTTP::Core::Utilities.valid_html?(html_doc)
228
+ expect(valid_html).to eq(false)
229
+ end
230
+ end