Skip to content

Commit

Permalink
feat: adding log for not found configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
diegorubin committed Oct 29, 2023
1 parent 3909ee0 commit 15bcd92
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 59 deletions.
34 changes: 0 additions & 34 deletions .sourcelevel.yml

This file was deleted.

6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
tshield (0.14.0.0)
tshield (0.15.0.0)
grpc (~> 1.28, >= 1.28.0)
grpc-tools (~> 1.28, >= 1.28.0)
httparty (~> 0.14, >= 0.14.0)
Expand Down Expand Up @@ -59,10 +59,10 @@ GEM
ffi (1.15.1)
formatador (0.2.5)
gherkin (5.1.0)
google-protobuf (3.19.2)
google-protobuf (3.19.2-x86_64-linux)
googleapis-common-protos-types (1.0.6)
google-protobuf (~> 3.14)
grpc (1.38.0)
grpc (1.38.0-x86_64-linux)
google-protobuf (~> 3.15)
googleapis-common-protos-types (~> 1.0)
grpc-tools (1.38.0)
Expand Down
12 changes: 6 additions & 6 deletions config/tshield.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
---
grpc:
port: 5678
proto_dir: 'proto'
proto_dir: "proto"
services:
'helloworld_services_pb':
module: 'Helloworld::Greeter'
hostname: '0.0.0.0:50051'
"helloworld_services_pb":
module: "Helloworld::Greeter"
hostname: "0.0.0.0:50051"
request:
timeout: 10
domains:
'http://localhost:9090':
name: 'components'
"http://localhost:9090":
name: "components"
skip_query_params:
- b
paths:
Expand Down
11 changes: 8 additions & 3 deletions lib/tshield/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'yaml'

require 'tshield/errors'
require 'tshield/after_filter'
require 'tshield/before_filter'
require 'tshield/options'
Expand Down Expand Up @@ -59,7 +60,7 @@ def get_domain_for(path)
result = self.class.get_url_for_domain_by_path(path, config)
return url if result
end
nil
raise ConfigurationNotFoundError.new("Domain not found for path #{path}")
end

def windows_compatibility?
Expand Down Expand Up @@ -89,8 +90,12 @@ def cache_request?(domain)
end

def get_filters(domain)
(domains[domain]['filters'] || [])
.collect { |filter| Class.const_get(filter) }
begin
(domains[domain]['filters'] || [])
.collect { |filter| Class.const_get(filter) }
rescue
puts "Error loading filters for domain #{domain}"
end
end

def get_excluded_headers(domain)
Expand Down
32 changes: 21 additions & 11 deletions lib/tshield/controllers/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ def treat(params, request, _response)
ip: request.ip
}

treat_headers_by_domain(options, path)

if %w[POST PUT PATCH].include? method
result = request.body.read.encode('UTF-8',
invalid: :replace,
Expand All @@ -67,13 +65,21 @@ def treat(params, request, _response)
options[:body] = result
end
api_response = TShield::RequestMatching.new(path, options.clone).match_request

unless api_response
add_headers(options, path)

api_response ||= TShield::RequestVCR.new(path, options.clone).vcr_response
api_response.headers.reject! do |key, _v|
configuration.get_excluded_headers(domain(path)).include?(key)
begin
treat_headers_by_domain(options, path)
add_headers(options, path)

api_response ||= TShield::RequestVCR.new(path, options.clone).vcr_response
api_response.headers.reject! do |key, _v|
configuration.get_excluded_headers(domain(path)).include?(key)
end
rescue ConfigurationNotFoundError => e
logger.error("Error on recover configuration for #{path}")

status 500
body({tshield: e }.to_json)
return
end
end

Expand Down Expand Up @@ -110,9 +116,13 @@ def domain(path)
end

def delay(path)
delay_in_seconds = configuration.get_delay(domain(path), path) || 0
logger.info("Response with delay of #{delay_in_seconds} seconds")
sleep delay_in_seconds
begin
delay_in_seconds = configuration.get_delay(domain(path), path) || 0
logger.info("Response with delay of #{delay_in_seconds} seconds")
sleep delay_in_seconds
rescue ConfigurationNotFoundError
logger.debug('No delay configured')
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/tshield/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@

class AppendSessionWithoutMainSessionError < RuntimeError
end

class ConfigurationNotFoundError < RuntimeError
end

2 changes: 1 addition & 1 deletion lib/tshield/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module TShield
# Control version of gem
class Version
MAJOR = 0
MINOR = 14
MINOR = 15
PATCH = 0
PRE = 0

Expand Down
2 changes: 1 addition & 1 deletion spec/tshield/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
end

it 'return nil if domain not found' do
expect(@configuration.get_domain_for('/api/four')).to be_nil
expect{@configuration.get_domain_for('/api/four')}.to raise_error(ConfigurationNotFoundError)
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/tshield/controllers/requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def body(_value); end
allow(matched_response).to receive(:headers).and_return({})
allow(matched_response).to receive(:body).and_return('')
allow(@mock_logger).to receive(:info)
allow(@mock_logger).to receive(:debug)

expect(TShield::Controllers::Helpers::SessionHelpers)
.to receive(:current_session_call).with(request, '/?a=b', 'GET')
Expand Down

0 comments on commit 15bcd92

Please sign in to comment.