testing

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2025 License: Apache-2.0, Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package testing provides common test utilities for transport packages.

Index

Constants

This section is empty.

Variables

View Source
var ErrFlakyFailure = errors.New("simulated read failure")

ErrFlakyFailure is returned when FlakyReader simulates a failure.

Functions

func AssertDataEquals

func AssertDataEquals(t *testing.T, got, want []byte)

AssertDataEquals checks if two byte slices are equal.

func AssertError

func AssertError(t *testing.T, err error, msg string)

AssertError fails the test if err is nil.

func AssertHeaderAbsent

func AssertHeaderAbsent(t *testing.T, headers map[string][]string, key string)

AssertHeaderAbsent checks if a header is absent.

func AssertHeaderEquals

func AssertHeaderEquals(t *testing.T, headers map[string][]string, key, want string)

AssertHeaderEquals checks if a header has the expected value.

func AssertHeaderPresent

func AssertHeaderPresent(t *testing.T, headers map[string][]string, key string)

AssertHeaderPresent checks if a header is present.

func AssertNoError

func AssertNoError(t *testing.T, err error, msg string)

AssertNoError fails the test if err is not nil.

func ChunkData

func ChunkData(data []byte, n int) [][]byte

ChunkData splits data into n chunks of approximately equal size.

func ConcatChunks

func ConcatChunks(chunks [][]byte) []byte

ConcatChunks concatenates multiple byte slices into one.

func GenerateRandomData

func GenerateRandomData(size int) []byte

GenerateRandomData generates random test data of the specified size.

func GenerateTestData

func GenerateTestData(size int) []byte

GenerateTestData generates deterministic test data of the specified size.

func MustRead

func MustRead(t *testing.T, r io.Reader, n int) []byte

MustRead reads exactly n bytes from a reader or fails the test.

func ReadAll

func ReadAll(t *testing.T, r io.Reader) []byte

ReadAll reads all data from a reader and returns it.

func ReadAllWithError

func ReadAllWithError(r io.Reader) ([]byte, error)

ReadAllWithError reads all data from a reader and returns both data and error.

Types

type ByteRange

type ByteRange struct {
	// Start is the starting byte position (inclusive).
	Start int64
	// End is the ending byte position (inclusive).
	End int64
}

ByteRange represents a byte range.

func CalculateByteRanges

func CalculateByteRanges(totalSize int64, n int) []ByteRange

CalculateByteRanges calculates byte ranges for splitting a file of given size into n parts.

type FakeResource

type FakeResource struct {
	// Data provides random access to the resource content.
	Data io.ReaderAt
	// Length is the total number of bytes in the resource content.
	Length int64
	// SupportsRange indicates if this resource supports byte ranges.
	SupportsRange bool
	// ETag is the ETag header value (optional).
	ETag string
	// LastModified is the Last-Modified header value (optional).
	LastModified string
	// ContentType is the Content-Type header value (optional).
	ContentType string
	// Headers are additional headers to include in responses.
	Headers http.Header
}

FakeResource represents a resource that can be served by FakeTransport.

type FakeTransport

type FakeTransport struct {

	// RequestHook is called for each request if set.
	RequestHook func(*http.Request)
	// ResponseHook is called for each response if set.
	ResponseHook func(*http.Response)
	// contains filtered or unexported fields
}

FakeTransport is a test http.RoundTripper that serves fake resources.

func NewFakeTransport

func NewFakeTransport() *FakeTransport

NewFakeTransport creates a new FakeTransport.

func (*FakeTransport) Add

func (ft *FakeTransport) Add(url string, resource *FakeResource)

Add adds a resource to the fake transport.

func (*FakeTransport) AddSimple

func (ft *FakeTransport) AddSimple(url string, data io.ReaderAt, length int64, supportsRange bool)

AddSimple adds a simple resource with the provided reader and length.

func (*FakeTransport) GetRequestHeaders

func (ft *FakeTransport) GetRequestHeaders(url string) []http.Header

GetRequestHeaders returns the headers from all requests for a given URL.

func (*FakeTransport) GetRequests

func (ft *FakeTransport) GetRequests() []http.Request

GetRequests returns a copy of all requests made to this transport.

func (*FakeTransport) RoundTrip

func (ft *FakeTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

func (*FakeTransport) SetFailAfter

func (ft *FakeTransport) SetFailAfter(url string, n int)

SetFailAfter configures the transport to fail after serving n bytes for the given URL.

type FlakyReader

type FlakyReader struct {
	// contains filtered or unexported fields
}

FlakyReader simulates a reader that fails after a certain number of bytes.

func NewFlakyReader

func NewFlakyReader(data io.ReaderAt, length int64, failAfter int) *FlakyReader

NewFlakyReader creates a FlakyReader that fails after reading failAfter bytes. If failAfter is 0 or negative, it never fails.

func (*FlakyReader) Close

func (fr *FlakyReader) Close() error

Close implements io.Closer.

func (*FlakyReader) HasFailed

func (fr *FlakyReader) HasFailed() bool

HasFailed returns true if the reader has simulated a failure.

func (*FlakyReader) Position

func (fr *FlakyReader) Position() int

Position returns the current read position.

func (*FlakyReader) Read

func (fr *FlakyReader) Read(p []byte) (int, error)

Read implements io.Reader.

func (*FlakyReader) Reset

func (fr *FlakyReader) Reset()

Reset resets the reader to start from the beginning.

type MultiFailReader

type MultiFailReader struct {
	// contains filtered or unexported fields
}

MultiFailReader simulates multiple failures at different points.

func NewMultiFailReader

func NewMultiFailReader(data io.ReaderAt, length int64, failurePoints []int) *MultiFailReader

NewMultiFailReader creates a reader that fails at specified byte positions.

func (*MultiFailReader) Close

func (mfr *MultiFailReader) Close() error

Close implements io.Closer.

func (*MultiFailReader) Read

func (mfr *MultiFailReader) Read(p []byte) (int, error)

Read implements io.Reader.

func (*MultiFailReader) Reset

func (mfr *MultiFailReader) Reset()

Reset resets the reader to the beginning and clears failure state.

Jump to

Keyboard shortcuts

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