Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gbbr/mocks
Tiny collection of mocks for faking a network connection in Go
https://github.com/gbbr/mocks
Last synced: 8 days ago
JSON representation
Tiny collection of mocks for faking a network connection in Go
- Host: GitHub
- URL: https://github.com/gbbr/mocks
- Owner: gbbr
- Created: 2014-10-04T18:29:23.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-12-02T18:48:28.000Z (almost 10 years ago)
- Last Synced: 2023-08-14T11:32:29.578Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 375 KB
- Stars: 44
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Mocks
Mocks is a small package that helps with testing network applications.
#### Mocking a network connection
To mock the `net.Conn` interface, import `github.com/gbbr/mocks` into your package
and use a configured Conn structure, such as:```go
var mockConn net.ConnmockConn = &mocks.Conn{
// Local address
LAddr: "127.0.0.1:888",
LNet: "tcp",// Remote address
RAddr: "10.18.20.21:123",
RNet: "udp",
}fmt.Println(mockConn.LocalAddr().String()) // prints "127.0.0.1:888"
fmt.Println(mockConn.RemoteAddr().String()) // prints "10.18.20.21:123"
```The view data that was sent to the mock connection, configure the `In` io.Writer
interface of mocks.Conn, like:```go
var buf bytes.Buffer
mockConn.In = &buf
fmt.Fprintf(mockConn, "Message")fmt.Println(buf.String()) // prints "Message"
```To set a data source for the network connection the `Out` io.Reader may be used as follows:
```go
mockConn.Out = bytes.NewBuffer([]byte("Test\n"))var msg string
fmt.Scanln(mockConn, &msg)fmt.Println(msg) // outputs "Test"
```#### Obtaining a full communication channel
Pipe returns a full duplex network connection that receives data on either end and outputs
it on the other one.```go
c1, c2 := Pipe(
&Conn{RAddr: "1.1.1.1:123"},
&Conn{LAddr: "127.0.0.1:12", RAddr: "2.2.2.2:456"},
)// Go routine writes to connection 1
go c1.Write([]byte("Hello"))// Read 5 bytes
b := make([]byte, 5)// Connection 2 receives message
n, err := c2.Read(b)
if err != nil {
t.Errorf("Could not read c2: %s", err)
}fmt.Println(string(b)) // outputs "Hello"
```Refer to the [tests](https://github.com/gbbr/mocks/blob/master/conn_test.go#L75) for a complete example.
### Considerations
If you do not wish to to create the above examples (ie. you do not need to fake the remote/local address), you may also consider using the [pipe](http://golang.org/pkg/net/#Pipe) provided in the `net` package, which returns two ends of a network stream. _Careful though_, when using net.Pipe() and requesting LocalAddr() or RemoteAddr() nil pointer panic will happen.