From 16ef51b4e423f99a2366e5b38e7387d996a411f7 Mon Sep 17 00:00:00 2001 From: Rob DiMarco Date: Tue, 19 May 2015 11:21:15 -0700 Subject: [PATCH] Emit response event from file#createReadStream() --- lib/storage/file.js | 14 ++++++----- test/storage/file.js | 59 +++++++++++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/lib/storage/file.js b/lib/storage/file.js index d64aa09c725..7813611dd01 100644 --- a/lib/storage/file.js +++ b/lib/storage/file.js @@ -464,6 +464,8 @@ File.prototype.createReadStream = function(options) { request(authorizedReqOpts) .on('error', done) + .on('response', throughStream.emit.bind(throughStream, 'response')) + .on('data', function(chunk) { if (crc32c) { localCrcHash = crc.calculate(chunk, localCrcHash); @@ -1025,7 +1027,7 @@ File.prototype.getSignedPolicy = function(options, callback) { * @param {string=} options.responseDisposition - The * response-content-disposition parameter (http://goo.gl/yMWxQV) of the * signed url. - * @param {string=} options.responseType - The response-content-type parameter + * @param {string=} options.responseType - The response-content-type parameter * of the signed url. * @param {function=} callback - The callback function. * @@ -1071,20 +1073,20 @@ File.prototype.getSignedUrl = function(options, callback) { var responseContentType = ''; if (util.is(options.responseType, 'string')) { - responseContentType = - '&response-content-type=' + + responseContentType = + '&response-content-type=' + encodeURIComponent(options.responseType); } var responseContentDisposition = ''; if (util.is(options.promptSaveAs, 'string')) { responseContentDisposition = - '&response-content-disposition=attachment; filename="' + + '&response-content-disposition=attachment; filename="' + encodeURIComponent(options.promptSaveAs) + '"'; } if (util.is(options.responseDisposition, 'string')) { - responseContentDisposition = - '&response-content-disposition=' + + responseContentDisposition = + '&response-content-disposition=' + encodeURIComponent(options.responseDisposition); } diff --git a/test/storage/file.js b/test/storage/file.js index 9913f3c20e7..e67c05a5bd6 100644 --- a/test/storage/file.js +++ b/test/storage/file.js @@ -370,6 +370,30 @@ describe('File', function() { }); describe('createReadStream', function() { + + function getFakeRequest(data, fakeResponse) { + function FakeRequest(req) { + if (!(this instanceof FakeRequest)) { + return new FakeRequest(req); + } + + var that = this; + + stream.Readable.call(this); + this._read = function() { + this.push(data); + this.push(null); + }; + + setImmediate(function() { + that.emit('response', fakeResponse); + that.emit('complete', fakeResponse); + }); + } + nodeutil.inherits(FakeRequest, stream.Readable); + return FakeRequest; + } + it('should create an authorized request', function(done) { var expectedPath = util.format('https://{b}.storage.googleapis.com/{o}', { b: file.bucket.name, @@ -398,6 +422,19 @@ describe('File', function() { }); }); + it('should emit response event from request', function(done) { + var response = { + headers: { 'x-goog-hash': 'md5=fakefakefake' } + }; + request_Override = getFakeRequest('body', response); + + file.createReadStream({ validation: false }) + .on('response', function(res) { + assert.deepEqual(response, res); + done(); + }) + }); + it('should get readable stream from request', function(done) { var fakeRequest = { a: 'b', c: 'd' }; @@ -442,28 +479,6 @@ describe('File', function() { } }; - function getFakeRequest(data, fakeResponse) { - function FakeRequest(req) { - if (!(this instanceof FakeRequest)) { - return new FakeRequest(req); - } - - var that = this; - - stream.Readable.call(this); - this._read = function() { - this.push(data); - this.push(null); - }; - - setImmediate(function() { - that.emit('complete', fakeResponse); - }); - } - nodeutil.inherits(FakeRequest, stream.Readable); - return FakeRequest; - } - beforeEach(function() { file.metadata.mediaLink = 'http://uri';