diff --git a/proxy/proxy_test.go b/proxy/proxy_test.go index 096423ee81..63d11a0638 100644 --- a/proxy/proxy_test.go +++ b/proxy/proxy_test.go @@ -2069,14 +2069,12 @@ func TestEnableAccessLogWithFilter(t *testing.T) { } func TestAccessLogOnFailedRequest(t *testing.T) { - buf := NewLockedBuffer() - logging.Init(logging.Options{ - AccessLogOutput: buf}) + testLog := NewTestLog() + defer testLog.Close() - s := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) - s.Close() + logging.Init(logging.Options{AccessLogOutput: testLog}) - p, err := newTestProxy(fmt.Sprintf(`* -> "%s"`, s.URL), 0) + p, err := newTestProxy(`* -> "http://bad-gateway.test"`, FlagsNone) if err != nil { t.Fatalf("Failed to create test proxy: %v", err) return @@ -2086,31 +2084,21 @@ func TestAccessLogOnFailedRequest(t *testing.T) { ps := httptest.NewServer(p.proxy) defer ps.Close() - rsp, err := http.Get(ps.URL) + rsp, err := ps.Client().Get(ps.URL) if err != nil { t.Fatalf("Failed to GET: %v", err) return } - defer rsp.Body.Close() if rsp.StatusCode != http.StatusBadGateway { t.Errorf("failed to return 502 Bad Gateway on failing backend connection: %d", rsp.StatusCode) } - time.Sleep(time.Millisecond) - output := buf.String() - - proxyURL, err := url.Parse(ps.URL) - if err != nil { - t.Fatalf("Failed to parse url: %v", err) - return - } - - expected := fmt.Sprintf(`"GET / HTTP/1.1" %d %d "-" "Go-http-client/1.1"`, http.StatusBadGateway, len(http.StatusText(http.StatusBadGateway))+1) - if !strings.Contains(output, expected) || !strings.Contains(output, proxyURL.Host) { - t.Errorf("Failed to get accesslog '%v' '%v'", output, expected) - t.Logf("%s", cmp.Diff(output, expected)) + const expected = `"GET / HTTP/1.1" 502 12 "-" "Go-http-client/1.1"` + if err = testLog.WaitFor(expected, 100*time.Millisecond); err != nil { + t.Errorf("Failed to get accesslog %v: %v", expected, err) + t.Logf("%s", cmp.Diff(testLog.String(), expected)) } } diff --git a/proxy/utils.go b/proxy/utils.go deleted file mode 100644 index 7cbc8039dc..0000000000 --- a/proxy/utils.go +++ /dev/null @@ -1,29 +0,0 @@ -package proxy - -import ( - "bytes" - "sync" -) - -type LockedBuffer struct { - mu sync.Mutex - buf *bytes.Buffer -} - -func NewLockedBuffer() *LockedBuffer { - return &LockedBuffer{ - buf: &bytes.Buffer{}, - } -} - -func (b *LockedBuffer) Write(p []byte) (int, error) { - b.mu.Lock() - defer b.mu.Unlock() - return b.buf.Write(p) -} - -func (b *LockedBuffer) String() string { - b.mu.Lock() - defer b.mu.Unlock() - return b.buf.String() -}