Download this file
| 1 | package pi |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // The key is redacted wherever it falls across the pieces of a stream, |
| 9 | // and nothing else is held back for good. |
| 10 | func TestRedactorStream(t *testing.T) { |
| 11 | key := "tx-abcdefghijklmnop" |
| 12 | text := "before " + key + " middle " + key + key + " after tx-a" |
| 13 | for cut := 1; cut < 12; cut++ { |
| 14 | r := NewRedactor(key) |
| 15 | var out strings.Builder |
| 16 | for i := 0; i < len(text); i += cut { |
| 17 | out.WriteString(r.Stream(text[i:min(i+cut, len(text))])) |
| 18 | } |
| 19 | out.WriteString(r.Flush()) |
| 20 | if strings.Contains(out.String(), key) || out.String() != strings.ReplaceAll(text, key, Redacted) { |
| 21 | t.Fatalf("pieces of %d: %q", cut, out.String()) |
| 22 | } |
| 23 | } |
| 24 | quoted := NewRedactor(`tx-"q"`) |
| 25 | if got := quoted.Redact(`{"summary":"tx-\"q\" here"}`); strings.Contains(got, `tx-\"q\"`) { |
| 26 | t.Fatalf("the JSON form: %s", got) |
| 27 | } |
| 28 | } |
| 29 | |