Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit response event from file#createReadStream() #610

Merged
merged 3 commits into from
May 20, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}

Expand Down
59 changes: 37 additions & 22 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

});
}
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,
Expand Down Expand Up @@ -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' };

Expand Down Expand Up @@ -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';

Expand Down