hopp/internal/testutil/conn-recorder.go

46 lines
940 B
Go

package testutil
import "net"
import "fmt"
import "strings"
var _ net.Conn = new(ConnRecorder)
// ConnRecorder records write/flush actions performed on a net.Conn.
type ConnRecorder struct {
net.Conn
// A []byte means data was written, and untyped nil
// means data was flushed.
Log []any
}
func RecordConn(underlying net.Conn) *ConnRecorder {
return &ConnRecorder {
Conn: underlying,
}
}
func (this *ConnRecorder) Write(data []byte) (n int, err error) {
this.Log = append(this.Log, data)
return len(data), nil
}
func (this *ConnRecorder) Flush() error {
this.Log = append(this.Log, nil)
return nil
}
func (this *ConnRecorder) Dump() string {
builder := strings.Builder { }
for index, item := range this.Log {
fmt.Fprintf(&builder, "%06d ", index)
switch item := item.(type) {
case nil:
fmt.Fprintln(&builder, "FLUSH")
case []byte:
fmt.Fprintln(&builder, HexBytes(item))
}
}
return builder.String()
}