@@ -13,6 +13,7 @@ var execSync = require('child_process').execSync;
1313var debug = require ( 'debug' ) ( 'http-disk-cache' ) ;
1414var async = require ( 'artillery-async' ) ;
1515var glob = require ( 'glob' ) ;
16+ var stream = require ( 'stream' ) ;
1617
1718var httpcache = require ( './index' ) ;
1819
@@ -36,16 +37,19 @@ function newUrlReply(contents, status, headers, defer) {
3637
3738function catStream ( stream , cb ) {
3839 chunks = [ ] ;
40+ stream . on ( 'error' , function ( err ) {
41+ cb ( err ) ;
42+ } ) ;
3943 stream . on ( 'data' , function ( chunk ) {
4044 chunks . push ( chunk ) ;
4145 } ) ;
4246 stream . on ( 'end' , function ( ) {
4347 if ( chunks . length === 0 ) {
44- cb ( null ) ;
48+ cb ( null , null ) ;
4549 } else if ( typeof chunks [ 0 ] === 'string' ) {
46- cb ( chunks . join ( '' ) ) ;
50+ cb ( null , chunks . join ( '' ) ) ;
4751 } else { // Buffer
48- cb ( Buffer . concat ( chunks ) ) ;
52+ cb ( null , Buffer . concat ( chunks ) ) ;
4953 }
5054 } ) ;
5155}
@@ -166,7 +170,7 @@ exports.tests = {
166170 var _this = this ;
167171 this . cache . openReadStream ( this . createUrl ( '/url1' ) , function ( err , stream , path ) {
168172 test . ok ( stream instanceof fs . ReadStream , "stream should be an fs.ReadStream" ) ;
169- catStream ( stream , function ( contents ) {
173+ catStream ( stream , function ( err , contents ) {
170174 test . equal ( contents . toString ( 'utf8' ) , 'url1 contents' ) ;
171175 test . done ( ) ;
172176 } ) ;
@@ -182,7 +186,7 @@ exports.tests = {
182186 test . equal ( _this . requests . length , 1 ) ;
183187 test . equal ( _this . requests [ 0 ] , '/url5' ) ;
184188 test . ok ( stream instanceof fs . ReadStream , "stream should be an fs.ReadStream" ) ;
185- catStream ( stream , function ( contents ) {
189+ catStream ( stream , function ( err , contents ) {
186190 test . equal ( contents . toString ( 'utf8' ) , 'url5 contents' ) ;
187191 test . done ( ) ;
188192 } ) ;
@@ -192,7 +196,7 @@ exports.tests = {
192196 _this . cache . openReadStream ( { url : _this . createUrl ( '/url5' ) , etagFormat : 'md5' } , function ( err , stream , path ) {
193197 test . equal ( _this . requests . length , 1 ) ; // request is handled from cache.
194198 test . ok ( stream instanceof fs . ReadStream , "stream should be an fs.ReadStream" ) ;
195- catStream ( stream , function ( contents ) {
199+ catStream ( stream , function ( err , contents ) {
196200 test . equal ( contents . toString ( 'utf8' ) , 'url5 contents' ) ;
197201 test . done ( ) ;
198202 } ) ;
@@ -213,7 +217,7 @@ exports.tests = {
213217 test . equal ( _this . requests . length , 1 ) ;
214218 test . equal ( _this . requests [ 0 ] , '/url7' ) ;
215219 test . ok ( stream instanceof fs . ReadStream , "stream should be an fs.ReadStream" ) ;
216- catStream ( stream , function ( contents ) {
220+ catStream ( stream , function ( err , contents ) {
217221 test . equal ( contents . toString ( 'utf8' ) , 'url7 contents' ) ;
218222 test . done ( ) ;
219223 } ) ;
@@ -223,7 +227,7 @@ exports.tests = {
223227 _this . cache . openReadStream ( { url : _this . createUrl ( '/url7' ) , etagFormat : 'md5' } , function ( err , stream , path ) {
224228 test . equal ( _this . requests . length , 1 ) ; // request is handled from cache.
225229 test . ok ( stream instanceof fs . ReadStream , "stream should be an fs.ReadStream" ) ;
226- catStream ( stream , function ( contents ) {
230+ catStream ( stream , function ( err , contents ) {
227231 test . equal ( contents . toString ( 'utf8' ) , 'url7 contents' ) ;
228232 test . done ( ) ;
229233 } ) ;
@@ -256,7 +260,7 @@ exports.tests = {
256260 _this . cache . openReadStream ( { url : _this . createUrl ( '/url1' ) , etagFormat : 'md5' } , function ( err , stream , path ) {
257261 test . equal ( _this . requests . length , 1 ) ;
258262 test . equal ( _this . requests [ 0 ] , '/url1' ) ;
259- catStream ( stream , function ( contents ) {
263+ catStream ( stream , function ( err , contents ) {
260264 test . equal ( contents . toString ( 'utf8' ) , 'url1 contents' ) ;
261265 cb ( ) ;
262266 } ) ;
@@ -268,7 +272,7 @@ exports.tests = {
268272 _this . cache . openReadStream ( { url : _this . createUrl ( '/url1' ) , etagFormat : 'md5' } , function ( err , stream , path ) {
269273 test . equal ( _this . requests . length , 2 ) ;
270274 test . equal ( _this . requests [ 1 ] , '/url1' ) ;
271- catStream ( stream , function ( contents ) {
275+ catStream ( stream , function ( err , contents ) {
272276 test . equal ( contents . toString ( 'utf8' ) , 'url1 contents' ) ;
273277 cb ( ) ;
274278 } ) ;
@@ -358,7 +362,7 @@ exports.tests = {
358362 } ,
359363
360364 testConcurrentRequests : function ( test ) {
361- test . expect ( 4 ) ;
365+ test . expect ( 2 ) ;
362366 var _this = this ;
363367 var count = 2 ;
364368
@@ -367,8 +371,7 @@ exports.tests = {
367371 if ( count === 0 ) { test . done ( ) ; }
368372 } ;
369373 var cb = function ( err , stream , path ) {
370- test . ok ( stream instanceof fs . ReadStream , "stream should be an fs.ReadStream" ) ;
371- catStream ( stream , function ( contents ) {
374+ catStream ( stream , function ( err , contents ) {
372375 test . equal ( contents . toString ( 'utf8' ) , 'url1 contents' ) ;
373376 barrier ( ) ;
374377 } ) ;
@@ -379,34 +382,34 @@ exports.tests = {
379382
380383
381384 testBasicCaching : function ( test ) {
382- test . expect ( 8 ) ;
385+ test . expect ( 6 ) ;
383386 doTest ( this , test , '/url1' , 'url1 contents' , false , true , 0 , test . done ) ;
384387 } ,
385388
386389 testExplicitNoCache : function ( test ) {
387- test . expect ( 8 ) ;
390+ test . expect ( 6 ) ;
388391 doTest ( this , test , '/url2' , 'url2 contents' , false , false , 0 , test . done ) ;
389392 } ,
390393
391394 testUnparseableCacheControl : function ( test ) {
392- test . expect ( 8 ) ;
395+ test . expect ( 6 ) ;
393396 doTest ( this , test , '/url4' , 'url4 contents' , false , false , 0 , test . done ) ;
394397 } ,
395398
396399 testNoCache : function ( test ) {
397400 // URLs without a Cache-Control header don't get cached.
398- test . expect ( 8 ) ;
401+ test . expect ( 6 ) ;
399402 doTest ( this , test , '/url3' , 'url3 contents' , false , false , 0 , test . done ) ;
400403 } ,
401404
402405 testUnexpiredCache : function ( test ) {
403- test . expect ( 8 ) ;
406+ test . expect ( 6 ) ;
404407 // 200 is the maximum allowable age.
405408 doTest ( this , test , '/url1' , 'url1 contents' , false , true , 200 , test . done ) ;
406409 } ,
407410
408411 testExpiredCache : function ( test ) {
409- test . expect ( 8 ) ;
412+ test . expect ( 6 ) ;
410413 doTest ( this , test , '/url1' , 'url1 contents' , false , false , 201 , test . done ) ;
411414 } ,
412415
@@ -691,29 +694,27 @@ exports.tests = {
691694function doTest ( _this , test , url , contents , firstCached , secondCached , deltaT , cb ) {
692695 var count = 0 ;
693696 if ( ! deltaT ) { deltaT = 0 ; }
694- _this . cache . openReadStream ( _this . createUrl ( url ) , function ( err , stream , path ) {
697+
698+ _this . cache . getContents ( _this . createUrl ( url ) , function ( err , buffer , path ) {
695699 if ( ! firstCached ) { count ++ ; }
696- if ( ! stream ) {
697- test . ok ( err , "if stream is null there had better be an error" ) ;
700+ if ( ! buffer ) {
701+ test . ok ( err , "if buffer is null there had better be an error" ) ;
698702 return cb ( ) ;
699703 }
700- test . ok ( stream instanceof fs . ReadStream , "stream should be an fs.ReadStream" ) ;
701704 test . ok ( fs . existsSync ( path ) ) ;
702- catStream ( stream , function ( contents ) {
703- test . equal ( contents . toString ( 'utf8' ) , contents ) ;
705+
706+ test . equal ( buffer . toString ( 'utf8' ) , contents ) ;
707+ test . equal ( _this . serverUrls [ url ] . fetchCount , count ) ;
708+ _this . nowSeconds += deltaT ;
709+ _this . cache . reset ( ) ;
710+
711+ _this . cache . getContents ( _this . createUrl ( url ) , function ( err , buffer , path ) {
712+ if ( ! secondCached ) { count ++ ; }
713+
714+ test . ok ( fs . existsSync ( path ) ) ;
704715 test . equal ( _this . serverUrls [ url ] . fetchCount , count ) ;
705- _this . nowSeconds += deltaT ;
706- _this . cache . reset ( ) ;
707- _this . cache . openReadStream ( _this . createUrl ( url ) , function ( err , stream , path ) {
708- if ( ! secondCached ) { count ++ ; }
709- test . ok ( stream instanceof fs . ReadStream , "stream should be an fs.ReadStream" ) ;
710- test . ok ( fs . existsSync ( path ) ) ;
711- test . equal ( _this . serverUrls [ url ] . fetchCount , count ) ;
712- catStream ( stream , function ( contents ) {
713- test . equal ( contents . toString ( 'utf8' ) , contents ) ;
714- cb ( ) ;
715- } ) ;
716- } ) ;
716+ test . equal ( buffer . toString ( 'utf8' ) , contents ) ;
717+ cb ( ) ;
717718 } ) ;
718719 } ) ;
719720
0 commit comments