|
1 | 1 | package proxy
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "io"
|
5 | 6 | "sync"
|
6 | 7 | "testing"
|
@@ -61,3 +62,34 @@ func TestLogMonitor(t *testing.T) {
|
61 | 62 | t.Errorf("Client2 expected %s, got: %s", expectedHistory, c2Data)
|
62 | 63 | }
|
63 | 64 | }
|
| 65 | + |
| 66 | +func TestWrite_ImmutableBuffer(t *testing.T) { |
| 67 | + // Create a new LogMonitor instance |
| 68 | + lm := NewLogMonitorWriter(io.Discard) |
| 69 | + |
| 70 | + // Prepare a message to write |
| 71 | + msg := []byte("Hello, World!") |
| 72 | + lenmsg := len(msg) |
| 73 | + |
| 74 | + // Write the message to the LogMonitor |
| 75 | + n, err := lm.Write(msg) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("Write failed: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + if n != lenmsg { |
| 81 | + t.Errorf("Expected %d bytes written but got %d", lenmsg, n) |
| 82 | + } |
| 83 | + |
| 84 | + // Change the original message |
| 85 | + msg[0] = 'B' // This should not affect the buffer |
| 86 | + |
| 87 | + // Get the history from the LogMonitor |
| 88 | + history := lm.GetHistory() |
| 89 | + |
| 90 | + // Check that the history contains the original message, not the modified one |
| 91 | + expected := []byte("Hello, World!") |
| 92 | + if !bytes.Equal(history, expected) { |
| 93 | + t.Errorf("Expected history to be %q, got %q", expected, history) |
| 94 | + } |
| 95 | +} |
0 commit comments