fbe 0.0.82 → 0.0.83
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/Gemfile.lock +6 -5
- data/lib/fbe/iterate.rb +3 -3
- data/lib/fbe/middleware/formatter.rb +84 -0
- data/lib/fbe/middleware/quota.rb +1 -3
- data/lib/fbe/octo.rb +2 -12
- data/lib/fbe.rb +1 -1
- data/test/fbe/middleware/test_formatter.rb +100 -0
- metadata +4 -4
- data/lib/fbe/middleware/logging_formatter.rb +0 -63
- data/test/fbe/middleware/test_logging_formatter.rb +0 -99
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 650928bcebcf4d25d8f9d1cb38036a3ffddc9169316692023b3be534ccfadf39
|
|
4
|
+
data.tar.gz: 2ee99b9be72972ad948457f2c636a746d48161a9e7b3840f00926a39bfa9e7a8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: aad6fbd2f387ac6fe8f88111f57e301224279a288ca45d447b3a66f3dba9cc53276438966a68f86544df7e49cbc9b92dcb075a55db29a2c086d8a083d8ae43d1
|
|
7
|
+
data.tar.gz: 5c4b9b4379a7565b5e06e2458474d4710f7b4c78791d389fd66da3b14029ea2fbb5e244b86ca69b2555db2edaa6611857f84f268f7f6581950dee7c4f9d73b86
|
data/Gemfile.lock
CHANGED
|
@@ -86,7 +86,7 @@ GEM
|
|
|
86
86
|
elapsed (0.0.1)
|
|
87
87
|
loog (> 0)
|
|
88
88
|
tago (> 0)
|
|
89
|
-
erubi (1.13.
|
|
89
|
+
erubi (1.13.1)
|
|
90
90
|
ethon (0.16.0)
|
|
91
91
|
ffi (>= 1.15.0)
|
|
92
92
|
factbase (0.4.0)
|
|
@@ -130,8 +130,8 @@ GEM
|
|
|
130
130
|
rdoc (>= 4.0.0)
|
|
131
131
|
reline (>= 0.4.2)
|
|
132
132
|
iri (0.8.0)
|
|
133
|
-
json (2.9.
|
|
134
|
-
judges (0.
|
|
133
|
+
json (2.9.1)
|
|
134
|
+
judges (0.28.1)
|
|
135
135
|
backtrace (~> 0)
|
|
136
136
|
baza.rb (~> 0)
|
|
137
137
|
concurrent-ruby (~> 1.2)
|
|
@@ -148,7 +148,7 @@ GEM
|
|
|
148
148
|
typhoeus (~> 1.3)
|
|
149
149
|
language_server-protocol (3.17.0.3)
|
|
150
150
|
liquid (5.5.1)
|
|
151
|
-
logger (1.6.
|
|
151
|
+
logger (1.6.4)
|
|
152
152
|
loofah (2.23.1)
|
|
153
153
|
crass (~> 1.0.2)
|
|
154
154
|
nokogiri (>= 1.12.0)
|
|
@@ -290,6 +290,7 @@ GEM
|
|
|
290
290
|
PLATFORMS
|
|
291
291
|
arm64-darwin-22
|
|
292
292
|
arm64-darwin-23
|
|
293
|
+
arm64-darwin-24
|
|
293
294
|
x64-mingw-ucrt
|
|
294
295
|
x86_64-darwin-20
|
|
295
296
|
x86_64-darwin-21
|
|
@@ -311,4 +312,4 @@ DEPENDENCIES
|
|
|
311
312
|
yard (= 0.9.37)
|
|
312
313
|
|
|
313
314
|
BUNDLED WITH
|
|
314
|
-
2.
|
|
315
|
+
2.5.16
|
data/lib/fbe/iterate.rb
CHANGED
|
@@ -175,15 +175,15 @@ class Fbe::Iterate
|
|
|
175
175
|
end
|
|
176
176
|
end
|
|
177
177
|
if oct.off_quota
|
|
178
|
-
@loog.
|
|
178
|
+
@loog.info("We are off GitHub quota, time to stop after #{start.ago}")
|
|
179
179
|
break
|
|
180
180
|
end
|
|
181
181
|
unless seen.any? { |r, v| v < @repeats && !restarted.include?(r) }
|
|
182
|
-
@loog.debug("No more repos to scan (out of #{repos.size}), quitting")
|
|
182
|
+
@loog.debug("No more repos to scan (out of #{repos.size}), quitting after #{start.ago}")
|
|
183
183
|
break
|
|
184
184
|
end
|
|
185
185
|
if restarted.size == repos.size
|
|
186
|
-
@loog.debug("All #{repos.size} repos restarted, quitting")
|
|
186
|
+
@loog.debug("All #{repos.size} repos restarted, quitting after #{start.ago}")
|
|
187
187
|
break
|
|
188
188
|
end
|
|
189
189
|
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# MIT License
|
|
4
|
+
#
|
|
5
|
+
# Copyright (c) 2024 Zerocracy
|
|
6
|
+
#
|
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
# furnished to do so, subject to the following conditions:
|
|
13
|
+
#
|
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
# copies or substantial portions of the Software.
|
|
16
|
+
#
|
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
# SOFTWARE.
|
|
24
|
+
|
|
25
|
+
require 'faraday/logging/formatter'
|
|
26
|
+
require_relative '../../fbe'
|
|
27
|
+
require_relative '../../fbe/middleware'
|
|
28
|
+
|
|
29
|
+
# Faraday logging formatter show verbose log for only error response
|
|
30
|
+
#
|
|
31
|
+
# Author:: Yegor Bugayenko (yegor256@gmail.com)
|
|
32
|
+
# Copyright:: Copyright (c) 2024 Zerocracy
|
|
33
|
+
# License:: MIT
|
|
34
|
+
class Fbe::Middleware::Formatter < Faraday::Logging::Formatter
|
|
35
|
+
# Log HTTP request.
|
|
36
|
+
#
|
|
37
|
+
# @param [Hash] http The hash with data about HTTP request
|
|
38
|
+
def request(http)
|
|
39
|
+
@req = http
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Log HTTP response.
|
|
43
|
+
#
|
|
44
|
+
# @param [Hash] http The hash with data about HTTP response
|
|
45
|
+
def response(http)
|
|
46
|
+
return if http.status < 400
|
|
47
|
+
if http.status == 403 && http.response_headers['content-type'].start_with?('application/json')
|
|
48
|
+
warn(
|
|
49
|
+
[
|
|
50
|
+
"#{@req.method.upcase} #{apply_filters(@req.url.to_s)}",
|
|
51
|
+
'->',
|
|
52
|
+
http.status,
|
|
53
|
+
'/',
|
|
54
|
+
JSON.parse(http.response_body)['message']
|
|
55
|
+
].join(' ')
|
|
56
|
+
)
|
|
57
|
+
return
|
|
58
|
+
end
|
|
59
|
+
error(
|
|
60
|
+
[
|
|
61
|
+
"#{@req.method.upcase} #{apply_filters(@req.url.to_s)} HTTP/1.1",
|
|
62
|
+
shifted(apply_filters(dump_headers(@req.request_headers))),
|
|
63
|
+
'',
|
|
64
|
+
shifted(apply_filters(@req.request_body)),
|
|
65
|
+
"HTTP/1.1 #{http.status}",
|
|
66
|
+
shifted(apply_filters(dump_headers(http.response_headers))),
|
|
67
|
+
'',
|
|
68
|
+
shifted(apply_filters(http.response_body))
|
|
69
|
+
].join("\n")
|
|
70
|
+
)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
def shifted(txt)
|
|
76
|
+
return '' if txt.nil?
|
|
77
|
+
" #{txt.gsub("\n", "\n ")}"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def dump_headers(headers)
|
|
81
|
+
return '' if headers.nil?
|
|
82
|
+
headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
|
|
83
|
+
end
|
|
84
|
+
end
|
data/lib/fbe/middleware/quota.rb
CHANGED
|
@@ -51,9 +51,7 @@ class Fbe::Middleware::Quota < Faraday::Middleware
|
|
|
51
51
|
@requests += 1
|
|
52
52
|
response = @app.call(env)
|
|
53
53
|
if out_of_limit?(env)
|
|
54
|
-
@loog.info(
|
|
55
|
-
"Too much GitHub API quota consumed, pausing for #{@pause} seconds"
|
|
56
|
-
)
|
|
54
|
+
@loog.info("Too much GitHub API quota consumed, pausing for #{@pause} seconds")
|
|
57
55
|
sleep(@pause)
|
|
58
56
|
@requests = 0
|
|
59
57
|
end
|
data/lib/fbe/octo.rb
CHANGED
|
@@ -31,7 +31,7 @@ require 'octokit'
|
|
|
31
31
|
require 'verbose'
|
|
32
32
|
require_relative '../fbe'
|
|
33
33
|
require_relative 'middleware'
|
|
34
|
-
require_relative 'middleware/
|
|
34
|
+
require_relative 'middleware/formatter'
|
|
35
35
|
require_relative 'middleware/quota'
|
|
36
36
|
|
|
37
37
|
# Makes a call to the GitHub API.
|
|
@@ -93,17 +93,7 @@ def Fbe.octo(options: $options, global: $global, loog: $loog)
|
|
|
93
93
|
builder.use(Fbe::Middleware::Quota, loog:, pause: options.github_api_pause || 60)
|
|
94
94
|
builder.use(Faraday::HttpCache, serializer: Marshal, shared_cache: false, logger: Loog::NULL)
|
|
95
95
|
builder.use(Octokit::Response::RaiseError)
|
|
96
|
-
builder.use(
|
|
97
|
-
Faraday::Response::Logger,
|
|
98
|
-
loog,
|
|
99
|
-
{
|
|
100
|
-
formatter: Fbe::Middleware::LoggingFormatter,
|
|
101
|
-
log_only_errors: true,
|
|
102
|
-
headers: true,
|
|
103
|
-
bodies: true,
|
|
104
|
-
errors: false
|
|
105
|
-
}
|
|
106
|
-
)
|
|
96
|
+
builder.use(Faraday::Response::Logger, loog, formatter: Fbe::Middleware::Formatter)
|
|
107
97
|
builder.adapter(Faraday.default_adapter)
|
|
108
98
|
end
|
|
109
99
|
o.middleware = stack
|
data/lib/fbe.rb
CHANGED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# MIT License
|
|
4
|
+
#
|
|
5
|
+
# Copyright (c) 2024 Zerocracy
|
|
6
|
+
#
|
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
# furnished to do so, subject to the following conditions:
|
|
13
|
+
#
|
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
# copies or substantial portions of the Software.
|
|
16
|
+
#
|
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
# SOFTWARE.
|
|
24
|
+
|
|
25
|
+
require 'faraday'
|
|
26
|
+
require 'loog'
|
|
27
|
+
require 'minitest/autorun'
|
|
28
|
+
require_relative '../../../lib/fbe'
|
|
29
|
+
require_relative '../../../lib/fbe/middleware'
|
|
30
|
+
require_relative '../../../lib/fbe/middleware/formatter'
|
|
31
|
+
|
|
32
|
+
# Test.
|
|
33
|
+
# Author:: Yegor Bugayenko (yegor256@gmail.com)
|
|
34
|
+
# Copyright:: Copyright (c) 2024 Zerocracy
|
|
35
|
+
# License:: MIT
|
|
36
|
+
class LoggingFormatterTest < Minitest::Test
|
|
37
|
+
def test_success_response
|
|
38
|
+
log_it(status: 200) do |loog|
|
|
39
|
+
assert_empty(loog.to_s)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_forward_response
|
|
44
|
+
log_it(status: 303) do |loog|
|
|
45
|
+
assert_empty(loog.to_s)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_error_response
|
|
50
|
+
log_it(status: 401) do |loog|
|
|
51
|
+
str = loog.to_s
|
|
52
|
+
refute_empty(str)
|
|
53
|
+
assert_match(%r{http://example.com}, str)
|
|
54
|
+
assert_match(/Authorization:/, str)
|
|
55
|
+
assert_match(%r{HTTP/1.1 401}, str)
|
|
56
|
+
assert_match(/x-github-api-version-selected: "2022-11-28"/, str)
|
|
57
|
+
assert_match(/hello, world!/, str)
|
|
58
|
+
assert_match(/request body/, str)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_limit_response
|
|
63
|
+
log_it(status: 403) do |loog|
|
|
64
|
+
str = loog.to_s
|
|
65
|
+
refute_empty(str)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
def log_it(status:, method: :get)
|
|
72
|
+
loog = Loog::Buffer.new
|
|
73
|
+
formatter = Fbe::Middleware::Formatter.new(logger: loog, options: {})
|
|
74
|
+
formatter.request(
|
|
75
|
+
Faraday::Env.from(
|
|
76
|
+
{
|
|
77
|
+
method:,
|
|
78
|
+
request_body: 'some request body',
|
|
79
|
+
url: URI('http://example.com'),
|
|
80
|
+
request_headers: {
|
|
81
|
+
'Authorization' => 'Bearer github_pat_11AAsecret'
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
)
|
|
85
|
+
)
|
|
86
|
+
formatter.response(
|
|
87
|
+
Faraday::Env.from(
|
|
88
|
+
{
|
|
89
|
+
status:,
|
|
90
|
+
response_body: '{"message": "hello, world!"}',
|
|
91
|
+
response_headers: {
|
|
92
|
+
'content-type' => 'application/json',
|
|
93
|
+
'x-github-api-version-selected' => '2022-11-28'
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
yield loog
|
|
99
|
+
end
|
|
100
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: fbe
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.83
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yegor Bugayenko
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2024-12-
|
|
11
|
+
date: 2024-12-20 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: backtrace
|
|
@@ -287,7 +287,7 @@ files:
|
|
|
287
287
|
- lib/fbe/iterate.rb
|
|
288
288
|
- lib/fbe/just_one.rb
|
|
289
289
|
- lib/fbe/middleware.rb
|
|
290
|
-
- lib/fbe/middleware/
|
|
290
|
+
- lib/fbe/middleware/formatter.rb
|
|
291
291
|
- lib/fbe/middleware/quota.rb
|
|
292
292
|
- lib/fbe/octo.rb
|
|
293
293
|
- lib/fbe/overwrite.rb
|
|
@@ -299,7 +299,7 @@ files:
|
|
|
299
299
|
- lib/fbe/who.rb
|
|
300
300
|
- renovate.json
|
|
301
301
|
- rules/basic.fe
|
|
302
|
-
- test/fbe/middleware/
|
|
302
|
+
- test/fbe/middleware/test_formatter.rb
|
|
303
303
|
- test/fbe/middleware/test_quota.rb
|
|
304
304
|
- test/fbe/test_award.rb
|
|
305
305
|
- test/fbe/test_bylaws.rb
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# MIT License
|
|
4
|
-
#
|
|
5
|
-
# Copyright (c) 2024 Zerocracy
|
|
6
|
-
#
|
|
7
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
-
# of this software and associated documentation files (the "Software"), to deal
|
|
9
|
-
# in the Software without restriction, including without limitation the rights
|
|
10
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
-
# copies of the Software, and to permit persons to whom the Software is
|
|
12
|
-
# furnished to do so, subject to the following conditions:
|
|
13
|
-
#
|
|
14
|
-
# The above copyright notice and this permission notice shall be included in all
|
|
15
|
-
# copies or substantial portions of the Software.
|
|
16
|
-
#
|
|
17
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
-
# SOFTWARE.
|
|
24
|
-
|
|
25
|
-
require 'faraday'
|
|
26
|
-
|
|
27
|
-
# Faraday logging formatter show verbose log for only error response
|
|
28
|
-
class Fbe::Middleware::LoggingFormatter < Faraday::Logging::Formatter
|
|
29
|
-
AUTHORIZATION_FILTER = [/(Authorization: )([^&]+)([^&]{5})/, '\1********\3'].freeze
|
|
30
|
-
|
|
31
|
-
def initialize(**)
|
|
32
|
-
super
|
|
33
|
-
filter(*AUTHORIZATION_FILTER)
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def request(env)
|
|
37
|
-
super unless log_only_errors?
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def response(env)
|
|
41
|
-
return super unless log_only_errors?
|
|
42
|
-
request_with_response(env) if env.status.nil? || env.status >= 400
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
def request_with_response(env)
|
|
46
|
-
oll = @options[:log_level]
|
|
47
|
-
@options[:log_level] = :error
|
|
48
|
-
public_send(log_level, 'request') do
|
|
49
|
-
"#{env.method.upcase} #{apply_filters(env.url.to_s)}"
|
|
50
|
-
end
|
|
51
|
-
log_headers('request', env.request_headers) if log_headers?(:request)
|
|
52
|
-
log_body('request', env[:request_body]) if env[:request_body] && log_body?(:request)
|
|
53
|
-
public_send(log_level, 'response') { "Status #{env.status}" }
|
|
54
|
-
log_headers('response', env.response_headers) if log_headers?(:response)
|
|
55
|
-
log_body('response', env[:response_body]) if env[:response_body] && log_body?(:response)
|
|
56
|
-
@options[:log_level] = oll
|
|
57
|
-
nil
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def log_only_errors?
|
|
61
|
-
@options[:log_only_errors]
|
|
62
|
-
end
|
|
63
|
-
end
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# MIT License
|
|
4
|
-
#
|
|
5
|
-
# Copyright (c) 2024 Zerocracy
|
|
6
|
-
#
|
|
7
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
-
# of this software and associated documentation files (the "Software"), to deal
|
|
9
|
-
# in the Software without restriction, including without limitation the rights
|
|
10
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
-
# copies of the Software, and to permit persons to whom the Software is
|
|
12
|
-
# furnished to do so, subject to the following conditions:
|
|
13
|
-
#
|
|
14
|
-
# The above copyright notice and this permission notice shall be included in all
|
|
15
|
-
# copies or substantial portions of the Software.
|
|
16
|
-
#
|
|
17
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
-
# SOFTWARE.
|
|
24
|
-
|
|
25
|
-
require 'minitest/autorun'
|
|
26
|
-
require 'faraday'
|
|
27
|
-
require 'loog'
|
|
28
|
-
require_relative '../../../lib/fbe/middleware'
|
|
29
|
-
require_relative '../../../lib/fbe/middleware/logging_formatter'
|
|
30
|
-
|
|
31
|
-
class LoggingFormatterTest < Minitest::Test
|
|
32
|
-
def test_success_response_with_debug_log_level
|
|
33
|
-
run_logging_formatter(status: 200, log_level: Logger::DEBUG) do |logger|
|
|
34
|
-
assert_empty(logger.to_s)
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def test_success_response_with_error_log_level
|
|
39
|
-
run_logging_formatter(status: 307, log_level: Logger::ERROR) do |logger|
|
|
40
|
-
assert_empty(logger.to_s)
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
def test_error_response_with_debug_log_level
|
|
45
|
-
run_logging_formatter(status: 400, log_level: Logger::DEBUG) do |logger|
|
|
46
|
-
str = logger.to_s
|
|
47
|
-
refute_empty(str)
|
|
48
|
-
assert_match(%r{http://example.com}, str)
|
|
49
|
-
assert_match(/Authorization: [\*]{8}cret"/, str)
|
|
50
|
-
assert_match(/Status 400/, str)
|
|
51
|
-
assert_match(/x-github-api-version-selected: "2022-11-28"/, str)
|
|
52
|
-
assert_match(/some response body/, str)
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def test_error_response_with_error_log_level
|
|
57
|
-
run_logging_formatter(method: :post, status: 500, log_level: Logger::ERROR) do |logger|
|
|
58
|
-
str = logger.to_s
|
|
59
|
-
refute_empty(str)
|
|
60
|
-
assert_match(%r{http://example.com}, str)
|
|
61
|
-
assert_match(/Authorization: [\*]{8}cret"/, str)
|
|
62
|
-
assert_match(/some request body/, str)
|
|
63
|
-
assert_match(/Status 500/, str)
|
|
64
|
-
assert_match(/x-github-api-version-selected: "2022-11-28"/, str)
|
|
65
|
-
assert_match(/some response body/, str)
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
private
|
|
70
|
-
|
|
71
|
-
def run_logging_formatter(status:, log_level:, method: :get)
|
|
72
|
-
logger = Loog::Buffer.new(level: log_level)
|
|
73
|
-
options = {
|
|
74
|
-
log_only_errors: true,
|
|
75
|
-
headers: true,
|
|
76
|
-
bodies: true,
|
|
77
|
-
errors: false
|
|
78
|
-
}
|
|
79
|
-
formatter = Fbe::Middleware::LoggingFormatter.new(logger:, options:)
|
|
80
|
-
env = Faraday::Env.from(
|
|
81
|
-
{
|
|
82
|
-
method:,
|
|
83
|
-
request_body: method == :get ? nil : 'some request body',
|
|
84
|
-
url: URI('http://example.com'),
|
|
85
|
-
request_headers: {
|
|
86
|
-
'Authorization' => 'Bearer github_pat_11AAsecret'
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
)
|
|
90
|
-
formatter.request(env)
|
|
91
|
-
env[:response_headers] = {
|
|
92
|
-
'x-github-api-version-selected' => '2022-11-28'
|
|
93
|
-
}
|
|
94
|
-
env[:status] = status
|
|
95
|
-
env[:response_body] = 'some response body'
|
|
96
|
-
formatter.response(env)
|
|
97
|
-
yield logger
|
|
98
|
-
end
|
|
99
|
-
end
|