Documentation
¶
Overview ¶
Package testing provides common test utilities for transport packages.
Index ¶
- Variables
- func AssertDataEquals(t *testing.T, got, want []byte)
- func AssertError(t *testing.T, err error, msg string)
- func AssertHeaderAbsent(t *testing.T, headers map[string][]string, key string)
- func AssertHeaderEquals(t *testing.T, headers map[string][]string, key, want string)
- func AssertHeaderPresent(t *testing.T, headers map[string][]string, key string)
- func AssertNoError(t *testing.T, err error, msg string)
- func ChunkData(data []byte, n int) [][]byte
- func ConcatChunks(chunks [][]byte) []byte
- func GenerateRandomData(size int) []byte
- func GenerateTestData(size int) []byte
- func MustRead(t *testing.T, r io.Reader, n int) []byte
- func ReadAll(t *testing.T, r io.Reader) []byte
- func ReadAllWithError(r io.Reader) ([]byte, error)
- type ByteRange
- type FakeResource
- type FakeTransport
- func (ft *FakeTransport) Add(url string, resource *FakeResource)
- func (ft *FakeTransport) AddSimple(url string, data io.ReaderAt, length int64, supportsRange bool)
- func (ft *FakeTransport) GetRequestHeaders(url string) []http.Header
- func (ft *FakeTransport) GetRequests() []http.Request
- func (ft *FakeTransport) RoundTrip(req *http.Request) (*http.Response, error)
- func (ft *FakeTransport) SetFailAfter(url string, n int)
- type FlakyReader
- type MultiFailReader
Constants ¶
This section is empty.
Variables ¶
var ErrFlakyFailure = errors.New("simulated read failure")
ErrFlakyFailure is returned when FlakyReader simulates a failure.
Functions ¶
func AssertDataEquals ¶
AssertDataEquals checks if two byte slices are equal.
func AssertError ¶
AssertError fails the test if err is nil.
func AssertHeaderAbsent ¶
AssertHeaderAbsent checks if a header is absent.
func AssertHeaderEquals ¶
AssertHeaderEquals checks if a header has the expected value.
func AssertHeaderPresent ¶
AssertHeaderPresent checks if a header is present.
func AssertNoError ¶
AssertNoError fails the test if err is not nil.
func ConcatChunks ¶
ConcatChunks concatenates multiple byte slices into one.
func GenerateRandomData ¶
GenerateRandomData generates random test data of the specified size.
func GenerateTestData ¶
GenerateTestData generates deterministic test data of the specified size.
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 ¶
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 ¶
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) 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) 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.