package pi import ( "strings" "testing" ) // The key is redacted wherever it falls across the pieces of a stream, // and nothing else is held back for good. func TestRedactorStream(t *testing.T) { key := "tx-abcdefghijklmnop" text := "before " + key + " middle " + key + key + " after tx-a" for cut := 1; cut < 12; cut++ { r := NewRedactor(key) var out strings.Builder for i := 0; i < len(text); i += cut { out.WriteString(r.Stream(text[i:min(i+cut, len(text))])) } out.WriteString(r.Flush()) if strings.Contains(out.String(), key) || out.String() != strings.ReplaceAll(text, key, Redacted) { t.Fatalf("pieces of %d: %q", cut, out.String()) } } quoted := NewRedactor(`tx-"q"`) if got := quoted.Redact(`{"summary":"tx-\"q\" here"}`); strings.Contains(got, `tx-\"q\"`) { t.Fatalf("the JSON form: %s", got) } }