nice_http 1.8.5 → 1.8.6
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.
- checksums.yaml +4 -4
- data/README.md +72 -2
- data/lib/nice_http.rb +7 -3
- data/lib/nice_http/manage_request.rb +47 -17
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d71a8b887f41eb061251023b5490469bd1a19cb6f1c567ecb73a76dc0771a62
|
4
|
+
data.tar.gz: d8d11267e373ca3ffa54366be21116949c1d4bccad9ee58ded6b445708a3aa40
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d1f38f6e9f08c8a90094e5f15b337ca89c3f3894fa9c3ce313ed1f2360386f870393a2ac86d4f99e8c9c14f1f13bdbe191926f49fe8b7c9a7f91fce203bef24
|
7
|
+
data.tar.gz: c751a5dfc67127dbf5e89be704d46381888128b8f96b15a8819006d5ee4ec46cfdaec4df208fdb66eb3e8ca7db61b9020132ef0108a1dce4f03c6c6c361572c6
|
data/README.md
CHANGED
@@ -29,6 +29,11 @@ Example that creates 1000 good random and unique requests to register an user an
|
|
29
29
|
- [Responses](#Responses)
|
30
30
|
- [Special settings](#Special-settings)
|
31
31
|
- [Authentication requests](#Authentication-requests)
|
32
|
+
- [Basic Authentication](#Basic-Authentication)
|
33
|
+
- [OpenID](#OpenID)
|
34
|
+
- [OAuth2](#OAuth2)
|
35
|
+
- [JWT Token](#JWT-Token)
|
36
|
+
- [lambda on headers](#lambda-on-headers)
|
32
37
|
- [Http logs](#Http-logs)
|
33
38
|
- [Multithreading](#Multithreading)
|
34
39
|
- [Http stats](#Http-stats)
|
@@ -241,6 +246,19 @@ If the request hash contains a key :method with one of these possible values: :g
|
|
241
246
|
resp = @http.send_request req
|
242
247
|
```
|
243
248
|
|
249
|
+
You can always access to the last request as a Hash object by: `NiceHttp.request`
|
250
|
+
|
251
|
+
If you want to change the value of all headers using the value of the request on runtime, use `lambda`, `NiceHttp.requests` and `NiceHttp.request`:
|
252
|
+
|
253
|
+
```ruby
|
254
|
+
NiceHttp.requests = {
|
255
|
+
headers: {
|
256
|
+
Referer: lambda { "http://myserver.com" + NiceHttp.request.path }
|
257
|
+
}
|
258
|
+
}
|
259
|
+
```
|
260
|
+
|
261
|
+
You can use `NiceHttp.requests` to specify certain `headers` or `data` that will apply on all requests sent.
|
244
262
|
|
245
263
|
## Responses
|
246
264
|
|
@@ -275,6 +293,8 @@ Also interesting keys would be: *time_elapsed_total*, *time_elapsed* and many mo
|
|
275
293
|
|
276
294
|
All we need to do is to add to our request the correct authentication tokens, seeds, headers.
|
277
295
|
|
296
|
+
### Basic Authentication
|
297
|
+
|
278
298
|
For example for Basic Authentication we need to add to the authorization header a seed generated with the user and password we want ot authenticate
|
279
299
|
|
280
300
|
```ruby
|
@@ -305,6 +325,8 @@ Remember for other kind of authentication systems NiceHttp take care of the redi
|
|
305
325
|
|
306
326
|
In case you want or need to control the redirections by yourself instead of allowing NiceHttp to do it, then set ```@http.auto_redirect = false```
|
307
327
|
|
328
|
+
### OpenID
|
329
|
+
|
308
330
|
An example using OpenID authentication:
|
309
331
|
|
310
332
|
```ruby
|
@@ -349,17 +371,65 @@ The output:
|
|
349
371
|
|
350
372
|
```
|
351
373
|
|
374
|
+
### OAuth2
|
375
|
+
|
352
376
|
You can see on the next link how to get the OAuth2 token for Microsoft Azure and add it to your Http connection header.
|
353
377
|
|
354
378
|
https://gist.github.com/MarioRuiz/d3525185024737885c0c9afa6dc8b9e5
|
355
379
|
|
380
|
+
### JWT token
|
381
|
+
|
382
|
+
An example for Google using JWT
|
383
|
+
|
384
|
+
my_json_key_file.json:
|
385
|
+
```json
|
386
|
+
{
|
387
|
+
"type": "service_account",
|
388
|
+
"project_id": "example",
|
389
|
+
"private_key_id": "fjdslkafldkasfadsjflkjdsaklfjasdklfjlkdsjfl",
|
390
|
+
"private_key": "-----BEGIN PRIVATE KEY-----....==\n-----END PRIVATE KEY-----\n",
|
391
|
+
"client_email": "example@example.iam.gserviceaccount.com",
|
392
|
+
"client_id": "46545646",
|
393
|
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
394
|
+
"token_uri": "https://oauth2.googleapis.com/token",
|
395
|
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
396
|
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/example%40example.iam.gserviceaccount.com"
|
397
|
+
}
|
398
|
+
```
|
399
|
+
|
400
|
+
```ruby
|
401
|
+
require 'jwt'
|
402
|
+
require 'nice_hash'
|
403
|
+
def generate_jwt(audience, json_key_file)
|
404
|
+
json = File.open(json_key_file).read.json
|
405
|
+
now = Time.new
|
406
|
+
payload = {
|
407
|
+
iss: json.client_email,
|
408
|
+
sub: json.client_email,
|
409
|
+
aud: audience,
|
410
|
+
exp: (now + 3600).to_i,
|
411
|
+
iat: (now - 60).to_i,
|
412
|
+
kid: json.private_key_id
|
413
|
+
}
|
414
|
+
jwt_token = JWT.encode payload, OpenSSL::PKey::RSA.new(json.private_key), "RS256"
|
415
|
+
return jwt_token
|
416
|
+
end
|
417
|
+
|
418
|
+
NiceHttp.headers = {
|
419
|
+
Authorization: lambda { "Bearer " + generate_jwt('https:/myhost.com', './my_json_key_file.json') }
|
420
|
+
}
|
421
|
+
|
422
|
+
```
|
423
|
+
|
424
|
+
### lambda on headers
|
425
|
+
|
356
426
|
If you need a new token every time a new http connection is created you can use `lambda`
|
357
427
|
|
358
428
|
```ruby
|
359
429
|
NiceHttp.headers[:Authorization] = lambda {get_token()}
|
360
430
|
```
|
361
431
|
|
362
|
-
NiceHttp will call the get_token method you created every time a new
|
432
|
+
NiceHttp will call the get_token method you created every time a new connection is created.
|
363
433
|
|
364
434
|
## Http logs
|
365
435
|
|
@@ -489,7 +559,7 @@ RESPONSE:
|
|
489
559
|
|
490
560
|
```
|
491
561
|
|
492
|
-
If you want to get the last request sent or the last response use `NiceHttp.last_request` or `NiceHttp.last_response`
|
562
|
+
If you want to get the last request sent or the last response as a message use `NiceHttp.last_request` or `NiceHttp.last_response`. If you want to access the last request as a Hash use `NiceHttp.request`
|
493
563
|
|
494
564
|
Also you can collect all data sent and received by setting `NiceHttp.capture = true` and all data will be stored on `NiceHttp.captured` as an Array of Strings (Request+Response).
|
495
565
|
|
data/lib/nice_http.rb
CHANGED
@@ -9,7 +9,7 @@ require_relative "nice_http/http_methods"
|
|
9
9
|
# Attributes you can access using NiceHttp.the_attribute:
|
10
10
|
# :host, :port, :ssl, :headers, :debug, :log, :log_headers, :proxy_host, :proxy_port,
|
11
11
|
# :last_request, :last_response, :request_id, :use_mocks, :connections,
|
12
|
-
# :active, :auto_redirect, :values_for, :create_stats, :stats, :capture, :captured
|
12
|
+
# :active, :auto_redirect, :values_for, :create_stats, :stats, :capture, :captured, :request, :requests
|
13
13
|
#
|
14
14
|
# @attr [String] host The host to be accessed
|
15
15
|
# @attr [Integer] port The port number
|
@@ -29,6 +29,8 @@ require_relative "nice_http/http_methods"
|
|
29
29
|
# @attr [Integer] proxy_port the proxy port to be used
|
30
30
|
# @attr [String] last_request The last request with all the content sent
|
31
31
|
# @attr [String] last_response Only in case :debug is true, the last response with all the content
|
32
|
+
# @attr [Hash] request The last request with all the content sent
|
33
|
+
# @attr [Hash] requests The defaults for all requests. keys: :headers and :data
|
32
34
|
# @attr [String] request_id If the response includes a requestId, will be stored here
|
33
35
|
# @attr [Boolean] use_mocks If true, in case the request hash includes a :mock_response key, it will be used as the response instead
|
34
36
|
# @attr [Array] connections It will include all the active connections (NiceHttp instances)
|
@@ -70,8 +72,8 @@ class NiceHttp
|
|
70
72
|
|
71
73
|
class << self
|
72
74
|
attr_accessor :host, :port, :ssl, :headers, :debug, :log_path, :log, :proxy_host, :proxy_port, :log_headers,
|
73
|
-
:last_request, :last_response, :request_id, :use_mocks, :connections,
|
74
|
-
:active, :auto_redirect, :log_files, :values_for, :create_stats, :stats, :capture, :captured
|
75
|
+
:last_request, :last_response, :request, :request_id, :use_mocks, :connections,
|
76
|
+
:active, :auto_redirect, :log_files, :values_for, :create_stats, :stats, :capture, :captured, :requests
|
75
77
|
end
|
76
78
|
|
77
79
|
at_exit do
|
@@ -96,6 +98,8 @@ class NiceHttp
|
|
96
98
|
@proxy_host = nil
|
97
99
|
@proxy_port = nil
|
98
100
|
@last_request = nil
|
101
|
+
@request = nil
|
102
|
+
@requests = nil
|
99
103
|
@last_response = nil
|
100
104
|
@request_id = ""
|
101
105
|
@use_mocks = false
|
@@ -13,6 +13,9 @@ module NiceHttpManageRequest
|
|
13
13
|
require "json"
|
14
14
|
|
15
15
|
@prev_request = Hash.new() if @prev_request.nil?
|
16
|
+
@defaults_request = self.class.requests if @defaults_request.nil? and self.class.requests.is_a?(Hash)
|
17
|
+
@request = Hash.new() if @request.nil?
|
18
|
+
@defaults_request = Hash.new() unless @defaults_request.is_a?(Hash)
|
16
19
|
|
17
20
|
begin
|
18
21
|
content_type_included = false
|
@@ -21,6 +24,8 @@ module NiceHttpManageRequest
|
|
21
24
|
|
22
25
|
@response = Hash.new()
|
23
26
|
headers_t = @headers.dup()
|
27
|
+
headers_t.merge!(@defaults_request[:headers]) if @defaults_request.key?(:headers)
|
28
|
+
|
24
29
|
cookies_to_set_str = ""
|
25
30
|
if arguments.size == 3
|
26
31
|
path = arguments[0]
|
@@ -125,6 +130,13 @@ module NiceHttpManageRequest
|
|
125
130
|
}
|
126
131
|
end
|
127
132
|
elsif data.kind_of?(Hash)
|
133
|
+
data.merge!(@defaults_request[:data]) if @defaults_request.key?(:data)
|
134
|
+
#lambdas on data only supports on root of the hash
|
135
|
+
data.each do |k, v|
|
136
|
+
if v.is_a?(Proc)
|
137
|
+
data[k] = v.call
|
138
|
+
end
|
139
|
+
end
|
128
140
|
if arguments[0].include?(:values_for)
|
129
141
|
data = data.set_values(arguments[0][:values_for])
|
130
142
|
end
|
@@ -192,6 +204,40 @@ module NiceHttpManageRequest
|
|
192
204
|
headers_t["Accept-Encoding"].gsub!("gzip", "") #removed so the response is in plain text
|
193
205
|
end
|
194
206
|
|
207
|
+
if data.to_s() != "" and encoding.to_s().upcase != "UTF-8" and encoding != ""
|
208
|
+
data = data.to_s().encode(encoding, "UTF-8")
|
209
|
+
end
|
210
|
+
@request[:path] = path
|
211
|
+
@request[:data] = data
|
212
|
+
@request[:headers] = headers_t
|
213
|
+
@request[:method] = method_s.upcase
|
214
|
+
if arguments.size == 1 and arguments[0].kind_of?(Hash) and arguments[0].key?(:name)
|
215
|
+
@request[:name] = arguments[0][:name]
|
216
|
+
end
|
217
|
+
self.class.request = @request
|
218
|
+
headers_t.each do |k, v|
|
219
|
+
# for lambdas
|
220
|
+
if v.is_a?(Proc)
|
221
|
+
headers_t[k] = v.call
|
222
|
+
end
|
223
|
+
end
|
224
|
+
@request[:headers] = headers_t
|
225
|
+
self.class.request = @request
|
226
|
+
|
227
|
+
if @debug or @prev_request[:path] != path or @prev_request[:headers] != headers_t or @prev_request[:data] != data
|
228
|
+
show_headers_data = true
|
229
|
+
else
|
230
|
+
show_headers_data = false
|
231
|
+
end
|
232
|
+
|
233
|
+
@prev_request[:path] = path
|
234
|
+
@prev_request[:data] = data
|
235
|
+
@prev_request[:headers] = headers_t
|
236
|
+
@prev_request[:method] = method_s.upcase
|
237
|
+
if arguments.size == 1 and arguments[0].kind_of?(Hash) and arguments[0].key?(:name)
|
238
|
+
@prev_request[:name] = arguments[0][:name]
|
239
|
+
end
|
240
|
+
|
195
241
|
headers_ts = ""
|
196
242
|
|
197
243
|
if @log_headers == :none
|
@@ -217,7 +263,7 @@ module NiceHttpManageRequest
|
|
217
263
|
message += "#{method_s.upcase} Request"
|
218
264
|
end
|
219
265
|
message += "\n path: " + path.to_s() + "\n"
|
220
|
-
if
|
266
|
+
if show_headers_data
|
221
267
|
message += " headers: {" + headers_ts.to_s() + "}\n"
|
222
268
|
message += " data: " + data_s.to_s() + "\n"
|
223
269
|
message = @message_server + "\n" + message
|
@@ -232,22 +278,6 @@ module NiceHttpManageRequest
|
|
232
278
|
@logger.info(message)
|
233
279
|
end
|
234
280
|
|
235
|
-
if data.to_s() != "" and encoding.to_s().upcase != "UTF-8" and encoding != ""
|
236
|
-
data = data.to_s().encode(encoding, "UTF-8")
|
237
|
-
end
|
238
|
-
headers_t.each do |k, v|
|
239
|
-
# for lambdas
|
240
|
-
if v.is_a?(Proc)
|
241
|
-
headers_t[k] = v.call
|
242
|
-
end
|
243
|
-
end
|
244
|
-
@prev_request[:path] = path
|
245
|
-
@prev_request[:data] = data
|
246
|
-
@prev_request[:headers] = headers_t
|
247
|
-
@prev_request[:method] = method_s.upcase
|
248
|
-
if arguments.size == 1 and arguments[0].kind_of?(Hash) and arguments[0].key?(:name)
|
249
|
-
@prev_request[:name] = arguments[0][:name]
|
250
|
-
end
|
251
281
|
return path, data, headers_t
|
252
282
|
rescue Exception => stack
|
253
283
|
@logger.fatal(stack)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nice_http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.8.
|
4
|
+
version: 1.8.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mario Ruiz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-04-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nice_hash
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '1.15'
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 1.15.
|
22
|
+
version: 1.15.6
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '1.15'
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: 1.15.
|
32
|
+
version: 1.15.6
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: rspec
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|