Skip to content

Commit

Permalink
Merge pull request #156 from alexindigo/master
Browse files Browse the repository at this point in the history
Housecleaning. Added test coverage.
  • Loading branch information
alexindigo committed Dec 30, 2015
2 parents 1494fa3 + f573321 commit 7afd252
Show file tree
Hide file tree
Showing 22 changed files with 226 additions and 53 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.iml
*.sublime-*
*.un~
.idea
sftp-config.json
node_modules/
test/tmp/
51 changes: 41 additions & 10 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,33 +1,64 @@
{
"env": {
"node": true,
"browser": false
},
"rules": {
"indent": [2, 2],
// 2-space indentation
"indent": [2, 2, {"SwitchCase": 1}],
// Require strings to use single quotes
"quotes": [2, "single"],
// Allow only unix line-endings
"linebreak-style": [2, "unix"],
// Always require semicolons
"semi": [2, "always"],
"curly": [2, "all"],
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
// Require curly braces for multi-line control statements
"curly": [2, "multi-line"],
// Always handle callback error cases
"handle-callback-err": [2, "^err"],
// Require JSDoc for all function definitions
"valid-jsdoc": [2, {
"requireReturn": false,
"requireReturnDescription": false,
"prefer": {
"return": "returns"
}
}],
"require-jsdoc": [2, {
"require": {
"FunctionDeclaration": true
}
}],
"no-redeclare": [2, { "builtinGlobals": true }],
"no-shadow": [2, { "builtinGlobals": true, "hoist": "all" }],
// Disallow using variables before they've been defined
// functions are ok
"no-use-before-define": [2, "nofunc"],
"no-shadow-restricted-names": 2,
"no-extra-semi": 2,
// Disallow unused variables
"no-unused-vars": 2,
"no-undef": 2,
// Use if () { }
// ^ space
"space-after-keywords": [2, "always"],
// Use if () { }
// ^ space
"space-before-blocks": [2, "always"],
// eslint can't handle this, so the check is disabled.
"key-spacing": 0,
"strict": 0,
"no-underscore-dangle": 0,
"no-use-before-define": 0,
// Do not force dot-notation
"dot-notation": 0,
"eol-last": 0,
"no-new": 0,
"semi-spacing": 0,
// Allow multi spaces around operators since they are
// used for alignment. This is not consistent in the
// code.
"no-multi-spaces": 0,
"eqeqeq": 0,
"no-mixed-requires": 0
},
"env": {
"node": true,
"browser": true
"no-mixed-requires": 0,
"no-console": 0
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
*.iml
*.log
*.sublime-*
*.un~

.idea

sftp-config.json

coverage/
node_modules/
test/tmp/
17 changes: 17 additions & 0 deletions .istanbul.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
verbose: false
instrumentation:
root: .
extensions:
- .js
default-excludes: true
excludes: ['browser.js']
reporting:
print: summary
reports:
- lcov
dir: ./coverage
watermarks:
statements: [80, 95]
lines: [80, 95]
functions: [80, 95]
branches: [80, 95]
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
*.iml
*.log
*.sublime-*
*.un~

.idea
.gitignore
.istanbul.yml
.npmignore
.travis.yml

Makefile
sftp-config.json

coverage/
node_modules/
test/
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ node_js:
- "0.10"
- "0.12"
- "iojs"
- "4.2"
- "stable"
after_script:
- "cat coverage/lcov.info | ./node_modules/.bin/coveralls"
notifications:
webhooks:
urls:
Expand Down
8 changes: 6 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# Form-Data [![NPM Module](https://img.shields.io/npm/v/form-data.svg)](https://www.npmjs.com/package/form-data) [![Join the chat at https://gitter.im/form-data/form-data](http://form-data.github.io/images/gitterbadge.svg)](https://gitter.im/form-data/form-data)

[![Codacy Badge](https://img.shields.io/codacy/43ece80331c246179695e41f81eeffe2.svg)](https://www.codacy.com/app/form-data/form-data) [![Build Status](https://img.shields.io/travis/form-data/form-data/master.svg)](https://travis-ci.org/form-data/form-data) [![Dependency Status](https://img.shields.io/david/form-data/form-data.svg)](https://david-dm.org/form-data/form-data)

A library to create readable ```"multipart/form-data"``` streams. Can be used to submit forms and file uploads to other web applications.

The API of this library is inspired by the [XMLHttpRequest-2 FormData Interface][xhr2-fd].

[xhr2-fd]: http://dev.w3.org/2006/webapi/XMLHttpRequest-2/Overview.html#the-formdata-interface
[streams2-thing]: http://nodejs.org/api/stream.html#stream_compatibility_with_older_node_versions

[![Build Status](https://img.shields.io/travis/form-data/form-data/master.svg)](https://travis-ci.org/form-data/form-data) [![Coverage Status](https://coveralls.io/repos/form-data/form-data/badge.svg?branch=master&service=github)](https://coveralls.io/github/form-data/form-data?branch=master) [![Dependency Status](https://img.shields.io/david/form-data/form-data.svg)](https://david-dm.org/form-data/form-data)

<!--
[![bitHound Overall Score](https://www.bithound.io/github/form-data/form-data/badges/score.svg)](https://www.bithound.io/github/form-data/form-data) [![Codacy Badge](https://img.shields.io/codacy/43ece80331c246179695e41f81eeffe2.svg)](https://www.codacy.com/app/form-data/form-data)
-->

## Install

```
Expand Down
3 changes: 2 additions & 1 deletion lib/browser.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
module.exports = FormData;
/* eslint-env browser */
module.exports = FormData;
18 changes: 14 additions & 4 deletions lib/form_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@ var mime = require('mime-types');
var async = require('async');
var populate = require('./populate.js');

// Public API
module.exports = FormData;

// make it a Stream
util.inherits(FormData, CombinedStream);

/**
* Create readable "multipart/form-data" streams.
* Can be used to submit forms
* and file uploads to other web applications.
*
* @constructor
*/
function FormData() {
if (!(this instanceof FormData)) {
throw new TypeError('Failed to construct FormData: Please use the _new_ operator, this object constructor cannot be called as a function.');
Expand All @@ -21,7 +33,6 @@ function FormData() {

CombinedStream.call(this);
}
util.inherits(FormData, CombinedStream);

FormData.LINE_BREAK = '\r\n';
FormData.DEFAULT_CONTENT_TYPE = 'application/octet-stream';
Expand Down Expand Up @@ -340,9 +351,8 @@ FormData.prototype.getLength = function(cb) {
FormData.prototype.submit = function(params, cb) {
var request
, options
, defaults = {
method : 'post'
};
, defaults = {method: 'post'}
;

// parse provided url if it's string
// or treat it as options object
Expand Down
21 changes: 16 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
"main": "./lib/form_data",
"browser": "./lib/browser",
"scripts": {
"test": "./test/run.js",
"lint": "eslint lib/*.js test/*.js test/**/*.js"
"pretest": "rimraf coverage test/tmp",
"test": "istanbul cover --report none test/run.js && istanbul report",
"lint": "eslint lib/*.js test/*.js test/**/*.js",
"predebug": "rimraf coverage test/tmp",
"debug": "verbose=1 ./test/run.js",
"check": "istanbul check-coverage coverage/coverage*.json",
"coverage": "codeclimate-test-reporter < ./coverage/lcov.info; codacy-coverage < ./coverage/lcov.info; true"
},
"pre-commit": [
"lint",
"test"
"test",
"check"
],
"engines": {
"node": ">= 0.10"
Expand All @@ -27,11 +33,16 @@
},
"license": "MIT",
"devDependencies": {
"eslint": "^0.24.1",
"codacy-coverage": "^1.1.3",
"codeclimate-test-reporter": "^0.1.1",
"coveralls": "^2.11.6",
"eslint": "^1.10.3",
"fake": "^0.2.2",
"far": "^0.0.7",
"formidable": "^1.0.17",
"istanbul": "^0.4.1",
"pre-commit": "^1.0.10",
"request": "^2.60.0"
"request": "^2.60.0",
"rimraf": "^2.5.0"
}
}
10 changes: 5 additions & 5 deletions test/integration/test-custom-content-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ var FIELDS = {
value: 'my_value',
expectedType: 'image/png',
options: {
contentType: 'image/png'
contentType: 'image/png'
}
},
'default_type': {
expectedType: FormData.DEFAULT_CONTENT_TYPE,
value: function(){ return new Buffer([1, 2, 3]); }
value: function() { return new Buffer([1, 2, 3]); }
},
'implicit_type': {
expectedType: mime.lookup(common.dir.fixture + '/unicycle.jpg'),
value: function(){ return fs.createReadStream(common.dir.fixture + '/unicycle.jpg'); }
value: function() { return fs.createReadStream(common.dir.fixture + '/unicycle.jpg'); }
},
'overridden_type': {
expectedType: 'image/png',
options: {
contentType: 'image/png'
contentType: 'image/png'
},
value: function(){ return fs.createReadStream(common.dir.fixture + '/unicycle.jpg'); }
value: function() { return fs.createReadStream(common.dir.fixture + '/unicycle.jpg'); }
}
};
var fieldsPassed = false;
Expand Down
15 changes: 7 additions & 8 deletions test/integration/test-http-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ var remoteFile = 'http://localhost:' + common.staticPort + '/unicycle.jpg';
var FIELDS;
var server;

var parsedUrl = parseUrl(remoteFile)
, options = {
method: 'get',
port: parsedUrl.port || 80,
path: parsedUrl.pathname,
host: parsedUrl.hostname
}
;
var parsedUrl = parseUrl(remoteFile);
var options = {
method: 'get',
port: parsedUrl.port || 80,
path: parsedUrl.pathname,
host: parsedUrl.hostname
};

// request static file
http.request(options, function(response) {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/test-last_boundary-line_break.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ var assert = common.assert;
var FormData = require(common.dir.lib + '/form_data');
var server;

/**
* Test submission of proper line ending
*/
function submitForm() {

var form = new FormData();
Expand Down
6 changes: 3 additions & 3 deletions test/integration/test-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ var FIELDS = {
},
'my_buffer': {
type: FormData.DEFAULT_CONTENT_TYPE,
value: function(){ return new Buffer([1, 2, 3]); }
value: function() { return new Buffer([1, 2, 3]); }
},
'my_file': {
type: mime.lookup(common.dir.fixture + '/unicycle.jpg'),
value: function(){ return fs.createReadStream(common.dir.fixture + '/unicycle.jpg'); }
value: function() { return fs.createReadStream(common.dir.fixture + '/unicycle.jpg'); }
},
'remote_file': {
type: mime.lookup(common.dir.fixture + '/unicycle.jpg'),
value: function(){ return request(remoteFile); }
value: function() { return request(remoteFile); }
}
};
var fieldsPassed = Object.keys(FIELDS).length;
Expand Down
5 changes: 5 additions & 0 deletions test/integration/test-ranged-filestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ var testSubjects = {
}
};

/**
* Accumulates read data size
*
* @param {string} data - chunk of read data
*/
function readSizeAccumulator(data) {
this.readSize += data.length;
}
Expand Down
25 changes: 24 additions & 1 deletion test/integration/test-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,36 @@ var http = require('http');
var path = require('path');
var mime = require('mime-types');
var request = require('request');
var FormData = require(common.dir.lib + '/form_data');
var fs = require('fs');
var IncomingForm = require('formidable').IncomingForm;

var fileName = common.dir.fixture + '/unicycle.jpg';
var myFile = function(){ return fs.createReadStream(fileName); };
var myFile = function() { return fs.createReadStream(fileName); };
var numItems = 5;

// Make request to use our FormData
request.prototype.form = function (form) {
var self = this;
if (form) {
if (!/^application\/x-www-form-urlencoded\b/.test(self.getHeader('content-type'))) {
self.setHeader('content-type', 'application/x-www-form-urlencoded');
}
self.body = (typeof form === 'string')
? self._qs.rfc3986(form.toString('utf8'))
: self._qs.stringify(form).toString('utf8');
return self;
}
// create form-data object
self._form = new FormData();
self._form.on('error', function(err) {
err.message = 'form-data: ' + err.message;
self.emit('error', err);
self.abort();
});
return self._form;
};

var server = http.createServer(function(req, res) {

var form = new IncomingForm({uploadDir: common.dir.tmp});
Expand Down
Loading

0 comments on commit 7afd252

Please sign in to comment.