netstub

package module
v0.0.0-...-e5f4b4b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2026 License: GPL-3.0 Imports: 4 Imported by: 1

README

Golang Helpers for Network Testing

GoDoc Build Status codecov

The netstub Go package contains small helpers for testing networking code.

For example:

import (
	"context"
	"errors"
	"net"

	"github.com/bassosimone/netstub"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// Create a Dialer that always fails with a mocked error.
dialer := &netstub.FuncDialer{
	DialContextFunc: func(ctx context.Context, network, address string) (net.Conn, error) {
		return nil, errors.New("mocked error")
	},
}

// Use the dialer in code under test.
conn, err := dialer.DialContext(context.Background(), "tcp", "example.com:80")

// Verify the test.
require.Error(t, err)
assert.Nil(t, conn)

Installation

To add this package as a dependency to your module:

go get github.com/bassosimone/netstub

Development

To run the tests:

go test -v .

To measure test coverage:

go test -v -cover .

License

SPDX-License-Identifier: GPL-3.0-or-later

History

Adapted from ooni/probe-cli.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FuncAddr

type FuncAddr struct {
	NetworkFunc func() string
	StringFunc  func() string
}

FuncAddr allows to mock any net.Addr.

func (*FuncAddr) Network

func (fa *FuncAddr) Network() string

Network implements net.Addr.

func (*FuncAddr) String

func (fa *FuncAddr) String() string

String implements net.Addr.

type FuncConn

type FuncConn struct {
	ReadFunc        func([]byte) (int, error)
	WriteFunc       func([]byte) (int, error)
	CloseFunc       func() error
	LocalAddrFunc   func() net.Addr
	RemoteAddrFunc  func() net.Addr
	SetDeadlineFunc func(time.Time) error
	SetReadDeadFunc func(time.Time) error
	SetWriteDeaFunc func(time.Time) error
}

FuncConn allows to mock any net.Conn.

func (*FuncConn) Close

func (fc *FuncConn) Close() error

Close implements net.Conn.

func (*FuncConn) LocalAddr

func (fc *FuncConn) LocalAddr() net.Addr

LocalAddr implements net.Conn.

func (*FuncConn) Read

func (fc *FuncConn) Read(b []byte) (int, error)

Read implements net.Conn.

func (*FuncConn) RemoteAddr

func (fc *FuncConn) RemoteAddr() net.Addr

RemoteAddr implements net.Conn.

func (*FuncConn) SetDeadline

func (fc *FuncConn) SetDeadline(t time.Time) error

SetDeadline implements net.Conn.

func (*FuncConn) SetReadDeadline

func (fc *FuncConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements net.Conn.

func (*FuncConn) SetWriteDeadline

func (fc *FuncConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements net.Conn.

func (*FuncConn) Write

func (fc *FuncConn) Write(b []byte) (int, error)

Write implements net.Conn.

type FuncDialer

type FuncDialer struct {
	DialContextFunc func(ctx context.Context, network, address string) (net.Conn, error)
}

FuncDialer allows to mock any dialer with a DialContext method.

func (*FuncDialer) DialContext

func (fd *FuncDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error)

DialContext implements a dialer with a DialContext method.

type FuncListener

type FuncListener struct {
	AcceptFunc func() (net.Conn, error)
	CloseFunc  func() error
	AddrFunc   func() net.Addr
}

FuncListener allows to mock any net.Listener.

func (*FuncListener) Accept

func (fl *FuncListener) Accept() (net.Conn, error)

Accept implements net.Listener.

func (*FuncListener) Addr

func (fl *FuncListener) Addr() net.Addr

Addr implements net.Listener.

func (*FuncListener) Close

func (fl *FuncListener) Close() error

Close implements net.Listener.

type FuncResolver

type FuncResolver struct {
	LookupHostFunc func(ctx context.Context, name string) ([]string, error)
}

FuncResolver allows to mock any resolver with a LookupHost method.

func (*FuncResolver) LookupHost

func (fr *FuncResolver) LookupHost(ctx context.Context, name string) ([]string, error)

LookupHost implements a resolver with a LookupHost method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL