Permalink
Browse files

Don't forward etag response header when uploading stream. Close #11.

  • Loading branch information...
1 parent 4916458 commit 4b22b0ee47af8ec6613912f3231c20403ecee3dd @rossj committed Mar 19, 2013
Showing with 67 additions and 6 deletions.
  1. +15 −3 lib/rackit.js
  2. +52 −3 test/rackit.test.js
View
18 lib/rackit.js
@@ -476,7 +476,7 @@ Rackit.prototype._getContainer = function (cb) {
/**
* Adds a file!
* @param {string|ReadableStream} source - Either a string (local path) or ReadableStream representing the file to add
- * @param {{type: string, filename: string, meta: Object, headers: Object.<string, string>}} options - Additonal options
+ * @param {{type: string, filename: string, meta: Object, headers: Object.<string, string>}|function(?Error, string=)} options - Additonal options
* @param {function(?Error, string=)} cb - Callback, returns error or the cloud path
*/
Rackit.prototype.add = function (source, options, cb) {
@@ -538,7 +538,7 @@ Rackit.prototype.add = function (source, options, cb) {
// The source is a stream, so it might already be a reqeust with a content-type header.
// In this case, the content-type will be forwarded automatically
if (source.headers && source.headers['content-type']) {
- cb();
+ cb(null, source.headers['content-type']);
} else {
cb(new Error('Unable to determine content-type. You must specify the type for file streams.'));
}
@@ -558,10 +558,15 @@ Rackit.prototype.add = function (source, options, cb) {
// Generate file id
var id = options.filename || utils.uid(24);
+ //
+ // Generate the headers to be send to Rackspace
+ //
var headers = {};
if (fromFile) {
- headers['content-length'] = results.stats.size;
+ headers['content-length'] = '' + results.stats.size;
+ } else if (source.headers && source.headers['content-length']) {
+ headers['content-length'] = source.headers['content-length'];
} else {
headers['transfer-encoding'] = 'chunked';
}
@@ -584,6 +589,9 @@ Rackit.prototype.add = function (source, options, cb) {
}
}
+ //
+ // Generate the cloud request options
+ //
var cloudPath = results.container + '/' + id;
var reqOptions = {
method : 'PUT',
@@ -607,6 +615,10 @@ Rackit.prototype.add = function (source, options, cb) {
// Open a file stream, and pipe it to the request.
if (fromFile)
source = fs.createReadStream(source);
+ else if (source.headers) {
+ // We want to remove any headers from the source stream so they don't clobber our own headers.
+ delete source.headers;
+ }
source.resume();
source.pipe(req);
View
55 test/rackit.test.js
@@ -217,12 +217,13 @@ var superNock = {
.filteringPath(new RegExp(container + '/.*', 'g'), container + '/filename')
.put(path, data)
.matchHeader('X-Auth-Token', mockOptions.token)
- .matchHeader('Content-Type', type);
+ .matchHeader('Content-Type', type)
+ .matchHeader('ETag', undefined);
if (chunked) {
scope.matchHeader('Transfer-Encoding', 'chunked');
} else {
- scope.matchHeader('Content-Length', Buffer.byteLength(data));
+ scope.matchHeader('Content-Length', '' + Buffer.byteLength(data));
}
scope = scope.reply(201);
@@ -612,7 +613,7 @@ describe('Rackit', function () {
var stream = fs.createReadStream(testFile.path);
superNock.add(container, testFile.data, testFile.type, true);
- rackit.add(fs.createReadStream(testFile.path), {type: testFile.type}, assertAdd(container, count + 1, cb));
+ rackit.add(stream, {type: testFile.type}, assertAdd(container, count + 1, cb));
});
it('should successfuly upload a ServerRequest stream with forwarded type', function (cb) {
@@ -638,6 +639,54 @@ describe('Rackit', function () {
fs.createReadStream(testFile.path).pipe(req);
});
+ it('should successfuly upload a ServerRequest stream with forwarded length', function (cb) {
+ var container = 'empty0';
+ var count = getFreeContainerCount(container);
+
+ superNock.add(container, testFile.data, testFile.type, false);
+
+ // Set up the small server that will forward the request to Rackit
+ var server = http.createServer(function(req, res) {
+ rackit.add(req, assertAdd(container, count + 1, cb));
+ server.close();
+ }).listen(7357);
+
+ // Create the request to the small server above
+ var req = request.put({
+ uri : 'http://localhost:7357',
+ headers: {
+ 'content-type': 'text/plain',
+ 'content-length': '' + Buffer.byteLength(testFile.data)
+ }
+ });
+
+ fs.createReadStream(testFile.path).pipe(req);
+ });
+
+ it('should not forward the etag header of a ServerRequest stream', function (cb) {
+ var container = 'empty0';
+ var count = getFreeContainerCount(container);
+
+ superNock.add(container, testFile.data, testFile.type, true);
+
+ // Set up the small server that will forward the request to Rackit
+ var server = http.createServer(function(req, res) {
+ rackit.add(req, assertAdd(container, count + 1, cb));
+ server.close();
+ }).listen(7357);
+
+ // Create the request to the small server above
+ var req = request.put({
+ uri : 'http://localhost:7357',
+ headers: {
+ 'content-type': 'text/plain',
+ 'etag' : 'somehashvalue234'
+ }
+ });
+
+ fs.createReadStream(testFile.path).pipe(req);
+ });
+
it('should successfuly upload a ServerRequest stream with explicit type', function (cb) {
var container = 'empty0';
var count = getFreeContainerCount(container);

0 comments on commit 4b22b0e

Please sign in to comment.