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.
@@ -0,0 +1,275 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'webmock/rspec'
4
+ require_relative '../../spec_helper'
5
+
6
+ describe 'NetHTTP::Client web service calls' do
7
+ it 'makes a request with an invalid HTTP method' do
8
+ net_http_client = NetHTTP.client(
9
+ uri: 'https://jsonplaceholder.typicode.com/posts'
10
+ )
11
+
12
+ expect {
13
+ net_http_client.call_web_service(
14
+ method: 'DOES_NOT_EXIST'
15
+ )
16
+ }.to raise_error(RuntimeError, "Request method => 'DOES_NOT_EXIST' not yet supported.")
17
+ end
18
+
19
+ it 'makes a request with valid HTTP method => delete' do
20
+ stub_request(
21
+ :delete,
22
+ 'https://jsonplaceholder.typicode.com/posts'
23
+ ).with(
24
+ headers: request_headers,
25
+ body: nil
26
+ ).to_return(
27
+ status: 200,
28
+ headers: successful_post_response_headers,
29
+ body: successful_post_response_body
30
+ )
31
+
32
+ net_http_client = NetHTTP.client(
33
+ uri: 'https://jsonplaceholder.typicode.com/'
34
+ )
35
+
36
+ response = net_http_client.call_web_service(
37
+ method: 'delete',
38
+ path: '/posts'
39
+ )
40
+
41
+ expect(response.code.to_s).to eq('200')
42
+ expect(response.headers).to eq(successful_post_response_headers)
43
+ expect(response.headers_hash).not_to eq(nil)
44
+ expect(response.headers_hash.class).to eq(Hash)
45
+ expect(response.body).to eq(successful_post_response_body)
46
+ expect(response.body_obj).not_to eq(nil)
47
+ expect(response.body_obj.class).to eq(Hash)
48
+ end
49
+
50
+ it 'makes a request with valid HTTP method => get' do
51
+ stub_request(
52
+ :get,
53
+ 'https://jsonplaceholder.typicode.com/posts'
54
+ ).with(
55
+ headers: request_headers,
56
+ body: nil
57
+ ).to_return(
58
+ status: 200,
59
+ headers: successful_post_response_headers,
60
+ body: successful_post_response_body
61
+ )
62
+
63
+ net_http_client = NetHTTP.client(
64
+ uri: 'https://jsonplaceholder.typicode.com/'
65
+ )
66
+
67
+ response = net_http_client.call_web_service(
68
+ method: 'get',
69
+ path: '/posts'
70
+ )
71
+
72
+ expect(response.code.to_s).to eq('200')
73
+ expect(response.headers).to eq(successful_post_response_headers)
74
+ expect(response.headers_hash).not_to eq(nil)
75
+ expect(response.headers_hash.class).to eq(Hash)
76
+ expect(response.body).to eq(successful_post_response_body)
77
+ expect(response.body_obj).not_to eq(nil)
78
+ expect(response.body_obj.class).to eq(Hash)
79
+ end
80
+
81
+ it 'makes a request with valid HTTP method => post' do
82
+ stub_request(
83
+ :post,
84
+ 'https://jsonplaceholder.typicode.com/posts'
85
+ ).with(
86
+ headers: request_headers,
87
+ body: nil
88
+ ).to_return(
89
+ status: 200,
90
+ headers: successful_post_response_headers,
91
+ body: successful_post_response_body
92
+ )
93
+
94
+ net_http_client = NetHTTP.client(
95
+ uri: 'https://jsonplaceholder.typicode.com/'
96
+ )
97
+
98
+ response = net_http_client.call_web_service(
99
+ method: 'post',
100
+ path: '/posts'
101
+ )
102
+
103
+ expect(response.code.to_s).to eq('200')
104
+ expect(response.headers).to eq(successful_post_response_headers)
105
+ expect(response.headers_hash).not_to eq(nil)
106
+ expect(response.headers_hash.class).to eq(Hash)
107
+ expect(response.body).to eq(successful_post_response_body)
108
+ expect(response.body_obj).not_to eq(nil)
109
+ expect(response.body_obj.class).to eq(Hash)
110
+ end
111
+
112
+ # it 'makes a request with valid HTTP method => post_form' do
113
+ # stub_request(
114
+ # :post,
115
+ # 'https://jsonplaceholder.typicode.com/posts'
116
+ # ).with(
117
+ # headers: request_headers,
118
+ # body: nil
119
+ # ).to_return(
120
+ # status: 200,
121
+ # headers: successful_post_response_headers,
122
+ # body: successful_post_response_body
123
+ # )
124
+ #
125
+ # net_http_client = NetHTTP.client(
126
+ # uri: 'https://jsonplaceholder.typicode.com/'
127
+ # )
128
+ # response = net_http_client.call_web_service(
129
+ # method: 'post_form',
130
+ # path: '/posts'
131
+ # )
132
+ #
133
+ # expect(response.code.to_s).to eq('200')
134
+ # expect(response.headers).to eq(successful_post_response_headers)
135
+ # expect(response.headers_hash).not_to eq(nil)
136
+ # expect(response.headers_hash.class).to eq(Hash)
137
+ # expect(response.body).to eq(successful_post_response_body)
138
+ # expect(response.body_obj).not_to eq(nil)
139
+ # expect(response.body_obj.class).to eq(Hash)
140
+ # end
141
+
142
+ it 'makes a request with valid HTTP method => put' do
143
+ stub_request(
144
+ :put,
145
+ 'https://jsonplaceholder.typicode.com/posts'
146
+ ).with(
147
+ headers: request_headers,
148
+ body: nil
149
+ ).to_return(
150
+ status: 200,
151
+ headers: successful_post_response_headers,
152
+ body: successful_post_response_body
153
+ )
154
+
155
+ net_http_client = NetHTTP.client(
156
+ uri: 'https://jsonplaceholder.typicode.com/'
157
+ )
158
+
159
+ response = net_http_client.call_web_service(
160
+ method: 'put',
161
+ path: '/posts'
162
+ )
163
+
164
+ expect(response.code.to_s).to eq('200')
165
+ expect(response.headers).to eq(successful_post_response_headers)
166
+ expect(response.headers_hash).not_to eq(nil)
167
+ expect(response.headers_hash.class).to eq(Hash)
168
+ expect(response.body).to eq(successful_post_response_body)
169
+ expect(response.body_obj).not_to eq(nil)
170
+ expect(response.body_obj.class).to eq(Hash)
171
+ end
172
+
173
+ it 'successfully make a POST request' do
174
+ stub_request(
175
+ :post,
176
+ 'https://jsonplaceholder.typicode.com/posts'
177
+ ).with(
178
+ headers: request_headers,
179
+ body: nil
180
+ ).to_return(
181
+ status: 200,
182
+ headers: successful_post_response_headers,
183
+ body: successful_post_response_body
184
+ )
185
+
186
+ net_http_client = NetHTTP.client(
187
+ uri: 'https://jsonplaceholder.typicode.com/'
188
+ )
189
+ response = net_http_client.post(
190
+ path: '/posts'
191
+ )
192
+
193
+ expect(response.code.to_s).to eq('200')
194
+ expect(response.headers).to eq(successful_post_response_headers)
195
+ expect(response.headers_hash).not_to eq(nil)
196
+ expect(response.headers_hash.class).to eq(Hash)
197
+ expect(response.body).to eq(successful_post_response_body)
198
+ expect(response.body_obj).not_to eq(nil)
199
+ expect(response.body_obj.class).to eq(Hash)
200
+ end
201
+
202
+ it 'makes a request with valid HTTP method => get with ? in request path' do
203
+ stub_request(
204
+ :get,
205
+ "https://autotrader.com/cars-for-sale/searchresults.xhtml?searchRadius=0"
206
+ ).with(
207
+ headers: {
208
+ 'Accept' => '*/*',
209
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
210
+ 'User-Agent' => 'Ruby'
211
+ }
212
+ ).to_return(
213
+ status: 200,
214
+ body: '',
215
+ headers: {}
216
+ )
217
+
218
+ net_http_client = NetHTTP.client(
219
+ uri: 'https://autotrader.com'
220
+ )
221
+
222
+ response = net_http_client.get(
223
+ path: '/cars-for-sale/searchresults.xhtml',
224
+ query: '?searchRadius=0'
225
+ )
226
+
227
+ expect(response.code.to_s).to eq('200')
228
+ expect(response.headers).to eq({})
229
+ expect(response.headers_hash).not_to eq(nil)
230
+ expect(response.headers_hash.class).to eq(Hash)
231
+ expect(response.body).to eq('')
232
+ expect(response.body_obj).not_to eq(nil)
233
+ expect(response.body_obj.class).to eq(Hash)
234
+ end
235
+
236
+ private
237
+
238
+ def request_headers
239
+ {
240
+ 'Accept' => '*/*',
241
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
242
+ 'User-Agent' => 'Ruby'
243
+ }
244
+ end
245
+
246
+ def successful_post_response_headers
247
+ {
248
+ 'date' => 'Tue, 05 Mar 2019 22:44:58 GMT',
249
+ 'content-type' => 'application/json; charset=utf-8',
250
+ 'content-length' => '15',
251
+ 'connection' => 'close',
252
+ 'set-cookie' => '__cfduid=d9e01b81c42675802849336961ecd18371551825898; expires=Wed, 04-Mar-20 22:44:58 GMT; path=/; domain=.typicode.com; HttpOnly',
253
+ 'x-powered-by' => 'Express',
254
+ 'vary' => 'Origin, X-HTTP-Method-Override, Accept-Encoding',
255
+ 'access-control-allow-credentials' => 'true',
256
+ 'cache-control' => 'no-cache',
257
+ 'pragma' => 'no-cache',
258
+ 'expires' => '-1',
259
+ 'access-control-expose-headers' => 'Location',
260
+ 'location' => 'http://jsonplaceholder.typicode.com/posts/101',
261
+ 'x-content-type-options' => 'nosniff',
262
+ 'etag' => "W/'f-4jjw4Y8q22Yv1PV9m28FczJgjzk'",
263
+ 'via' => '1.1 vegur',
264
+ 'expect-ct' => "max-age=604800, report-uri='https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct'",
265
+ 'server' => 'cloudflare',
266
+ 'cf-ray' => '4b2f9e1b0c9dcd00-EWR'
267
+ }
268
+ end
269
+
270
+ def successful_post_response_body
271
+ '{
272
+ "id": 101
273
+ }'
274
+ end
275
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../spec_helper'
4
+
5
+ describe 'NetHTTP::Response' do
6
+ it 'rescues body_obj= JSON::ParserError' do
7
+ # response = NetHTTP::Response.new(
8
+ # response:
9
+ # )
10
+ end
11
+
12
+ it 'rescues body_obj= XML Nokogiri::XML::SyntaxError' do
13
+
14
+ end
15
+
16
+ it 'rescues body_obj= HTML Nokogiri::XML::SyntaxError' do
17
+
18
+ end
19
+
20
+ private
21
+
22
+ def invalid_json_body
23
+ '''
24
+
25
+ '''
26
+ end
27
+
28
+ def invalid_xml_body
29
+ '''
30
+
31
+ '''
32
+ end
33
+
34
+ def invalid_html_body
35
+ '''
36
+
37
+ '''
38
+ end
39
+ end
@@ -1,4 +1,4 @@
1
- require_relative '../../spec_helper'
1
+ require_relative '../spec_helper'
2
2
 
