2024-03-15 21:38:13 +00:00
|
|
|
package seal
|
2024-01-22 16:33:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2024-02-12 14:14:40 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-01-22 16:33:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestUrlPieceReader_Read tests various scenarios of reading data from UrlPieceReader
|
|
|
|
func TestUrlPieceReader_Read(t *testing.T) {
|
|
|
|
// Create a test server
|
|
|
|
testData := "This is a test string."
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-02-12 14:14:40 +00:00
|
|
|
_, err := io.WriteString(w, testData)
|
|
|
|
require.NoError(t, err)
|
2024-01-22 16:33:59 +00:00
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
rawSize int64
|
|
|
|
expected string
|
|
|
|
expectError bool
|
|
|
|
expectEOF bool
|
|
|
|
}{
|
|
|
|
{"ReadExact", int64(len(testData)), testData, false, true},
|
|
|
|
{"ReadLess", 10, testData[:10], false, false},
|
|
|
|
{"ReadMore", int64(len(testData)) + 10, "", true, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2024-02-12 14:14:40 +00:00
|
|
|
tt := tt
|
|
|
|
|
2024-01-22 16:33:59 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
reader := UrlPieceReader{
|
|
|
|
Url: ts.URL,
|
|
|
|
RawSize: tt.rawSize,
|
|
|
|
}
|
|
|
|
buffer, err := io.ReadAll(&reader)
|
|
|
|
if err != nil {
|
|
|
|
if (err != io.EOF && !tt.expectError) || (err == io.EOF && !tt.expectEOF) {
|
|
|
|
t.Errorf("Read() error = %v, expectError %v, expectEOF %v", err, tt.expectError, tt.expectEOF)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if got := string(buffer); got != tt.expected {
|
|
|
|
t.Errorf("Read() got = %v, expected %v", got, tt.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestUrlPieceReader_Read_Error tests the error handling of UrlPieceReader
|
|
|
|
func TestUrlPieceReader_Read_Error(t *testing.T) {
|
|
|
|
// Simulate a server that returns an error
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Error(w, "error", http.StatusInternalServerError)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
reader := UrlPieceReader{
|
|
|
|
Url: ts.URL,
|
|
|
|
RawSize: 100,
|
|
|
|
}
|
|
|
|
buffer := make([]byte, 200)
|
|
|
|
|
|
|
|
_, err := reader.Read(buffer)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected an error, but got nil")
|
|
|
|
}
|
|
|
|
}
|