@@ -10,6 +10,11 @@ const isNode = require('detect-node')
10
10
const path = require ( 'path' )
11
11
const fs = require ( 'fs' )
12
12
const os = require ( 'os' )
13
+ const http = require ( 'http' )
14
+ const https = require ( 'https' )
15
+ const each = require ( 'async/each' )
16
+ const waterfall = require ( 'async/waterfall' )
17
+ const parallel = require ( 'async/parallel' )
13
18
14
19
const IPFSApi = require ( '../src' )
15
20
const f = require ( './utils/factory' )
@@ -32,7 +37,8 @@ describe('.util', () => {
32
37
} )
33
38
} )
34
39
35
- after ( ( done ) => {
40
+ after ( function ( done ) {
41
+ this . timeout ( 10 * 1000 )
36
42
if ( ! ipfsd ) return done ( )
37
43
ipfsd . stop ( done )
38
44
} )
@@ -113,81 +119,257 @@ describe('.util', () => {
113
119
} )
114
120
115
121
describe ( '.urlAdd' , ( ) => {
116
- it ( 'http' , function ( done ) {
117
- this . timeout ( 40 * 1000 )
118
-
119
- ipfs . util . addFromURL ( 'http://example.com/' , ( err , result ) => {
120
- expect ( err ) . to . not . exist ( )
121
- expect ( result . length ) . to . equal ( 1 )
122
- done ( )
122
+ let testServers = [ ]
123
+
124
+ const sslOpts = {
125
+ key : fs . readFileSync ( path . join ( __dirname , 'fixtures' , 'ssl' , 'privkey.pem' ) ) ,
126
+ cert : fs . readFileSync ( path . join ( __dirname , 'fixtures' , 'ssl' , 'cert.pem' ) )
127
+ }
128
+
129
+ const startTestServer = ( handler , opts , cb ) => {
130
+ if ( typeof opts === 'function' ) {
131
+ cb = opts
132
+ opts = { }
133
+ }
134
+
135
+ const server = opts . secure
136
+ ? https . createServer ( sslOpts , handler )
137
+ : http . createServer ( handler )
138
+
139
+ server . listen ( ( err ) => {
140
+ if ( err ) return cb ( err )
141
+ testServers . push ( server )
142
+ cb ( null , server )
123
143
} )
144
+ }
145
+
146
+ beforeEach ( ( ) => {
147
+ // Instructs node to not reject our snake oil SSL certificate when it
148
+ // can't verify the certificate authority
149
+ process . env . NODE_TLS_REJECT_UNAUTHORIZED = 0
124
150
} )
125
151
126
- it ( 'https' , function ( done ) {
127
- this . timeout ( 40 * 1000 )
152
+ afterEach ( ( done ) => {
153
+ // Reinstate unauthorised SSL cert rejection
154
+ process . env . NODE_TLS_REJECT_UNAUTHORIZED = 1
128
155
129
- ipfs . util . addFromURL ( 'https://example.com/' , ( err , result ) => {
130
- expect ( err ) . to . not . exist ( )
131
- expect ( result . length ) . to . equal ( 1 )
132
- done ( )
156
+ each ( testServers , ( server , cb ) => server . close ( cb ) , ( err ) => {
157
+ testServers = [ ]
158
+ done ( err )
133
159
} )
134
160
} )
135
161
136
- it ( 'http with redirection' , function ( done ) {
137
- this . timeout ( 40 * 1000 )
138
-
139
- ipfs . util . addFromURL ( 'http://covers.openlibrary.org/book/id/969165.jpg' , ( err , result ) => {
162
+ it ( 'http' , ( done ) => {
163
+ const data = Buffer . from ( `TEST${ Date . now ( ) } ` )
164
+
165
+ parallel ( {
166
+ server : ( cb ) => {
167
+ const handler = ( req , res ) => {
168
+ res . write ( data )
169
+ res . end ( )
170
+ }
171
+ startTestServer ( handler , cb )
172
+ } ,
173
+ expectedResult : ( cb ) => ipfs . add ( data , cb )
174
+ } , ( err , taskResult ) => {
140
175
expect ( err ) . to . not . exist ( )
141
- expect ( result [ 0 ] . hash ) . to . equal ( 'QmaL9zy7YUfvWmtD5ZXp42buP7P4xmZJWFkm78p8FJqgjg' )
142
- done ( )
176
+ const { server, expectedResult } = taskResult
177
+
178
+ const url = `http://127.0.0.1:${ server . address ( ) . port } /`
179
+ ipfs . util . addFromURL ( url , ( err , result ) => {
180
+ expect ( err ) . to . not . exist ( )
181
+ expect ( result ) . to . deep . equal ( expectedResult )
182
+ done ( )
183
+ } )
143
184
} )
144
185
} )
145
186
146
- it ( 'https with redirection' , function ( done ) {
147
- this . timeout ( 40 * 1000 )
187
+ it ( 'https' , ( done ) => {
188
+ const data = Buffer . from ( `TEST${ Date . now ( ) } ` )
189
+
190
+ parallel ( {
191
+ server : ( cb ) => {
192
+ const handler = ( req , res ) => {
193
+ res . write ( data )
194
+ res . end ( )
195
+ }
196
+ startTestServer ( handler , { secure : true } , cb )
197
+ } ,
198
+ expectedResult : ( cb ) => ipfs . add ( data , cb )
199
+ } , ( err , taskResult ) => {
200
+ expect ( err ) . to . not . exist ( )
201
+ const { server, expectedResult } = taskResult
148
202
149
- ipfs . util . addFromURL ( 'https://coverartarchive.org/release/6e2a1694-d8b9-466a-aa33-b1077b2333c1' , ( err , result ) => {
203
+ const url = `https://127.0.0.1:${ server . address ( ) . port } /`
204
+ ipfs . util . addFromURL ( url , ( err , result ) => {
205
+ expect ( err ) . to . not . exist ( )
206
+ expect ( result ) . to . deep . equal ( expectedResult )
207
+ done ( )
208
+ } )
209
+ } )
210
+ } )
211
+
212
+ it ( 'http with redirection' , ( done ) => {
213
+ const data = Buffer . from ( `TEST${ Date . now ( ) } ` )
214
+
215
+ waterfall ( [
216
+ ( cb ) => {
217
+ const handler = ( req , res ) => {
218
+ res . write ( data )
219
+ res . end ( )
220
+ }
221
+ startTestServer ( handler , cb )
222
+ } ,
223
+ ( serverA , cb ) => {
224
+ const url = `http://127.0.0.1:${ serverA . address ( ) . port } `
225
+ const handler = ( req , res ) => {
226
+ res . statusCode = 302
227
+ res . setHeader ( 'Location' , url )
228
+ res . end ( )
229
+ }
230
+ startTestServer ( handler , ( err , serverB ) => {
231
+ if ( err ) return cb ( err )
232
+ cb ( null , { a : serverA , b : serverB } )
233
+ } )
234
+ }
235
+ ] , ( err , servers ) => {
150
236
expect ( err ) . to . not . exist ( )
151
- expect ( result [ 0 ] . hash ) . to . equal ( 'QmSUdDvmXuq5YGrL4M3SEz7UZh5eT9WMuAsd9K34sambSj' )
152
- done ( )
237
+
238
+ ipfs . add ( data , ( err , res ) => {
239
+ expect ( err ) . to . not . exist ( )
240
+
241
+ const expectedHash = res [ 0 ] . hash
242
+ const url = `http://127.0.0.1:${ servers . b . address ( ) . port } `
243
+
244
+ ipfs . util . addFromURL ( url , ( err , result ) => {
245
+ expect ( err ) . to . not . exist ( )
246
+ expect ( result [ 0 ] . hash ) . to . equal ( expectedHash )
247
+ done ( )
248
+ } )
249
+ } )
153
250
} )
154
251
} )
155
252
156
- it ( 'with only-hash=true' , function ( ) {
157
- this . timeout ( 40 * 1000 )
253
+ it ( 'https with redirection' , ( done ) => {
254
+ const data = Buffer . from ( `TEST${ Date . now ( ) } ` )
255
+
256
+ waterfall ( [
257
+ ( cb ) => {
258
+ const handler = ( req , res ) => {
259
+ res . write ( data )
260
+ res . end ( )
261
+ }
262
+ startTestServer ( handler , { secure : true } , cb )
263
+ } ,
264
+ ( serverA , cb ) => {
265
+ const url = `https://127.0.0.1:${ serverA . address ( ) . port } `
266
+ const handler = ( req , res ) => {
267
+ res . statusCode = 302
268
+ res . setHeader ( 'Location' , url )
269
+ res . end ( )
270
+ }
271
+ startTestServer ( handler , { secure : true } , ( err , serverB ) => {
272
+ if ( err ) return cb ( err )
273
+ cb ( null , { a : serverA , b : serverB } )
274
+ } )
275
+ }
276
+ ] , ( err , servers ) => {
277
+ expect ( err ) . to . not . exist ( )
278
+
279
+ ipfs . add ( data , ( err , res ) => {
280
+ expect ( err ) . to . not . exist ( )
281
+
282
+ const expectedHash = res [ 0 ] . hash
283
+ const url = `https://127.0.0.1:${ servers . b . address ( ) . port } `
158
284
159
- return ipfs . util . addFromURL ( 'http://www.randomtext.me/#/gibberish' , { onlyHash : true } )
160
- . then ( out => expectTimeout ( ipfs . object . get ( out [ 0 ] . hash ) , 4000 ) )
285
+ ipfs . util . addFromURL ( url , ( err , result ) => {
286
+ expect ( err ) . to . not . exist ( )
287
+ expect ( result [ 0 ] . hash ) . to . equal ( expectedHash )
288
+ done ( )
289
+ } )
290
+ } )
291
+ } )
161
292
} )
162
293
163
- it ( 'with wrap-with-directory=true' , function ( done ) {
164
- this . timeout ( 40 * 1000 )
294
+ it ( 'with only-hash=true' , ( done ) => {
295
+ const handler = ( req , res ) => {
296
+ res . write ( `TEST${ Date . now ( ) } ` )
297
+ res . end ( )
298
+ }
165
299
166
- ipfs . util . addFromURL ( 'http://ipfs.io/ipfs/QmWjppACLcFLQ2qL38unKQvJBhXH3RUtcGLPk7zmrTwV61/969165.jpg?foo=bar#buzz' , {
167
- wrapWithDirectory : true
168
- } , ( err , result ) => {
300
+ startTestServer ( handler , ( err , server ) => {
169
301
expect ( err ) . to . not . exist ( )
170
- expect ( result [ 0 ] . hash ) . to . equal ( 'QmaL9zy7YUfvWmtD5ZXp42buP7P4xmZJWFkm78p8FJqgjg' )
171
- expect ( result [ 0 ] . path ) . to . equal ( '969165.jpg' )
172
- expect ( result [ 1 ] . hash ) . to . equal ( 'QmWjppACLcFLQ2qL38unKQvJBhXH3RUtcGLPk7zmrTwV61' )
173
- expect ( result . length ) . to . equal ( 2 )
174
- done ( )
302
+
303
+ const url = `http://127.0.0.1:${ server . address ( ) . port } /`
304
+
305
+ ipfs . util . addFromURL ( url , { onlyHash : true } , ( err , res ) => {
306
+ expect ( err ) . to . not . exist ( )
307
+
308
+ // A successful object.get for this size data took my laptop ~14ms
309
+ let didTimeout = false
310
+ const timeoutId = setTimeout ( ( ) => {
311
+ didTimeout = true
312
+ done ( )
313
+ } , 500 )
314
+
315
+ ipfs . object . get ( res [ 0 ] . hash , ( ) => {
316
+ clearTimeout ( timeoutId )
317
+ if ( didTimeout ) return
318
+ expect ( new Error ( 'did not timeout' ) ) . to . not . exist ( )
319
+ } )
320
+ } )
175
321
} )
176
322
} )
177
323
178
- it ( 'with wrap-with-directory=true and URL-escaped file name' , function ( done ) {
179
- this . timeout ( 40 * 1000 )
324
+ it ( 'with wrap-with-directory=true' , ( done ) => {
325
+ const filename = `TEST${ Date . now ( ) } .txt`
326
+ const data = Buffer . from ( `TEST${ Date . now ( ) } ` )
327
+
328
+ parallel ( {
329
+ server : ( cb ) => startTestServer ( ( req , res ) => {
330
+ res . write ( data )
331
+ res . end ( )
332
+ } , cb ) ,
333
+ expectedResult : ( cb ) => {
334
+ ipfs . add ( [ { path : filename , content : data } ] , { wrapWithDirectory : true } , cb )
335
+ }
336
+ } , ( err , taskResult ) => {
337
+ expect ( err ) . to . not . exist ( )
338
+
339
+ const { server, expectedResult } = taskResult
340
+ const url = `http://127.0.0.1:${ server . address ( ) . port } /${ filename } ?foo=bar#buzz`
341
+
342
+ ipfs . util . addFromURL ( url , { wrapWithDirectory : true } , ( err , result ) => {
343
+ expect ( err ) . to . not . exist ( )
344
+ expect ( result ) . to . deep . equal ( expectedResult )
345
+ done ( )
346
+ } )
347
+ } )
348
+ } )
180
349
181
- // Sample URL contains URL-escaped ( ) and local diacritics
182
- ipfs . util . addFromURL ( 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/Doma%C5%BElice%2C_Jir%C3%A1skova_43_%289102%29.jpg/320px-Doma%C5%BElice%2C_Jir%C3%A1skova_43_%289102%29.jpg?foo=bar#buzz' , {
183
- wrapWithDirectory : true
184
- } , ( err , result ) => {
350
+ it ( 'with wrap-with-directory=true and URL-escaped file name' , ( done ) => {
351
+ const filename = '320px-Domažlice,_Jiráskova_43_(9102).jpg'
352
+ const data = Buffer . from ( `TEST${ Date . now ( ) } ` )
353
+
354
+ parallel ( {
355
+ server : ( cb ) => startTestServer ( ( req , res ) => {
356
+ res . write ( data )
357
+ res . end ( )
358
+ } , cb ) ,
359
+ expectedResult : ( cb ) => {
360
+ ipfs . add ( [ { path : filename , content : data } ] , { wrapWithDirectory : true } , cb )
361
+ }
362
+ } , ( err , taskResult ) => {
185
363
expect ( err ) . to . not . exist ( )
186
- expect ( result [ 0 ] . hash ) . to . equal ( 'QmRJ9ExxSMV4BLF9ZJUb2mLngupm6BXZEek755VHGTJo2Y' )
187
- expect ( result [ 0 ] . path ) . to . equal ( '320px-Domažlice,_Jiráskova_43_(9102).jpg' )
188
- expect ( result [ 1 ] . hash ) . to . equal ( 'QmbxsHFU3sCfr8wszDHuDLA76C2xCv9HT8L3aC1pBwgaHk' )
189
- expect ( result . length ) . to . equal ( 2 )
190
- done ( )
364
+
365
+ const { server, expectedResult } = taskResult
366
+ const url = `http://127.0.0.1:${ server . address ( ) . port } /${ encodeURIComponent ( filename ) } ?foo=bar#buzz`
367
+
368
+ ipfs . util . addFromURL ( url , { wrapWithDirectory : true } , ( err , result ) => {
369
+ expect ( err ) . to . not . exist ( )
370
+ expect ( result ) . to . deep . equal ( expectedResult )
371
+ done ( )
372
+ } )
191
373
} )
192
374
} )
193
375
0 commit comments