3
3
  describe NetHTTP::VERSION do
4
4
  it 'should have a valid version' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb-net_http-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Bostian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-28 00:00:00.000000000 Z
11
+ date: 2019-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-validation
@@ -209,22 +209,14 @@ files:
209
209
  - lib/request/request.rb
210
210
  - lib/request/schema.rb
211
211
  - lib/response/response.rb
212
- - lib/response/schema.rb
213
212
  - lib/version.rb
214
- - spec/integration/net_http/client/client_ext_spec.rb
215
- - spec/integration/net_http/client/client_spec.rb
216
- - spec/integration/net_http/net_http_core_spec.rb
217
- - spec/integration/net_http/request/request_ext_spec.rb
218
- - spec/integration/net_http/response/response_ext_spec.rb
219
- - spec/integration/net_http/version_spec.rb
220
- - spec/unit/net_http/client/client_ext_spec.rb
221
- - spec/unit/net_http/client/client_schema_spec.rb
222
- - spec/unit/net_http/client/client_spec.rb
223
- - spec/unit/net_http/net_http_core_spec.rb
224
- - spec/unit/net_http/request/request_ext_spec.rb
225
- - spec/unit/net_http/request/request_schema_spec.rb
226
- - spec/unit/net_http/response/response_ext_spec.rb
227
- - spec/unit/net_http/version_spec.rb
213
+ - spec/unit/client/client_spec.rb
214
+ - spec/unit/client/ext_spec.rb
215
+ - spec/unit/core/core_spec.rb
216
+ - spec/unit/core/utilities_spec.rb
217
+ - spec/unit/request/request_spec.rb
218
+ - spec/unit/response/response_spec.rb
219
+ - spec/unit/version_spec.rb
228
220
  homepage: https://github.com/rabos5/rb-net_http-client
