@@ -11,14 +11,15 @@ import (
11
11
"io"
12
12
"net/http"
13
13
"net/http/httptest"
14
+ "strconv"
14
15
"strings"
15
16
"testing"
16
17
17
18
tassert "github.com/stretchr/testify/assert"
18
19
"github.com/stretchr/testify/require"
19
20
)
20
21
21
- func TestParseMultiAddr (t * testing.T ) {
22
+ func TestIPFSParseMultiAddr (t * testing.T ) {
22
23
tassert := tassert .New (t )
23
24
24
25
tests := []struct {
@@ -75,6 +76,9 @@ func TestParseMultiAddr(t *testing.T) {
75
76
76
77
// Mock IPFS HTTP API server for testing
77
78
func setupMockIPFSServer () * httptest.Server {
79
+ // Content store to maintain uploaded files
80
+ contentStore := make (map [string ][]byte )
81
+
78
82
mux := http .NewServeMux ()
79
83
80
84
// Mock /api/v0/add endpoint
@@ -111,6 +115,9 @@ func setupMockIPFSServer() *httptest.Server {
111
115
mockCID = mockCID [:46 ]
112
116
}
113
117
118
+ // Store content for later retrieval
119
+ contentStore [mockCID ] = content
120
+
114
121
// Return IPFS add response format
115
122
w .Header ().Set ("Content-Type" , "application/json" )
116
123
fmt .Fprintf (w , `{"Name":"","Hash":"%s","Size":"%d"}` , mockCID , len (content ))
@@ -129,13 +136,13 @@ func setupMockIPFSServer() *httptest.Server {
129
136
return
130
137
}
131
138
132
- // Return mock content based on CID
133
- if strings .HasPrefix (cid , "Qm" ) {
134
- // Extract original content from mock CID (simplified)
139
+ // Return stored content based on CID
140
+ if content , exists := contentStore [cid ]; exists {
135
141
w .Header ().Set ("Content-Type" , "application/octet-stream" )
136
- fmt .Fprintf (w , "mock-content-for-%s" , cid )
142
+ w .Header ().Set ("Content-Length" , strconv .Itoa (len (content )))
143
+ w .Write (content )
137
144
} else {
138
- http .Error (w , "Invalid CID " , http .StatusBadRequest )
145
+ http .Error (w , "Content not found " , http .StatusNotFound )
139
146
}
140
147
})
141
148
@@ -153,7 +160,7 @@ func setupMockIPFSServer() *httptest.Server {
153
160
return httptest .NewServer (mux )
154
161
}
155
162
156
- func TestIPFSClientCompatibility (t * testing.T ) {
163
+ func TestIPFSHTTPClientCompatibility (t * testing.T ) {
157
164
// Setup mock IPFS server
158
165
server := setupMockIPFSServer ()
159
166
defer server .Close ()
@@ -178,15 +185,21 @@ func TestIPFSClientCompatibility(t *testing.T) {
178
185
})
179
186
180
187
t .Run ("Cat operation retrieves content" , func (t * testing.T ) {
181
- testCID := "QmTestCID12345"
188
+ // First upload content to get a valid CID
189
+ testData := []byte ("test data for cat operation" )
190
+ reader := bytes .NewReader (testData )
191
+
192
+ uploadedCID , err := client .Add (ctx , reader )
193
+ require .NoError (t , err )
182
194
183
- reader , err := client .Cat (ctx , testCID )
195
+ // Now retrieve it with Cat
196
+ catReader , err := client .Cat (ctx , uploadedCID )
184
197
require .NoError (t , err )
185
- defer reader .Close ()
198
+ defer catReader .Close ()
186
199
187
- content , err := io .ReadAll (reader )
200
+ content , err := io .ReadAll (catReader )
188
201
require .NoError (t , err )
189
- tassert .Contains (t , string ( content ), testCID )
202
+ tassert .Equal (t , testData , content , "Cat should return original content" )
190
203
})
191
204
192
205
t .Run ("SwarmPeers returns peer list" , func (t * testing.T ) {
@@ -198,7 +211,7 @@ func TestIPFSClientCompatibility(t *testing.T) {
198
211
})
199
212
}
200
213
201
- func TestLightweightClientCompatibility (t * testing.T ) {
214
+ func TestIPFSLightweightClientCompatibility (t * testing.T ) {
202
215
// Setup mock IPFS server
203
216
server := setupMockIPFSServer ()
204
217
defer server .Close ()
@@ -223,18 +236,21 @@ func TestLightweightClientCompatibility(t *testing.T) {
223
236
})
224
237
225
238
t .Run ("Get with IPFSPath returns IPFSNode" , func (t * testing.T ) {
226
- testCID := "QmTestCID67890"
227
- path , err := NewIPFSPath (testCID )
239
+ // First upload content to get a valid CID
240
+ testData := []byte ("test data for get operation" )
241
+ uploadFile := NewIPFSFileFromBytes (testData )
242
+ uploadedPath , err := client .Unixfs ().Add (ctx , uploadFile )
228
243
require .NoError (t , err )
229
244
230
- node , err := client .Unixfs ().Get (ctx , path )
245
+ // Now retrieve it
246
+ node , err := client .Unixfs ().Get (ctx , uploadedPath )
231
247
require .NoError (t , err )
232
248
defer node .Close ()
233
249
234
250
var buf bytes.Buffer
235
251
_ , err = buf .ReadFrom (node )
236
252
require .NoError (t , err )
237
- tassert .Contains (t , buf .String (), testCID )
253
+ tassert .Equal (t , testData , buf .Bytes (), "Retrieved content should match uploaded content" )
238
254
})
239
255
240
256
t .Run ("Swarm peers returns string slice" , func (t * testing.T ) {
@@ -245,7 +261,7 @@ func TestLightweightClientCompatibility(t *testing.T) {
245
261
})
246
262
}
247
263
248
- func TestBlobUploadResponseFormat (t * testing.T ) {
264
+ func TestIPFSBlobUploadResponseFormat (t * testing.T ) {
249
265
// This test verifies the blob upload response format matches expectations
250
266
// Based on the test failure: expected '/ipfs/CID' but got 'CID'
251
267
@@ -329,7 +345,7 @@ func TestIPFSFileOperations(t *testing.T) {
329
345
}
330
346
331
347
// Benchmark tests to ensure performance is acceptable
332
- func BenchmarkIPFSClientAdd (b * testing.B ) {
348
+ func BenchmarkIPFSHTTPClientAdd (b * testing.B ) {
333
349
server := setupMockIPFSServer ()
334
350
defer server .Close ()
335
351
@@ -348,7 +364,7 @@ func BenchmarkIPFSClientAdd(b *testing.B) {
348
364
}
349
365
}
350
366
351
- func BenchmarkLightweightClientAdd (b * testing.B ) {
367
+ func BenchmarkIPFSLightweightClientAdd (b * testing.B ) {
352
368
server := setupMockIPFSServer ()
353
369
defer server .Close ()
354
370
0 commit comments