Skip to content
Browse files

add field name to .attach()

wtf... this was pretty useless without this,
not sure what the hell i was thinking haha
  • Loading branch information...
1 parent c8d6d5b commit 9d62aab84154f8a4589f9162961d4e84c3bf05ac @tj tj committed Oct 1, 2012
Showing with 31 additions and 34 deletions.
  1. +8 −13 lib/node/index.js
  2. +5 −3 lib/node/part.js
  3. +18 −18 test/node/multipart.js
View
21 lib/node/index.js
@@ -145,17 +145,19 @@ Request.prototype.__proto__ = Stream.prototype;
* Queue the given `file` as an attachment
* with optional `filename`.
*
+ * @param {String} field
* @param {String} file
* @param {String} filename
* @return {Request} for chaining
* @api public
*/
-Request.prototype.attach = function(file, filename){
+Request.prototype.attach = function(field, file, filename){
this.attachments.push({
- name: file
- , part: new Part(this)
- , filename: filename
+ field: field,
+ path: file,
+ part: new Part(this),
+ filename: filename || file
});
return this;
};
@@ -722,15 +724,8 @@ Request.prototype.writeAttachments = function(){
return req.end();
}
- // custom filename
- if (file.filename) {
- file.part.type(file.name);
- file.part.set('Content-Disposition', 'attachment; filename="' + file.filename + '"');
- } else {
- file.part.filename(file.name);
- }
-
- var stream = fs.createReadStream(file.name);
+ file.part.attachment(file.field, file.filename);
+ var stream = fs.createReadStream(file.path);
// TODO: pipe
// TODO: handle errors
View
8 lib/node/part.js
@@ -111,16 +111,18 @@ Part.prototype.name = function(name){
};
/**
- * Set _Content-Disposition_ header field to _attachment_ with `filename`.
+ * Set _Content-Disposition_ header field to _attachment_ with `filename`
+ * and field `name`.
*
+ * @param {String} name
* @param {String} filename
* @return {Part}
* @api public
*/
-Part.prototype.filename = function(filename){
+Part.prototype.attachment = function(name, filename){
this.type(filename);
- this.set('Content-Disposition', 'attachment; filename="' + basename(filename) + '"');
+ this.set('Content-Disposition', 'attachment; name="' + name + '"; filename="' + basename(filename) + '"');
return this;
};
View
36 test/node/multipart.js
@@ -60,34 +60,34 @@ describe('Request', function(){
var req = request.post('http://localhost:3005/echo');
req.field('name', 'Tobi');
- req.attach('test/node/fixtures/user.html', 'document');
+ req.attach('document', 'test/node/fixtures/user.html');
req.field('species', 'ferret');
req.end(function(res){
res.body.name.should.equal('Tobi');
res.body.species.should.equal('ferret');
var html = res.files.document;
- html.name.should.equal('document');
+ html.name.should.equal('user.html');
html.type.should.equal('text/html');
read(html.path).should.equal('<h1>name</h1>');
done();
})
})
})
- describe('#attach(file)', function(){
+ describe('#attach(name, path)', function(){
it('should attach a file', function(done){
var req = request.post('http://localhost:3005/echo');
- req.attach('test/node/fixtures/user.html');
- req.attach('test/node/fixtures/user.json');
- req.attach('test/node/fixtures/user.txt');
+ req.attach('one', 'test/node/fixtures/user.html');
+ req.attach('two', 'test/node/fixtures/user.json');
+ req.attach('three', 'test/node/fixtures/user.txt');
req.end(function(res){
- var html = res.files['user.html'];
- var json = res.files['user.json'];
- var text = res.files['user.txt'];
+ var html = res.files.one;
+ var json = res.files.two
+ var text = res.files.three;
html.name.should.equal('user.html');
html.type.should.equal('text/html');
@@ -109,9 +109,9 @@ describe('Request', function(){
it('should emit an error', function(done){
var req = request.post('http://localhost:3005/echo');
- req.attach('foo');
- req.attach('bar');
- req.attach('baz');
+ req.attach('name', 'foo');
+ req.attach('name2', 'bar');
+ req.attach('name3', 'baz');
req.on('error', function(err){
err.message.should.include('ENOENT');
@@ -126,14 +126,14 @@ describe('Request', function(){
})
})
- describe('#attach(file, filename)', function(){
+ describe('#attach(name, path, filename)', function(){
it('should use the custom filename', function(done){
request
.post(':3005/echo')
- .attach('test/node/fixtures/user.html', 'document')
+ .attach('document', 'test/node/fixtures/user.html', 'doc.html')
.end(function(res){
var html = res.files.document;
- html.name.should.equal('document');
+ html.name.should.equal('doc.html');
html.type.should.equal('text/html');
read(html.path).should.equal('<h1>name</h1>');
done();
@@ -232,19 +232,19 @@ describe('Part', function(){
})
})
- describe('#filename(str)', function(){
+ describe('#attachment(name, path)', function(){
it('should set Content-Disposition and Content-Type', function(done){
var req = request
.post('http://localhost:3005/echo')
.type('multipart/form-data');
req
.part()
- .filename('path/to/my.txt')
+ .attachment('file', 'path/to/my.txt')
.write('Tobi');
req.end(function(res){
- var file = res.files['my.txt'];
+ var file = res.files.file;
file.name.should.equal('my.txt');
file.type.should.equal('text/plain');
done();

0 comments on commit 9d62aab

Please sign in to comment.
Something went wrong with that request. Please try again.