mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-09 04:18:50 +03:00
Merge pull request #1032 from mholt/errors-config-check-duplicate-status-codes
Check for duplicate status code entries in 'errors' directive
This commit is contained in:
commit
ba5aeab19d
2 changed files with 19 additions and 0 deletions
|
@ -123,12 +123,20 @@ func errorsParse(c *caddy.Controller) (*ErrorHandler, error) {
|
||||||
f.Close()
|
f.Close()
|
||||||
|
|
||||||
if what == "*" {
|
if what == "*" {
|
||||||
|
if handler.GenericErrorPage != "" {
|
||||||
|
return hadBlock, c.Errf("Duplicate status code entry: %s", what)
|
||||||
|
}
|
||||||
handler.GenericErrorPage = where
|
handler.GenericErrorPage = where
|
||||||
} else {
|
} else {
|
||||||
whatInt, err := strconv.Atoi(what)
|
whatInt, err := strconv.Atoi(what)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return hadBlock, c.Err("Expecting a numeric status code or '*', got '" + what + "'")
|
return hadBlock, c.Err("Expecting a numeric status code or '*', got '" + what + "'")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, exists := handler.ErrorPages[whatInt]; exists {
|
||||||
|
return hadBlock, c.Errf("Duplicate status code entry: %s", what)
|
||||||
|
}
|
||||||
|
|
||||||
handler.ErrorPages[whatInt] = where
|
handler.ErrorPages[whatInt] = where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,6 +115,15 @@ func TestErrorsParse(t *testing.T) {
|
||||||
503: "503.html",
|
503: "503.html",
|
||||||
},
|
},
|
||||||
}},
|
}},
|
||||||
|
// Next two test cases is the detection of duplicate status codes
|
||||||
|
{`errors {
|
||||||
|
503 503.html
|
||||||
|
503 503.html
|
||||||
|
}`, true, ErrorHandler{}},
|
||||||
|
{`errors {
|
||||||
|
* generic_error.html
|
||||||
|
* generic_error.html
|
||||||
|
}`, true, ErrorHandler{}},
|
||||||
}
|
}
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
actualErrorsRule, err := errorsParse(caddy.NewTestController("http", test.inputErrorsRules))
|
actualErrorsRule, err := errorsParse(caddy.NewTestController("http", test.inputErrorsRules))
|
||||||
|
@ -123,6 +132,8 @@ func TestErrorsParse(t *testing.T) {
|
||||||
t.Errorf("Test %d didn't error, but it should have", i)
|
t.Errorf("Test %d didn't error, but it should have", i)
|
||||||
} else if err != nil && !test.shouldErr {
|
} else if err != nil && !test.shouldErr {
|
||||||
t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
|
t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if actualErrorsRule.LogFile != test.expectedErrorHandler.LogFile {
|
if actualErrorsRule.LogFile != test.expectedErrorHandler.LogFile {
|
||||||
t.Errorf("Test %d expected LogFile to be %s, but got %s",
|
t.Errorf("Test %d expected LogFile to be %s, but got %s",
|
||||||
|
|
Loading…
Reference in a new issue