229
221
  licenses:
230
222
  - MIT
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'dry-validation'
4
-
5
- module NetHTTP
6
- class Response
7
- Schema = Dry::Validation.Schema do
8
- required(:response).filled(type?: Net::HTTPResponse)
9
- end
10
- end
11
- end
@@ -1,7 +0,0 @@
1
- require_relative '../../../spec_helper'
2
-
3
- describe NetHTTP::Client::Ext do
4
- # it '' do
5
- #
6
- # end
7
- end
@@ -1,7 +0,0 @@
1
- require_relative '../../../spec_helper'
2
-
3
- describe 'NetHTTP.client' do
4
- # it '' do
5
- #
6
- # end
7
- end
@@ -1,18 +0,0 @@
1
- require_relative '../../spec_helper'
2
-
3
- describe NetHTTP::Core do
4
- it 'returns schema validation errors when invalid options are used to instantiate a NetHTTP client' do
5
- client_opts = {}
6
-
7
- expect { NetHTTP.client(client_opts) } .to raise_error('NetHTTP::Core::Error - schema input validation failed.')
8
- end
9
-
10
- it 'returns schema validation errors when invalid options are used to instantiate a NetHTTP client with logger' do
11
- client_opts = {}
12
- logger = Logger.new(STDOUT)
13
- logger.level = Logger::INFO
14
- client_opts[:logger] = logger
15
-
16
- expect { NetHTTP.client(client_opts) } .to raise_error('NetHTTP::Core::Error - schema input validation failed.')
17
- end
18
- end