@@ -41,8 +41,6 @@ type ExpectedNginxField struct {
4141// ValidateNginxFieldExists accepts the nginx config and the configuration for the expected field,
4242// and returns whether or not that field exists where it should.
4343func ValidateNginxFieldExists (conf * Payload , expFieldCfg ExpectedNginxField ) error {
44- var directiveFoundInServer , directiveFoundInUpstream bool
45-
4644 b , err := json .Marshal (conf )
4745 if err != nil {
4846 return fmt .Errorf ("error marshaling nginx config: %w" , err )
@@ -61,15 +59,11 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
6159 continue
6260 }
6361
64- directiveFoundInServer = validateServerBlockDirectives (expFieldCfg , * directive )
65-
66- directiveFoundInUpstream = validateUpstreamDirectives (expFieldCfg , * directive )
67-
68- if len (expFieldCfg .Server ) > 0 && directiveFoundInServer {
62+ if len (expFieldCfg .Server ) > 0 && fieldExistsInServer (expFieldCfg , * directive ) {
6963 return nil
7064 }
7165
72- if len (expFieldCfg .Upstream ) > 0 && directiveFoundInUpstream {
66+ if len (expFieldCfg .Upstream ) > 0 && fieldExistsInUpstream ( expFieldCfg , * directive ) {
7367 return nil
7468 }
7569 }
@@ -78,37 +72,35 @@ func ValidateNginxFieldExists(conf *Payload, expFieldCfg ExpectedNginxField) err
7872 return fmt .Errorf ("directive %s not found in: nginx config %s" , expFieldCfg .Directive , string (b ))
7973}
8074
81- func validateServerBlockDirectives (
75+ func fieldExistsInServer (
8276 expFieldCfg ExpectedNginxField ,
8377 directive Directive ,
8478) bool {
85- var fieldFound bool
8679 if directive .Directive == "server" && getServerName (directive .Block ) == expFieldCfg .Server {
8780 for _ , serverDirective := range directive .Block {
8881 if expFieldCfg .Location == "" && expFieldCfg .fieldFound (serverDirective ) {
89- fieldFound = true
82+ return true
9083 } else if serverDirective .Directive == "location" &&
9184 fieldExistsInLocation (serverDirective , expFieldCfg ) {
92- fieldFound = true
85+ return true
9386 }
9487 }
9588 }
96- return fieldFound
89+ return false
9790}
9891
99- func validateUpstreamDirectives (
92+ func fieldExistsInUpstream (
10093 expFieldCfg ExpectedNginxField ,
10194 directive Directive ,
10295) bool {
103- var fieldFound bool
10496 if directive .Directive == "upstream" && directive .Args [0 ] == expFieldCfg .Upstream {
10597 for _ , directive := range directive .Block {
10698 if expFieldCfg .fieldFound (directive ) {
107- fieldFound = true
99+ return true
108100 }
109101 }
110102 }
111- return fieldFound
103+ return false
112104}
113105
114106func getServerName (serverBlock Directives ) string {
0 commit comments