-
Notifications
You must be signed in to change notification settings - Fork 5
/
gigtest.go
76 lines (57 loc) · 1.93 KB
/
gigtest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gig
import (
"crypto/tls"
"errors"
"net"
"net/url"
"time"
)
type (
// FakeAddr ia a fake net.Addr implementation.
FakeAddr struct{}
// FakeConn ia a fake net.Conn that can record what is written and can fail
// after FailAfter bytes were written.
FakeConn struct {
FailAfter int
Written string
}
)
// Network returns dummy data.
func (a *FakeAddr) Network() string { return "tcp" }
// String returns dummy data.
func (a *FakeAddr) String() string { return "192.0.2.1:25" }
// Read always returns success.
func (c *FakeConn) Read(b []byte) (n int, err error) { return len(b), nil }
// Write records bytes written and fails after FailAfter bytes.
func (c *FakeConn) Write(b []byte) (n int, err error) {
if c.FailAfter > 0 && len(c.Written)+len(b) > c.FailAfter {
cut := c.FailAfter - len(c.Written)
c.Written += string(b[:cut])
return cut, errors.New("cannot write")
}
c.Written += string(b)
return len(b), nil
}
// Close always returns nil.
func (c *FakeConn) Close() error { return nil }
// LocalAddr returns fake address.
func (c *FakeConn) LocalAddr() net.Addr { return &FakeAddr{} }
// RemoteAddr returns fake address.
func (c *FakeConn) RemoteAddr() net.Addr { return &FakeAddr{} }
// SetDeadline always returns nil.
func (c *FakeConn) SetDeadline(t time.Time) error { return nil }
// SetReadDeadline always returns nil.
func (c *FakeConn) SetReadDeadline(t time.Time) error { return nil }
// SetWriteDeadline always returns nil.
func (c *FakeConn) SetWriteDeadline(t time.Time) error { return nil }
// ConnectionState always returns nil.
func (c *FakeConn) ConnectionState() tls.ConnectionState { return tls.ConnectionState{} }
// NewFakeContext returns Context that writes to FakeConn.
func (g *Gig) NewFakeContext(uri string, tlsState *tls.ConnectionState) (Context, *FakeConn) {
u, err := url.Parse(uri)
if err != nil {
panic(err)
}
conn := &FakeConn{}
return g.newContext(conn, u, uri, tlsState), conn
}