2017-09-23 13:56:58 +08:00
|
|
|
// Copyright 2015 Light Code Labs, LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-06-05 12:48:27 +08:00
|
|
|
package errors
|
|
|
|
|
|
|
|
import (
|
2016-10-18 09:16:26 +08:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2016-06-05 12:48:27 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetup(t *testing.T) {
|
2016-06-21 01:44:20 +08:00
|
|
|
c := caddy.NewTestController("http", `errors`)
|
|
|
|
err := setup(c)
|
2016-06-05 12:48:27 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no errors, got: %v", err)
|
|
|
|
}
|
2016-06-21 01:44:20 +08:00
|
|
|
mids := httpserver.GetConfig(c).Middleware()
|
2016-06-05 12:48:27 +08: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)
|
|
|
|
}
|
|
|
|
|
2017-02-08 23:02:09 +08:00
|
|
|
expectedLogger := &httpserver.Logger{}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expectedLogger, myHandler.Log) {
|
|
|
|
t.Errorf("Expected '%v' as the default Log, got: '%v'", expectedLogger, myHandler.Log)
|
2016-06-05 12:48:27 +08:00
|
|
|
}
|
2017-02-08 23:02:09 +08:00
|
|
|
|
2016-06-05 12:48:27 +08:00
|
|
|
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-18 09:16:26 +08:00
|
|
|
testAbs, err := filepath.Abs("./404.html")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-06-05 12:48:27 +08:00
|
|
|
tests := []struct {
|
|
|
|
inputErrorsRules string
|
|
|
|
shouldErr bool
|
|
|
|
expectedErrorHandler ErrorHandler
|
|
|
|
}{
|
|
|
|
{`errors`, false, ErrorHandler{
|
2016-10-18 09:16:26 +08:00
|
|
|
ErrorPages: map[int]string{},
|
2017-02-08 23:02:09 +08:00
|
|
|
Log: &httpserver.Logger{},
|
2016-06-05 12:48:27 +08:00
|
|
|
}},
|
|
|
|
{`errors errors.txt`, false, ErrorHandler{
|
2016-10-18 09:16:26 +08:00
|
|
|
ErrorPages: map[int]string{},
|
2017-02-09 00:23:33 +08:00
|
|
|
Log: &httpserver.Logger{
|
|
|
|
Output: "errors.txt",
|
|
|
|
Roller: httpserver.DefaultLogRoller(),
|
|
|
|
},
|
2016-06-05 12:48:27 +08:00
|
|
|
}},
|
|
|
|
{`errors visible`, false, ErrorHandler{
|
2016-10-18 09:16:26 +08:00
|
|
|
ErrorPages: map[int]string{},
|
|
|
|
Debug: true,
|
2017-02-08 23:02:09 +08:00
|
|
|
Log: &httpserver.Logger{},
|
2016-06-05 12:48:27 +08:00
|
|
|
}},
|
2017-02-09 00:23:33 +08:00
|
|
|
{`errors errors.txt {
|
|
|
|
404 404.html
|
|
|
|
500 500.html
|
|
|
|
}`, false, ErrorHandler{
|
2016-06-05 12:48:27 +08:00
|
|
|
ErrorPages: map[int]string{
|
|
|
|
404: "404.html",
|
|
|
|
500: "500.html",
|
|
|
|
},
|
2017-02-09 00:23:33 +08:00
|
|
|
Log: &httpserver.Logger{
|
|
|
|
Output: "errors.txt",
|
|
|
|
Roller: httpserver.DefaultLogRoller(),
|
|
|
|
},
|
2016-06-05 12:48:27 +08:00
|
|
|
}},
|
2017-07-20 15:16:43 +08:00
|
|
|
{`errors errors.txt {
|
|
|
|
rotate_size 2
|
|
|
|
rotate_age 10
|
|
|
|
rotate_keep 3
|
|
|
|
rotate_compress
|
|
|
|
}`, false, ErrorHandler{
|
2017-02-08 23:02:09 +08:00
|
|
|
ErrorPages: map[int]string{},
|
2017-02-09 00:23:33 +08:00
|
|
|
Log: &httpserver.Logger{
|
|
|
|
Output: "errors.txt", Roller: &httpserver.LogRoller{
|
|
|
|
MaxSize: 2,
|
|
|
|
MaxAge: 10,
|
|
|
|
MaxBackups: 3,
|
2017-06-28 23:06:32 +08:00
|
|
|
Compress: true,
|
2017-02-09 00:23:33 +08:00
|
|
|
LocalTime: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
{`errors errors.txt {
|
|
|
|
rotate_size 3
|
|
|
|
rotate_age 11
|
|
|
|
rotate_keep 5
|
|
|
|
404 404.html
|
|
|
|
503 503.html
|
|
|
|
}`, false, ErrorHandler{
|
2016-06-05 12:48:27 +08:00
|
|
|
ErrorPages: map[int]string{
|
|
|
|
404: "404.html",
|
|
|
|
503: "503.html",
|
|
|
|
},
|
2017-02-09 00:23:33 +08:00
|
|
|
Log: &httpserver.Logger{
|
|
|
|
Output: "errors.txt",
|
|
|
|
Roller: &httpserver.LogRoller{
|
|
|
|
MaxSize: 3,
|
|
|
|
MaxAge: 11,
|
|
|
|
MaxBackups: 5,
|
2017-06-28 23:06:32 +08:00
|
|
|
Compress: false,
|
2017-02-09 00:23:33 +08:00
|
|
|
LocalTime: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
{`errors errors.txt {
|
|
|
|
* generic_error.html
|
|
|
|
404 404.html
|
|
|
|
503 503.html
|
|
|
|
}`, false, ErrorHandler{
|
|
|
|
Log: &httpserver.Logger{
|
|
|
|
Output: "errors.txt",
|
|
|
|
Roller: httpserver.DefaultLogRoller(),
|
2016-06-05 12:48:27 +08:00
|
|
|
},
|
2016-08-11 20:51:15 +08:00
|
|
|
GenericErrorPage: "generic_error.html",
|
|
|
|
ErrorPages: map[int]string{
|
|
|
|
404: "404.html",
|
|
|
|
503: "503.html",
|
|
|
|
},
|
|
|
|
}},
|
2016-10-18 09:16:26 +08:00
|
|
|
// test absolute file path
|
|
|
|
{`errors {
|
|
|
|
404 ` + testAbs + `
|
|
|
|
}`,
|
|
|
|
false, ErrorHandler{
|
|
|
|
ErrorPages: map[int]string{
|
|
|
|
404: testAbs,
|
|
|
|
},
|
2017-02-08 23:02:09 +08:00
|
|
|
Log: &httpserver.Logger{},
|
2016-10-18 09:16:26 +08:00
|
|
|
}},
|
2017-07-20 15:16:43 +08:00
|
|
|
{`errors errors.txt { rotate_size 2 rotate_age 10 rotate_keep 3 rotate_compress }`,
|
|
|
|
true, ErrorHandler{ErrorPages: map[int]string{}, Log: &httpserver.Logger{}}},
|
|
|
|
{`errors errors.txt {
|
|
|
|
rotate_compress invalid
|
|
|
|
}`,
|
|
|
|
true, ErrorHandler{ErrorPages: map[int]string{}, Log: &httpserver.Logger{}}},
|
2016-08-12 21:47:00 +08:00
|
|
|
// Next two test cases is the detection of duplicate status codes
|
|
|
|
{`errors {
|
2017-02-08 23:02:09 +08:00
|
|
|
503 503.html
|
|
|
|
503 503.html
|
|
|
|
}`, true, ErrorHandler{ErrorPages: map[int]string{}, Log: &httpserver.Logger{}}},
|
|
|
|
|
2016-08-12 21:47:00 +08:00
|
|
|
{`errors {
|
2017-02-08 23:02:09 +08:00
|
|
|
* generic_error.html
|
|
|
|
* generic_error.html
|
|
|
|
}`, true, ErrorHandler{ErrorPages: map[int]string{}, Log: &httpserver.Logger{}}},
|
2019-03-05 03:00:28 +08:00
|
|
|
{`errors /path error.txt {
|
|
|
|
404
|
|
|
|
}`, true, ErrorHandler{ErrorPages: map[int]string{}, Log: &httpserver.Logger{}}},
|
|
|
|
|
|
|
|
{`errors /path error.txt`, true, ErrorHandler{ErrorPages: map[int]string{}, Log: &httpserver.Logger{}}},
|
2016-06-05 12:48:27 +08:00
|
|
|
}
|
2017-02-08 23:02:09 +08:00
|
|
|
|
2016-06-05 12:48:27 +08:00
|
|
|
for i, test := range tests {
|
2016-06-21 01:44:20 +08:00
|
|
|
actualErrorsRule, err := errorsParse(caddy.NewTestController("http", test.inputErrorsRules))
|
2016-06-05 12:48:27 +08: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 21:44:34 +08:00
|
|
|
} else if err != nil && test.shouldErr {
|
2016-08-12 21:47:00 +08:00
|
|
|
continue
|
2016-06-05 12:48:27 +08:00
|
|
|
}
|
2016-10-18 09:16:26 +08:00
|
|
|
if !reflect.DeepEqual(actualErrorsRule, &test.expectedErrorHandler) {
|
|
|
|
t.Errorf("Test %d expect %v, but got %v", i,
|
2017-02-09 00:23:33 +08:00
|
|
|
test.expectedErrorHandler, actualErrorsRule)
|
2016-08-11 20:51:15 +08:00
|
|
|
}
|
2016-06-05 12:48:27 +08:00
|
|
|
}
|
|
|
|
}
|