Skip to content

Commit e1a1ab5

Browse files
committed
Fix typos and validation logic
1 parent 97d4084 commit e1a1ab5

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

app/messages/manifest_routes_update_message.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,25 +110,24 @@ def validate_route_hash_options(route)
110110
hash_header = options[:hash_header]
111111
hash_balance = options[:hash_balance]
112112

113-
return if validate_route_hash_header(route, hash_header)
114-
return if validate_route_hash_balance(route, hash_balance)
113+
validate_route_hash_header(route, hash_header)
114+
validate_route_hash_balance(route, hash_balance)
115115

116116
validate_route_hash_options_with_loadbalancing(route, loadbalancing, hash_header, hash_balance)
117117
end
118118

119119
def validate_route_hash_header(route, hash_header)
120-
return false unless hash_header.present? && (hash_header.to_s.length > 128)
120+
return unless hash_header.present? && (hash_header.to_s.length > 128)
121121

122122
errors.add(:base, message: "Route '#{route[:route]}': Hash header must be at most 128 characters")
123-
true
124123
end
125124

126125
def validate_route_hash_balance(route, hash_balance)
127-
return false if hash_balance.blank?
126+
return if hash_balance.blank?
128127

129128
if hash_balance.to_s.length > 16
130129
errors.add(:base, message: "Route '#{route[:route]}': Hash balance must be at most 16 characters")
131-
return true
130+
return
132131
end
133132

134133
validate_route_hash_balance_numeric(route, hash_balance)
@@ -137,11 +136,9 @@ def validate_route_hash_balance(route, hash_balance)
137136
def validate_route_hash_balance_numeric(route, hash_balance)
138137
balance_float = Float(hash_balance)
139138
# Must be either 0 or >= 1.1 and <= 10.0
140-
errors.add(:base, message: "Route '#{route[:route]}': Hash balance must be either 0 or between to 1.1 and 10.0") unless balance_float == 0 || balance_float.between?(1.1, 10)
141-
false
139+
errors.add(:base, message: "Route '#{route[:route]}': Hash balance must be either 0 or between 1.1 and 10.0") unless balance_float == 0 || balance_float.between?(1.1, 10)
142140
rescue ArgumentError, TypeError
143141
errors.add(:base, message: "Route '#{route[:route]}': Hash balance must be a numeric value")
144-
false
145142
end
146143

147144
def validate_route_hash_options_with_loadbalancing(route, loadbalancing, hash_header, hash_balance)

spec/unit/messages/manifest_routes_update_message_spec.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ module VCAP::CloudController
644644
msg = ManifestRoutesUpdateMessage.new(body)
645645

646646
expect(msg.valid?).to be(false)
647-
expect(msg.errors.full_messages).to include("Route 'existing.example.com': Hash balance must be either 0 or between to 1.1 and 10.0")
647+
expect(msg.errors.full_messages).to include("Route 'existing.example.com': Hash balance must be either 0 or between 1.1 and 10.0")
648648
end
649649
end
650650

@@ -685,7 +685,7 @@ module VCAP::CloudController
685685
msg = ManifestRoutesUpdateMessage.new(body)
686686

687687
expect(msg.valid?).to be(false)
688-
expect(msg.errors.full_messages).to include("Route 'existing.example.com': Hash balance must be either 0 or between to 1.1 and 10.0")
688+
expect(msg.errors.full_messages).to include("Route 'existing.example.com': Hash balance must be either 0 or between 1.1 and 10.0")
689689
end
690690
end
691691

@@ -790,6 +790,29 @@ module VCAP::CloudController
790790
end
791791
end
792792

793+
context 'when a route contains multiple issues with hash options' do
794+
let(:body) do
795+
{ 'routes' =>
796+
[
797+
{ 'route' => 'existing.example.com',
798+
'options' => {
799+
'loadbalancing' => 'hash',
800+
'hash_header' => 'X' * 129,
801+
'hash_balance' => '1' * 17
802+
} }
803+
] }
804+
end
805+
806+
it 'returns false and prints all errors' do
807+
msg = ManifestRoutesUpdateMessage.new(body)
808+
809+
expect(msg.valid?).to be(false)
810+
expect(msg.errors.full_messages).to include("Route 'existing.example.com': Hash header must be at most 128 characters")
811+
expect(msg.errors.full_messages).to include("Route 'existing.example.com': Hash balance must be at most 16 characters")
812+
813+
end
814+
end
815+
793816
context 'when multiple routes have mixed valid and invalid hash options' do
794817
let(:body) do
795818
{ 'routes' =>

0 commit comments

Comments
 (0)