allow overriding the Logger used by Proxy

This commit is contained in:
Harrison Healey 2019-04-22 19:49:45 -04:00 committed by Will Norris
parent e1558d5626
commit d4246a08fd
2 changed files with 73 additions and 6 deletions

View file

@ -21,9 +21,11 @@ import (
"fmt"
"image"
"image/png"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"reflect"
"strings"
"testing"
@ -412,6 +414,52 @@ func TestProxy_ServeHTTP_is304(t *testing.T) {
}
}
func TestProxy_log(t *testing.T) {
var b strings.Builder
p := &Proxy{
Logger: log.New(&b, "", 0),
}
p.log("Test")
if got, want := b.String(), "Test\n"; got != want {
t.Errorf("log wrote %s, want %s", got, want)
}
b.Reset()
p.logf("Test %v", 123)
if got, want := b.String(), "Test 123\n"; got != want {
t.Errorf("logf wrote %s, want %s", got, want)
}
}
func TestProxy_log_default(t *testing.T) {
var b strings.Builder
defer func(flags int) {
log.SetOutput(os.Stderr)
log.SetFlags(flags)
}(log.Flags())
log.SetOutput(&b)
log.SetFlags(0)
p := &Proxy{}
p.log("Test")
if got, want := b.String(), "Test\n"; got != want {
t.Errorf("log wrote %s, want %s", got, want)
}
b.Reset()
p.logf("Test %v", 123)
if got, want := b.String(), "Test 123\n"; got != want {
t.Errorf("logf wrote %s, want %s", got, want)
}
}
func TestTransformingTransport(t *testing.T) {
client := new(http.Client)
tr := &TransformingTransport{