2016-06-04 23:48:27 -05:00
|
|
|
package errors
|
|
|
|
|
|
|
|
import (
|
2016-10-17 20:16:26 -05:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2017-01-24 21:09:44 -05:00
|
|
|
"sync"
|
2016-06-04 23:48:27 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetup(t *testing.T) {
|
2016-06-20 12:44:20 -05:00
|
|
|
c := caddy.NewTestController("http", `errors`)
|
|
|
|
err := setup(c)
|
2016-06-04 23:48:27 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no errors, got: %v", err)
|
|
|
|
}
|
2016-06-20 12:44:20 -05:00
|
|
|
mids := httpserver.GetConfig(c).Middleware()
|
2016-06-04 23:48:27 -05:00
|
|
|
if len(mids) == 0 {
|
|
|
|
t.Fatal("Expected middlewares, was nil instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := mids[0](httpserver.EmptyNext)
|
|
|
|
myHandler, ok := handler.(*ErrorHandler)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Expected handler to be type ErrorHandler, got: %#v", handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
if myHandler.LogFile != "" {
|
|
|
|
t.Errorf("Expected '%s' as the default LogFile", "")
|
|
|
|
}
|
|
|
|
if myHandler.LogRoller != nil {
|
|
|
|
t.Errorf("Expected LogRoller to be nil, got: %v", *myHandler.LogRoller)
|
|
|
|
}
|
|
|
|
if !httpserver.SameNext(myHandler.Next, httpserver.EmptyNext) {
|
|
|
|
t.Error("'Next' field of handler was not set properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Startup function -- TODO
|
|
|
|
// if len(c.Startup) == 0 {
|
|
|
|
// t.Fatal("Expected 1 startup function, had 0")
|
|
|
|
// }
|
|
|
|
// c.Startup[0]()
|
|
|
|
// if myHandler.Log == nil {
|
|
|
|
// t.Error("Expected Log to be non-nil after startup because Debug is not enabled")
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorsParse(t *testing.T) {
|
2016-10-17 20:16:26 -05:00
|
|
|
testAbs, err := filepath.Abs("./404.html")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-06-04 23:48:27 -05:00
|
|
|
tests := []struct {
|
|
|
|
inputErrorsRules string
|
|
|
|
shouldErr bool
|
|
|
|
expectedErrorHandler ErrorHandler
|
|
|
|
}{
|
|
|
|
{`errors`, false, ErrorHandler{
|
2016-10-17 20:16:26 -05:00
|
|
|
ErrorPages: map[int]string{},
|
2017-01-24 21:09:44 -05:00
|
|
|
fileMu: new(sync.RWMutex),
|
2016-06-04 23:48:27 -05:00
|
|
|
}},
|
|
|
|
{`errors errors.txt`, false, ErrorHandler{
|
2016-10-17 20:16:26 -05:00
|
|
|
ErrorPages: map[int]string{},
|
|
|
|
LogFile: "errors.txt",
|
2017-01-24 21:09:44 -05:00
|
|
|
fileMu: new(sync.RWMutex),
|
2016-06-04 23:48:27 -05:00
|
|
|
}},
|
|
|
|
{`errors visible`, false, ErrorHandler{
|
2016-10-17 20:16:26 -05:00
|
|
|
ErrorPages: map[int]string{},
|
|
|
|
Debug: true,
|
2017-01-24 21:09:44 -05:00
|
|
|
fileMu: new(sync.RWMutex),
|
2016-06-04 23:48:27 -05:00
|
|
|
}},
|
|
|
|
{`errors { log visible }`, false, ErrorHandler{
|
2016-10-17 20:16:26 -05:00
|
|
|
ErrorPages: map[int]string{},
|
|
|
|
Debug: true,
|
2017-01-24 21:09:44 -05:00
|
|
|
fileMu: new(sync.RWMutex),
|
2016-06-04 23:48:27 -05:00
|
|
|
}},
|
|
|
|
{`errors { log errors.txt
|
|
|
|
404 404.html
|
|
|
|
500 500.html
|
|
|
|
}`, false, ErrorHandler{
|
|
|
|
LogFile: "errors.txt",
|
|
|
|
ErrorPages: map[int]string{
|
|
|
|
404: "404.html",
|
|
|
|
500: "500.html",
|
|
|
|
},
|
2017-01-24 21:09:44 -05:00
|
|
|
fileMu: new(sync.RWMutex),
|
2016-06-04 23:48:27 -05:00
|
|
|
}},
|
|
|
|
{`errors { log errors.txt { size 2 age 10 keep 3 } }`, false, ErrorHandler{
|
|
|
|
LogFile: "errors.txt",
|
|
|
|
LogRoller: &httpserver.LogRoller{
|
|
|
|
MaxSize: 2,
|
|
|
|
MaxAge: 10,
|
|
|
|
MaxBackups: 3,
|
|
|
|
LocalTime: true,
|
|
|
|
},
|
2016-10-17 20:16:26 -05:00
|
|
|
ErrorPages: map[int]string{},
|
2017-01-24 21:09:44 -05:00
|
|
|
fileMu: new(sync.RWMutex),
|
2016-06-04 23:48:27 -05:00
|
|
|
}},
|
|
|
|
{`errors { log errors.txt {
|
|
|
|
size 3
|
|
|
|
age 11
|
|
|
|
keep 5
|
|
|
|
}
|
|
|
|
404 404.html
|
|
|
|
503 503.html
|
|
|
|
}`, false, ErrorHandler{
|
|
|
|
LogFile: "errors.txt",
|
|
|
|
ErrorPages: map[int]string{
|
|
|
|
404: "404.html",
|
|
|
|
503: "503.html",
|
|
|
|
},
|
|
|
|
LogRoller: &httpserver.LogRoller{
|
|
|
|
MaxSize: 3,
|
|
|
|
MaxAge: 11,
|
|
|
|
MaxBackups: 5,
|
|
|
|
LocalTime: true,
|
|
|
|
},
|
2017-01-24 21:09:44 -05:00
|
|
|
fileMu: new(sync.RWMutex),
|
2016-06-04 23:48:27 -05:00
|
|
|
}},
|
2016-08-11 07:51:15 -05:00
|
|
|
{`errors { log errors.txt
|
|
|
|
* generic_error.html
|
|
|
|
404 404.html
|
|
|
|
503 503.html
|
|
|
|
}`, false, ErrorHandler{
|
|
|
|
LogFile: "errors.txt",
|
|
|
|
GenericErrorPage: "generic_error.html",
|
|
|
|
ErrorPages: map[int]string{
|
|
|
|
404: "404.html",
|
|
|
|
503: "503.html",
|
|
|
|
},
|
2017-01-24 21:09:44 -05:00
|
|
|
fileMu: new(sync.RWMutex),
|
2016-08-11 07:51:15 -05:00
|
|
|
}},
|
2016-10-17 20:16:26 -05:00
|
|
|
// test absolute file path
|
|
|
|
{`errors {
|
|
|
|
404 ` + testAbs + `
|
|
|
|
}`,
|
|
|
|
false, ErrorHandler{
|
|
|
|
ErrorPages: map[int]string{
|
|
|
|
404: testAbs,
|
|
|
|
},
|
2017-01-24 21:09:44 -05:00
|
|
|
fileMu: new(sync.RWMutex),
|
2016-10-17 20:16:26 -05:00
|
|
|
}},
|
2016-08-12 08:47:00 -05:00
|
|
|
// Next two test cases is the detection of duplicate status codes
|
|
|
|
{`errors {
|
|
|
|
503 503.html
|
|
|
|
503 503.html
|
2017-01-24 21:09:44 -05:00
|
|
|
}`, true, ErrorHandler{ErrorPages: map[int]string{}, fileMu: new(sync.RWMutex)}},
|
2016-08-12 08:47:00 -05:00
|
|
|
{`errors {
|
|
|
|
* generic_error.html
|
|
|
|
* generic_error.html
|
2017-01-24 21:09:44 -05:00
|
|
|
}`, true, ErrorHandler{ErrorPages: map[int]string{}, fileMu: new(sync.RWMutex)}},
|
2016-06-04 23:48:27 -05:00
|
|
|
}
|
|
|
|
for i, test := range tests {
|
2016-06-20 12:44:20 -05:00
|
|
|
actualErrorsRule, err := errorsParse(caddy.NewTestController("http", test.inputErrorsRules))
|
2016-06-04 23:48:27 -05:00
|
|
|
|
|
|
|
if err == nil && test.shouldErr {
|
|
|
|
t.Errorf("Test %d didn't error, but it should have", i)
|
|
|
|
} else if err != nil && !test.shouldErr {
|
|
|
|
t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
|
2016-08-15 08:44:34 -05:00
|
|
|
} else if err != nil && test.shouldErr {
|
2016-08-12 08:47:00 -05:00
|
|
|
continue
|
2016-06-04 23:48:27 -05:00
|
|
|
}
|
2016-10-17 20:16:26 -05:00
|
|
|
if !reflect.DeepEqual(actualErrorsRule, &test.expectedErrorHandler) {
|
|
|
|
t.Errorf("Test %d expect %v, but got %v", i,
|
|
|
|
actualErrorsRule, test.expectedErrorHandler)
|
2016-08-11 07:51:15 -05:00
|
|
|
}
|
2016-06-04 23:48:27 -05:00
|
|
|
}
|
|
|
|
}
|