-
Notifications
You must be signed in to change notification settings - Fork 0
/
walrus_test.go
290 lines (243 loc) · 8.07 KB
/
walrus_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package walrus_go
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
)
const (
testAggregatorURL = "https://aggregator.walrus-testnet.walrus.space"
testPublisherURL = "https://publisher.walrus-testnet.walrus.space"
testContent = "Hello, Walrus!"
)
// Helper function to create a test client
func newTestClient(t *testing.T) *Client {
return NewClient(testAggregatorURL, testPublisherURL)
}
// Helper function to store test content and return blobID
func storeTestContent(t *testing.T, client *Client) string {
resp, err := client.Store([]byte(testContent), &StoreOptions{Epochs: 1})
if err != nil {
t.Fatalf("Failed to store test content: %v", err)
}
resp.NormalizeBlobResponse()
return resp.Blob.BlobID
}
// TestNewClient tests client initialization
func TestNewClient(t *testing.T) {
client := newTestClient(t)
if client.AggregatorURL != testAggregatorURL {
t.Errorf("Expected aggregator URL %s, got %s", testAggregatorURL, client.AggregatorURL)
}
if client.PublisherURL != testPublisherURL {
t.Errorf("Expected publisher URL %s, got %s", testPublisherURL, client.PublisherURL)
}
}
// TestStore tests storing data
func TestStore(t *testing.T) {
client := newTestClient(t)
resp, err := client.Store([]byte(testContent), &StoreOptions{Epochs: 1})
if err != nil {
t.Fatalf("Store failed: %v", err)
}
resp.NormalizeBlobResponse()
if resp.Blob.BlobID == "" {
t.Error("Expected non-empty blob ID")
}
if resp.Blob.EndEpoch <= 0 {
t.Error("Expected positive end epoch")
}
t.Log("Stored blob with ID:", resp.Blob.BlobID)
}
// TestStoreReader tests storing data from a reader
func TestStoreReader(t *testing.T) {
client := newTestClient(t)
reader := strings.NewReader(testContent)
resp, err := client.StoreReader(reader, int64(len(testContent)), &StoreOptions{Epochs: 1})
if err != nil {
t.Fatalf("StoreReader failed: %v", err)
}
resp.NormalizeBlobResponse()
if resp.Blob.BlobID == "" {
t.Error("Expected non-empty blob ID")
}
t.Log("Stored blob with ID:", resp.Blob.BlobID)
}
// TestStoreFromURL tests storing data from a URL
func TestStoreFromURL(t *testing.T) {
client := newTestClient(t)
// Using a reliable test URL
testURL := "https://raw.githubusercontent.com/suiet/walrus-go/main/README.md"
resp, err := client.StoreFromURL(testURL, &StoreOptions{Epochs: 1})
if err != nil {
t.Fatalf("StoreFromURL failed: %v", err)
}
resp.NormalizeBlobResponse()
if resp.Blob.BlobID == "" {
t.Error("Expected non-empty blob ID")
}
t.Log("Stored blob with ID:", resp.Blob.BlobID)
}
// TestStoreFile tests storing a file
func TestStoreFile(t *testing.T) {
client := newTestClient(t)
// Create a temporary test file
tmpfile, err := os.CreateTemp("", "walrus-test-*.txt")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write([]byte(testContent)); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
tmpfile.Close()
resp, err := client.StoreFile(tmpfile.Name(), &StoreOptions{Epochs: 1})
if err != nil {
t.Fatalf("StoreFile failed: %v", err)
}
resp.NormalizeBlobResponse()
if resp.Blob.BlobID == "" {
t.Error("Expected non-empty blob ID")
}
}
// TestHead tests retrieving blob metadata
func TestHead(t *testing.T) {
client := newTestClient(t)
blobID := storeTestContent(t, client)
t.Log("Blob ID:", blobID)
metadata, err := client.Head(blobID)
if err != nil {
t.Fatalf("Head failed: %v", err)
}
if metadata.ContentLength != int64(len(testContent)) {
t.Errorf("Expected content length %d, got %d", len(testContent), metadata.ContentLength)
}
if metadata.ContentType == "" {
t.Error("Expected non-empty content type")
}
}
// TestRead tests reading blob content
func TestRead(t *testing.T) {
client := newTestClient(t)
blobID := storeTestContent(t, client)
data, err := client.Read(blobID)
if err != nil {
t.Fatalf("Read failed: %v", err)
}
if string(data) != testContent {
t.Errorf("Expected content %q, got %q", testContent, string(data))
}
t.Log("Read content:", string(data))
}
// TestReadToFile tests reading blob to a file
func TestReadToFile(t *testing.T) {
client := newTestClient(t)
blobID := storeTestContent(t, client)
tmpfile, err := os.CreateTemp("", "walrus-read-test-*.txt")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpfile.Name())
tmpfile.Close()
if err := client.ReadToFile(blobID, tmpfile.Name()); err != nil {
t.Fatalf("ReadToFile failed: %v", err)
}
content, err := os.ReadFile(tmpfile.Name())
if err != nil {
t.Fatalf("Failed to read temp file: %v", err)
}
if string(content) != testContent {
t.Errorf("Expected content %q, got %q", testContent, string(content))
}
t.Log("Read content:", string(content))
}
// TestReadToReader tests reading blob to an io.Reader
func TestReadToReader(t *testing.T) {
client := newTestClient(t)
blobID := storeTestContent(t, client)
reader, err := client.ReadToReader(blobID)
if err != nil {
t.Fatalf("ReadToReader failed: %v", err)
}
defer reader.Close()
var buf bytes.Buffer
if _, err := io.Copy(&buf, reader); err != nil {
t.Fatalf("Failed to read from reader: %v", err)
}
if buf.String() != testContent {
t.Errorf("Expected content %q, got %q", testContent, buf.String())
}
t.Log("Read content:", buf.String())
}
// TestGetAPISpec tests retrieving API specifications
func TestGetAPISpec(t *testing.T) {
client := newTestClient(t)
// Test aggregator API spec
aggSpec, err := client.GetAPISpec(true)
if err != nil {
t.Fatalf("GetAPISpec(aggregator) failed: %v", err)
}
if len(aggSpec) == 0 {
t.Error("Expected non-empty aggregator API spec")
}
// Test publisher API spec
pubSpec, err := client.GetAPISpec(false)
if err != nil {
t.Fatalf("GetAPISpec(publisher) failed: %v", err)
}
if len(pubSpec) == 0 {
t.Error("Expected non-empty publisher API spec")
}
t.Log("Aggregator API spec:", string(aggSpec))
}
// TestNormalizeBlobResponse tests response normalization
func TestNormalizeBlobResponse(t *testing.T) {
// Test with NewlyCreated response
newResp := &StoreResponse{
NewlyCreated: &struct {
BlobObject BlobObject `json:"blobObject"`
EncodedSize int `json:"encodedSize"`
Cost int `json:"cost"`
}{
BlobObject: BlobObject{
BlobID: "test-blob-id",
Storage: StorageInfo{
EndEpoch: 100,
},
},
},
}
newResp.NormalizeBlobResponse()
if newResp.Blob.BlobID != "test-blob-id" || newResp.Blob.EndEpoch != 100 {
t.Error("Failed to normalize NewlyCreated response")
}
// Test with AlreadyCertified response
certResp := &StoreResponse{
AlreadyCertified: &struct {
BlobID string `json:"blobId"`
Event EventInfo `json:"event"`
EndEpoch int `json:"endEpoch"`
}{
BlobID: "test-blob-id",
EndEpoch: 200,
},
}
certResp.NormalizeBlobResponse()
if certResp.Blob.BlobID != "test-blob-id" || certResp.Blob.EndEpoch != 200 {
t.Error("Failed to normalize AlreadyCertified response")
}
t.Log("Normalized response:", certResp)
}
// Example usage of the client
func ExampleClient_Store() {
client := NewClient(testAggregatorURL, testPublisherURL)
resp, err := client.Store([]byte("Hello, World!"), &StoreOptions{Epochs: 1})
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
resp.NormalizeBlobResponse()
fmt.Printf("Stored blob with ID: %s\n", resp.Blob.BlobID)
}