caddy/caddyhttp/log/log_test.go

262 lines
6.1 KiB
Go
Raw Normal View History

// 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.
package log
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
Rewrote Caddy from the ground up; initial commit of 0.9 branch These changes span work from the last ~4 months in an effort to make Caddy more extensible, reduce the coupling between its components, and lay a more robust foundation of code going forward into 1.0. A bunch of new features have been added, too, with even higher future potential. The most significant design change is an overall inversion of dependencies. Instead of the caddy package knowing about the server and the notion of middleware and config, the caddy package exposes an interface that other components plug into. This does introduce more indirection when reading the code, but every piece is very modular and pluggable. Even the HTTP server is pluggable. The caddy package has been moved to the top level, and main has been pushed into a subfolder called caddy. The actual logic of the main file has been pushed even further into caddy/caddymain/run.go so that custom builds of Caddy can be 'go get'able. The HTTPS logic was surgically separated into two parts to divide the TLS-specific code and the HTTPS-specific code. The caddytls package can now be used by any type of server that needs TLS, not just HTTP. I also added the ability to customize nearly every aspect of TLS at the site level rather than all sites sharing the same TLS configuration. Not all of this flexibility is exposed in the Caddyfile yet, but it may be in the future. Caddy can also generate self-signed certificates in memory for the convenience of a developer working on localhost who wants HTTPS. And Caddy now supports the DNS challenge, assuming at least one DNS provider is plugged in. Dozens, if not hundreds, of other minor changes swept through the code base as I literally started from an empty main function, copying over functions or files as needed, then adjusting them to fit in the new design. Most tests have been restored and adapted to the new API, but more work is needed there. A lot of what was "impossible" before is now possible, or can be made possible with minimal disruption of the code. For example, it's fairly easy to make plugins hook into another part of the code via callbacks. Plugins can do more than just be directives; we now have plugins that customize how the Caddyfile is loaded (useful when you need to get your configuration from a remote store). Site addresses no longer need be just a host and port. They can have a path, allowing you to scope a configuration to a specific path. There is no inheretance, however; each site configuration is distinct. Thanks to amazing work by Lucas Clemente, this commit adds experimental QUIC support. Turn it on using the -quic flag; your browser may have to be configured to enable it. Almost everything is here, but you will notice that most of the middle- ware are missing. After those are transferred over, we'll be ready for beta tests. I'm very excited to get this out. Thanks for everyone's help and patience these last few months. I hope you like it!!
2016-06-05 07:00:29 +08:00
"github.com/mholt/caddy/caddyhttp/httpserver"
)
type erroringMiddleware struct{}
func (erroringMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
Rewrote Caddy from the ground up; initial commit of 0.9 branch These changes span work from the last ~4 months in an effort to make Caddy more extensible, reduce the coupling between its components, and lay a more robust foundation of code going forward into 1.0. A bunch of new features have been added, too, with even higher future potential. The most significant design change is an overall inversion of dependencies. Instead of the caddy package knowing about the server and the notion of middleware and config, the caddy package exposes an interface that other components plug into. This does introduce more indirection when reading the code, but every piece is very modular and pluggable. Even the HTTP server is pluggable. The caddy package has been moved to the top level, and main has been pushed into a subfolder called caddy. The actual logic of the main file has been pushed even further into caddy/caddymain/run.go so that custom builds of Caddy can be 'go get'able. The HTTPS logic was surgically separated into two parts to divide the TLS-specific code and the HTTPS-specific code. The caddytls package can now be used by any type of server that needs TLS, not just HTTP. I also added the ability to customize nearly every aspect of TLS at the site level rather than all sites sharing the same TLS configuration. Not all of this flexibility is exposed in the Caddyfile yet, but it may be in the future. Caddy can also generate self-signed certificates in memory for the convenience of a developer working on localhost who wants HTTPS. And Caddy now supports the DNS challenge, assuming at least one DNS provider is plugged in. Dozens, if not hundreds, of other minor changes swept through the code base as I literally started from an empty main function, copying over functions or files as needed, then adjusting them to fit in the new design. Most tests have been restored and adapted to the new API, but more work is needed there. A lot of what was "impossible" before is now possible, or can be made possible with minimal disruption of the code. For example, it's fairly easy to make plugins hook into another part of the code via callbacks. Plugins can do more than just be directives; we now have plugins that customize how the Caddyfile is loaded (useful when you need to get your configuration from a remote store). Site addresses no longer need be just a host and port. They can have a path, allowing you to scope a configuration to a specific path. There is no inheretance, however; each site configuration is distinct. Thanks to amazing work by Lucas Clemente, this commit adds experimental QUIC support. Turn it on using the -quic flag; your browser may have to be configured to enable it. Almost everything is here, but you will notice that most of the middle- ware are missing. After those are transferred over, we'll be ready for beta tests. I'm very excited to get this out. Thanks for everyone's help and patience these last few months. I hope you like it!!
2016-06-05 07:00:29 +08:00
if rr, ok := w.(*httpserver.ResponseRecorder); ok {
rr.Replacer.Set("testval", "foobar")
}
return http.StatusNotFound, nil
}
func TestLoggedStatus(t *testing.T) {
var f bytes.Buffer
var next erroringMiddleware
rule := Rule{
PathScope: "/",
Entries: []*Entry{{
Format: DefaultLogFormat + " {testval}",
Log: httpserver.NewTestLogger(&f),
}},
}
logger := Logger{
Rules: []*Rule{&rule},
Next: next,
}
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
status, err := logger.ServeHTTP(rec, r)
if status != 0 {
t.Errorf("Expected status to be 0, but was %d", status)
}
if err != nil {
t.Errorf("Expected error to be nil, instead got: %v", err)
}
logged := f.String()
if !strings.Contains(logged, "404 13") {
t.Errorf("Expected log entry to contain '404 13', but it didn't: %s", logged)
}
// check custom placeholder
if !strings.Contains(logged, "foobar") {
t.Errorf("Expected the log entry to contain 'foobar' (custom placeholder), but it didn't: %s", logged)
}
}
func TestLogRequestBody(t *testing.T) {
var got bytes.Buffer
logger := Logger{
Rules: []*Rule{{
PathScope: "/",
Entries: []*Entry{{
Format: "{request_body}",
Log: httpserver.NewTestLogger(&got),
}},
}},
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
// drain up body
ioutil.ReadAll(r.Body)
return 0, nil
}),
}
for i, c := range []struct {
body string
expect string
}{
{"", "\n"},
{"{hello} world!", "{hello} world!\n"},
{func() string {
length := httpserver.MaxLogBodySize + 100
b := make([]byte, length)
for i := 0; i < length; i++ {
b[i] = 0xab
}
return string(b)
}(), func() string {
b := make([]byte, httpserver.MaxLogBodySize)
for i := 0; i < httpserver.MaxLogBodySize; i++ {
b[i] = 0xab
}
return string(b) + "\n"
}(),
},
} {
got.Reset()
r := httptest.NewRequest("POST", "/", bytes.NewBufferString(c.body))
r.Header.Set("Content-Type", "application/json")
status, err := logger.ServeHTTP(httptest.NewRecorder(), r)
if status != 0 {
t.Errorf("case %d: Expected status to be 0, but was %d", i, status)
}
if err != nil {
t.Errorf("case %d: Expected error to be nil, instead got: %v", i, err)
}
if got.String() != c.expect {
t.Errorf("case %d: Expected body %q, but got %q", i, c.expect, got.String())
}
}
}
func TestMultiEntries(t *testing.T) {
var (
got1 bytes.Buffer
got2 bytes.Buffer
)
logger := Logger{
Rules: []*Rule{{
PathScope: "/",
Entries: []*Entry{
{
Format: "foo {request_body}",
Log: httpserver.NewTestLogger(&got1),
},
{
Format: "{method} {request_body}",
Log: httpserver.NewTestLogger(&got2),
},
},
}},
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
// drain up body
ioutil.ReadAll(r.Body)
return 0, nil
}),
}
r, err := http.NewRequest("POST", "/", bytes.NewBufferString("hello world"))
if err != nil {
t.Fatal(err)
}
r.Header.Set("Content-Type", "application/json")
status, err := logger.ServeHTTP(httptest.NewRecorder(), r)
if status != 0 {
t.Errorf("Expected status to be 0, but was %d", status)
}
if err != nil {
t.Errorf("Expected error to be nil, instead got: %v", err)
}
if got, expect := got1.String(), "foo hello world\n"; got != expect {
t.Errorf("Expected %q, but got %q", expect, got)
}
if got, expect := got2.String(), "POST hello world\n"; got != expect {
t.Errorf("Expected %q, but got %q", expect, got)
}
}
func TestLogExcept(t *testing.T) {
tests := []struct {
LogRules []Rule
logPath string
shouldLog bool
}{
{[]Rule{{
PathScope: "/",
Entries: []*Entry{{
Log: &httpserver.Logger{
Exceptions: []string{"/soup"},
},
Format: DefaultLogFormat,
}},
}}, `/soup`, false},
{[]Rule{{
PathScope: "/",
Entries: []*Entry{{
Log: &httpserver.Logger{
Exceptions: []string{"/tart"},
},
Format: DefaultLogFormat,
}},
}}, `/soup`, true},
{[]Rule{{
PathScope: "/",
Entries: []*Entry{{
Log: &httpserver.Logger{
Exceptions: []string{"/soup"},
},
Format: DefaultLogFormat,
}},
}}, `/tomatosoup`, true},
{[]Rule{{
PathScope: "/",
Entries: []*Entry{{
Log: &httpserver.Logger{
Exceptions: []string{"/pie/"},
},
Format: DefaultLogFormat,
}},
// Check exception with a trailing slash does not match without
}}, `/pie`, true},
{[]Rule{{
PathScope: "/",
Entries: []*Entry{{
Log: &httpserver.Logger{
Exceptions: []string{"/pie.php"},
},
Format: DefaultLogFormat,
}},
}}, `/pie`, true},
{[]Rule{{
PathScope: "/",
Entries: []*Entry{{
Log: &httpserver.Logger{
Exceptions: []string{"/pie"},
},
Format: DefaultLogFormat,
}},
// Check that a word without trailing slash will match a filename
}}, `/pie.php`, false},
}
for i, test := range tests {
for _, LogRule := range test.LogRules {
for _, e := range LogRule.Entries {
shouldLog := e.Log.ShouldLog(test.logPath)
if shouldLog != test.shouldLog {
t.Fatalf("Test %d expected shouldLog=%t but got shouldLog=%t,", i, test.shouldLog, shouldLog)
}
}
}
}
}