Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions tests/logrus_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"strings"
"sync"
"testing"

"github.com/sirupsen/logrus"
)
Expand All @@ -30,6 +31,32 @@ func (lc *logCache) Fire(e *logrus.Entry) error {
return nil
}

// assertContains checks if msg is contained in any of the cached logged events
// and fails the test if it's not. It also prints the cached log messages to
// help debugging.
func (lc *logCache) assertContains(tb testing.TB, msg string) {
tb.Helper()

if lc.contains(msg) {
return
}
tb.Errorf("expected log cache to contain %q, but it didn't.", msg)
lc.dump(tb)
}

// dump prints all the cached log messages to the testing.TB.
func (lc *logCache) dump(tb testing.TB) {
tb.Helper()

lc.mu.RLock()
defer lc.mu.RUnlock()

tb.Log(strings.Repeat("-", 80))
for _, e := range lc.entries {
tb.Log(e.Message)
}
}

// contains returns true if msg is contained in any of the cached logged events
// or false otherwise.
func (lc *logCache) contains(msg string) bool {
Expand Down
9 changes: 4 additions & 5 deletions tests/network_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ func TestURLSkipRequest(t *testing.T) {

_, err := p.Goto("data:text/html,hello", nil)
require.NoError(t, err)
assert.True(t, tb.logCache.contains("skipping request handling of data URL"))
tb.logCache.assertContains(t, "skipping request handling of data URL")

_, err = p.Goto("blob:something", nil)
require.NoError(t, err)
assert.True(t, tb.logCache.contains("skipping request handling of blob URL"))
tb.logCache.assertContains(t, "skipping request handling of blob URL")
}

func TestBlockHostnames(t *testing.T) {
Expand All @@ -42,7 +42,7 @@ func TestBlockHostnames(t *testing.T) {
res, err := p.Goto("http://host.test/", nil)
require.NoError(t, err)
require.Nil(t, res)
require.True(t, tb.logCache.contains("was interrupted: hostname host.test is in a blocked pattern"))
tb.logCache.assertContains(t, "was interrupted: hostname host.test is in a blocked pattern")

res, err = p.Goto(tb.URL("/get"), nil)
require.NoError(t, err)
Expand All @@ -60,8 +60,7 @@ func TestBlockIPs(t *testing.T) {
res, err := p.Goto("http://10.0.0.1:8000/", nil)
require.NoError(t, err)
require.Nil(t, res)
assert.True(t, tb.logCache.contains(
`was interrupted: IP 10.0.0.1 is in a blacklisted range "10.0.0.0/8"`))
tb.logCache.assertContains(t, `was interrupted: IP 10.0.0.1 is in a blacklisted range "10.0.0.0/8"`)

// Ensure other requests go through
res, err = p.Goto(tb.URL("/get"), nil)
Expand Down