Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replaces tabs with spaces

  • Loading branch information...
commit b0f7ce167e3ccdc043d4ccf51c0c88752a85c666 1 parent 23ba0ba
@phstc authored
View
4 Makefile
@@ -18,7 +18,7 @@ progress:
simple:
supervisor examples/simple/app.js
-test-unit:
- ./node_modules/.bin/mocha $(TESTS) --reporter list --compilers coffee:coffee-script
+test-unit:
+ ./node_modules/.bin/mocha $(TESTS) --reporter list --compilers coffee:coffee-script
.PHONY: test-unit test compile progress simple
View
16 README.md
@@ -14,7 +14,7 @@ or
"node-upload-progress": "latest"
}
# ...
-
+
$ npm install
##Usage
@@ -23,7 +23,7 @@ or
var app = require('http');
var uploadProgress = require('node-upload-progress');
-
+
uploadHandler = new uploadProgress.UploadHandler;
The ```uploadHandler.uploadDir``` is the path where the files will be saved. Without this configuration the files will be saved in a path based on ```process.env.TMP```.
@@ -31,7 +31,7 @@ The ```uploadHandler.uploadDir``` is the path where the files will be saved. Wit
uploadHandler.configure(function() {
this.uploadDir = __dirname + '/uploads';
});
-
+
app.createServer(function(req, res) {
if (req.url === '/upload') {
uploadHandler.upload(req, res);
@@ -49,12 +49,12 @@ Configuring the ```uploadHandler.onEnd = customOnEndHandler```, you can write yo
this.uploadDir = "" + __dirname + "/uploads";
this.onEnd = customOnEndHandler;
});
-
- function customOnEndHandler(req, res){
+
+ function customOnEndHandler(req, res){
res.writeHead 200, {'Content-Type': 'text/plain'}
res.end('Upload received');
}
-
+
app.createServer(function(req, res) {
if (req.url === '/upload') {
uploadHandler.upload(req, res);
@@ -152,7 +152,7 @@ The upload request is in progress:
$ make simple
-Then
+Then
open http://localhost:8080
@@ -160,7 +160,7 @@ Then
$ make progress
-Then
+Then
open http://localhost:8080
View
21 examples/progress/app.coffee
@@ -4,19 +4,18 @@ uploadProgress = require '../../lib/node-upload-progress'
uploadHandler = new uploadProgress.UploadHandler
uploadHandler.configure ->
- this.uploadDir = "#{__dirname}/uploads"
+ this.uploadDir = "#{__dirname}/uploads"
app.createServer((req, res) ->
- if req.url.match /\/upload\?X\-Progress\-ID=.+/
- uploadHandler.upload req, res
- else if req.url.match /\/progress\?X\-Progress\-ID=.+/
- uploadHandler.progress req, res
- else
- fs.readFile "#{__dirname}/index.html", (err, data) ->
- res.writeHead 200, {'Content-Type': 'text/html'}
- res.write data.toString()
- res.end()
+ if req.url.match /\/upload\?X\-Progress\-ID=.+/
+ uploadHandler.upload req, res
+ else if req.url.match /\/progress\?X\-Progress\-ID=.+/
+ uploadHandler.progress req, res
+ else
+ fs.readFile "#{__dirname}/index.html", (err, data) ->
+ res.writeHead 200, {'Content-Type': 'text/html'}
+ res.write data.toString()
+ res.end()
).listen(8080)
console.log 'Server running at http://localhost:8080/'
-
View
112 examples/progress/index.html
@@ -1,62 +1,62 @@
<html lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Test Upload</title>
- <meta name="author" content="Pablo Cantero <pablo@pablocantero.com>">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
- <!-- Date: 2012-06-12 -->
- <script>
- $(function(){
- var uploadIntervalID;
- $('#form_upload').submit(function(){
- // Preventing multiples clicks on upload
- clearInterval(uploadIntervalID);
- var xProgressID = guidGenerator();
- $(this).attr('action', '/upload?X-Progress-ID=' + xProgressID);
- uploadIntervalID = setInterval(function(){
- $.get('/progress?X-Progress-ID=' + xProgressID, function(data){
- if(data.status === 'done'){
- clearInterval(uploadIntervalID);
- }
- updateUploadStatus(data);
- }).error(function(){clearInterval(uploadIntervalID)});
- }, 250);
- return true;
- });
-
- function updateUploadStatus(data){
- $('#upload_percent').text(data.percent);
- $('#upload_status').text(data.status);
- $('#upload_filename').text(data.fileName);
- $('#upload_filepath').text(data.filePath);
- }
-
- // http://stackoverflow.com/a/105074/464685
- function guidGenerator() {
- return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
- }
-
- function S4() {
- return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
- }
- });
- </script>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <title>Test Upload</title>
+ <meta name="author" content="Pablo Cantero <pablo@pablocantero.com>">
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
+ <!-- Date: 2012-06-12 -->
+ <script>
+ $(function(){
+ var uploadIntervalID;
+ $('#form_upload').submit(function(){
+ // Preventing multiples clicks on upload
+ clearInterval(uploadIntervalID);
+ var xProgressID = guidGenerator();
+ $(this).attr('action', '/upload?X-Progress-ID=' + xProgressID);
+ uploadIntervalID = setInterval(function(){
+ $.get('/progress?X-Progress-ID=' + xProgressID, function(data){
+ if(data.status === 'done'){
+ clearInterval(uploadIntervalID);
+ }
+ updateUploadStatus(data);
+ }).error(function(){clearInterval(uploadIntervalID)});
+ }, 250);
+ return true;
+ });
+
+ function updateUploadStatus(data){
+ $('#upload_percent').text(data.percent);
+ $('#upload_status').text(data.status);
+ $('#upload_filename').text(data.fileName);
+ $('#upload_filepath').text(data.filePath);
+ }
+
+ // http://stackoverflow.com/a/105074/464685
+ function guidGenerator() {
+ return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
+ }
+
+ function S4() {
+ return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
+ }
+ });
+ </script>
</head>
<body>
- <h1>Super Upload</h1>
- <form action="/upload?X-Progress-ID=1" enctype="multipart/form-data" method="post" id="form_upload" target="iframe_upload">
- <p>
- <label>File</label><br/>
- <input type="file" name="upload" id="upload"><br>
- <span id="upload_percent"></span><br/>
- <span id="upload_status"></span><br/>
- <span id="upload_filename"></span><br/>
- <span id="upload_filepath"></span>
- </p>
- <p>
- <input type="submit" value="Upload">
- </p>
- </form>
- <iframe id="iframe_upload" name="iframe_upload"></iframe>
+ <h1>Super Upload</h1>
+ <form action="/upload?X-Progress-ID=1" enctype="multipart/form-data" method="post" id="form_upload" target="iframe_upload">
+ <p>
+ <label>File</label><br/>
+ <input type="file" name="upload" id="upload"><br>
+ <span id="upload_percent"></span><br/>
+ <span id="upload_status"></span><br/>
+ <span id="upload_filename"></span><br/>
+ <span id="upload_filepath"></span>
+ </p>
+ <p>
+ <input type="submit" value="Upload">
+ </p>
+ </form>
+ <iframe id="iframe_upload" name="iframe_upload"></iframe>
</body>
</html>
View
17 examples/simple/app.coffee
@@ -4,17 +4,16 @@ uploadProgress = require '../../lib/node-upload-progress'
uploadHandler = new uploadProgress.UploadHandler
uploadHandler.configure ->
- this.uploadDir = "#{__dirname}/uploads"
+ this.uploadDir = "#{__dirname}/uploads"
app.createServer((req, res) ->
- if req.url == '/upload'
- uploadHandler.upload req, res
- else
- fs.readFile "#{__dirname}/index.html", (err, data) ->
- res.writeHead 200, {'Content-Type': 'text/html'}
- res.write data.toString()
- res.end()
+ if req.url == '/upload'
+ uploadHandler.upload req, res
+ else
+ fs.readFile "#{__dirname}/index.html", (err, data) ->
+ res.writeHead 200, {'Content-Type': 'text/html'}
+ res.write data.toString()
+ res.end()
).listen(8080)
console.log 'Server running at http://localhost:8080/'
-
View
28 examples/simple/index.html
@@ -1,20 +1,20 @@
<html lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Test Upload</title>
- <meta name="author" content="Pablo Cantero <pablo@pablocantero.com>">
- <!-- Date: 2012-06-12 -->
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <title>Test Upload</title>
+ <meta name="author" content="Pablo Cantero <pablo@pablocantero.com>">
+ <!-- Date: 2012-06-12 -->
</head>
<body>
- <h1>Super Upload</h1>
- <form action="/upload" enctype="multipart/form-data" method="post" id="form_upload">
- <p>
- <label>File</label><br/>
- <input type="file" name="upload" id="upload">
- </p>
- <p>
- <input type="submit" value="Upload">
- </p>
- </form>
+ <h1>Super Upload</h1>
+ <form action="/upload" enctype="multipart/form-data" method="post" id="form_upload">
+ <p>
+ <label>File</label><br/>
+ <input type="file" name="upload" id="upload">
+ </p>
+ <p>
+ <input type="submit" value="Upload">
+ </p>
+ </form>
</body>
</html>
View
84 lib/node-upload-progress.coffee
@@ -19,49 +19,49 @@ fs = require 'fs'
# new Object({ 'state' : 'uploading', 'received' : <size_received>, 'size' : <total_size>}) }
class UploadHandler
- constructor: (@uploadDir=null, @onEnd=null) ->
- @uploads = new Uploads
-
- configure: (func) ->
- func.call @
-
- formOnFile: (upload, field, file) ->
- upload.file = file
- if @uploadDir
- fs.rename file.path, "#{@uploadDir}/#{file.name}"
- file.path = "#{@uploadDir}/#{file.name}"
-
- formOnProgress: (upload, bytesReceived, bytesExpected) ->
- upload.updateProgress bytesReceived, bytesExpected
-
- _onEnd: (req, res) ->
- res.writeHead 200, 'Content-type': 'text/plain'
- res.end 'upload received'
+ constructor: (@uploadDir=null, @onEnd=null) ->
+ @uploads = new Uploads
- upload: (req, res) ->
- query = url.parse(req.url, true).query
- form = new formidable.IncomingForm()
-
- @uploads.add query['X-Progress-ID']
-
- form.parse req, (err, fields, files) =>
- @uploads.remove query['X-Progress-ID']
- (@onEnd || @_onEnd)(req, res)
-
- form.on 'file', (field, file) =>
- @formOnFile @uploads.get(query['X-Progress-ID']), field, file
-
- form.addListener 'progress' , (bytesReceived, bytesExpected) =>
- @formOnProgress @uploads.get(query['X-Progress-ID']), bytesReceived, bytesExpected
+ configure: (func) ->
+ func.call @
- progress: (req, res) ->
- query = url.parse(req.url, true).query
- upload = @uploads.get query['X-Progress-ID']
- if upload
- res.writeHead 200, 'Content-type': 'application/json'
- res.end upload.toJSON()
- else
- res.writeHead 404, 'Content-type': 'text/plain'
- res.end 'not found'
+ formOnFile: (upload, field, file) ->
+ upload.file = file
+ if @uploadDir
+ fs.rename file.path, "#{@uploadDir}/#{file.name}"
+ file.path = "#{@uploadDir}/#{file.name}"
+
+ formOnProgress: (upload, bytesReceived, bytesExpected) ->
+ upload.updateProgress bytesReceived, bytesExpected
+
+ _onEnd: (req, res) ->
+ res.writeHead 200, 'Content-type': 'text/plain'
+ res.end 'upload received'
+
+ upload: (req, res) ->
+ query = url.parse(req.url, true).query
+ form = new formidable.IncomingForm()
+
+ @uploads.add query['X-Progress-ID']
+
+ form.parse req, (err, fields, files) =>
+ @uploads.remove query['X-Progress-ID']
+ (@onEnd || @_onEnd)(req, res)
+
+ form.on 'file', (field, file) =>
+ @formOnFile @uploads.get(query['X-Progress-ID']), field, file
+
+ form.addListener 'progress' , (bytesReceived, bytesExpected) =>
+ @formOnProgress @uploads.get(query['X-Progress-ID']), bytesReceived, bytesExpected
+
+ progress: (req, res) ->
+ query = url.parse(req.url, true).query
+ upload = @uploads.get query['X-Progress-ID']
+ if upload
+ res.writeHead 200, 'Content-type': 'application/json'
+ res.end upload.toJSON()
+ else
+ res.writeHead 404, 'Content-type': 'text/plain'
+ res.end 'not found'
module.exports.UploadHandler = UploadHandler
View
62 lib/upload.coffee
@@ -1,34 +1,34 @@
class Upload
- @file = null
-
- constructor: (@bytesReceived=0, @bytesExpected=0) ->
-
- isDone: ->
- @bytesReceived == @bytesExpected
-
- isUploading: ->
- @bytesReceived < @bytesExpected
-
- toJSON: ->
- JSON.stringify
- bytesReceived: @bytesReceived
- bytesExpected: @bytesExpected
- percent: @percent()
- status: @status()
- fileName: @file.name if @file
- filePath: @file.path if @file
-
- status: ->
- return 'done' if @isDone() and @file
- return 'uploading' if @isUploading()
- 'starting'
-
- percent: ->
- return 0 if @bytesExpected == 0
- parseInt (@bytesReceived * 100) / @bytesExpected, 10
-
- updateProgress: (bytesReceived, bytesExpected) ->
- @bytesReceived = bytesReceived
- @bytesExpected = bytesExpected
+ @file = null
+
+ constructor: (@bytesReceived=0, @bytesExpected=0) ->
+
+ isDone: ->
+ @bytesReceived == @bytesExpected
+
+ isUploading: ->
+ @bytesReceived < @bytesExpected
+
+ toJSON: ->
+ JSON.stringify
+ bytesReceived: @bytesReceived
+ bytesExpected: @bytesExpected
+ percent: @percent()
+ status: @status()
+ fileName: @file.name if @file
+ filePath: @file.path if @file
+
+ status: ->
+ return 'done' if @isDone() and @file
+ return 'uploading' if @isUploading()
+ 'starting'
+
+ percent: ->
+ return 0 if @bytesExpected == 0
+ parseInt (@bytesReceived * 100) / @bytesExpected, 10
+
+ updateProgress: (bytesReceived, bytesExpected) ->
+ @bytesReceived = bytesReceived
+ @bytesExpected = bytesExpected
module.exports = Upload
View
22 lib/uploads.coffee
@@ -1,19 +1,19 @@
Upload = require './upload'
class Uploads
- constructor: ->
- @uploads = []
+ constructor: ->
+ @uploads = []
- add: (uuid, upload=(new Upload)) ->
- @uploads[uuid] = upload
+ add: (uuid, upload=(new Upload)) ->
+ @uploads[uuid] = upload
- get: (uuid) ->
- @uploads[uuid]
+ get: (uuid) ->
+ @uploads[uuid]
- remove: (uuid, millisec=50000) ->
- uploads = @uploads
- setTimeout ->
- delete uploads[uuid]
- , millisec
+ remove: (uuid, millisec=50000) ->
+ uploads = @uploads
+ setTimeout ->
+ delete uploads[uuid]
+ , millisec
module.exports = Uploads
View
14 package.json
@@ -6,18 +6,18 @@
"repository": {"type": "git", "url": "https://github.com/phstc/node-upload-progress"},
"author": "Pablo Cantero <pablo@pablocantero.com>",
"dependencies": {
- "formidable": "1.0.9"
- },
+ "formidable": "1.0.9"
+ },
"devDependencies": {
- "mocha": "1.0.1"
+ "mocha": "1.0.1"
, "should": "0.6.0"
, "coffee-script": "1.2.0"
- , "superagent": "0.5.0"
- , "supervisor": "0.3.5"
- },
+ , "superagent": "0.5.0"
+ , "supervisor": "0.3.5"
+ },
"main": "./index.js",
"scripts": {
- "test": "make test"
+ "test": "make test"
},
"engines": {"node": ">= 0.6.0"}
}
View
132 test/integration/node-upload-progress_test.coffee
@@ -1,73 +1,73 @@
app = require 'http'
request = require 'superagent'
uploadProgress = require '../../lib/node-upload-progress'
-
-describe 'app', ->
- describe 'simple upload', ->
- $ = {}
- before ->
- uploadHandler = new uploadProgress.UploadHandler
- $.server = app.createServer (req, res) ->
- uploadHandler.upload req, res
- $.server.listen(8080)
-
- after ->
- $.server.close()
-
- it 'should upload a file', (done) ->
- req = request.post 'http://localhost:8080/upload'
- part = req.part()
- part.set 'Content-Type', 'text/plain'
- part.set 'Content-Disposition', 'attachment; filename="hello.txt"'
- part.write 'hello world'
- req.end (res) ->
- res.should.have.status 200
- res.text.should.equal 'upload received'
- done()
-
- describe 'simple upload with onEnd', ->
- $ = {}
- before ->
- uploadHandler = new uploadProgress.UploadHandler
- uploadHandler.configure ->
- this.onEnd = (req, res)->
- res.writeHead 200, 'Content-type': 'text/plain'
- res.end 'awesome upload received'
- $.server = app.createServer (req, res) ->
- uploadHandler.upload req, res
- $.server.listen(8080)
-
- after ->
- $.server.close()
- it 'should response awesome upload received', (done) ->
- req = request.post 'http://localhost:8080/upload'
- part = req.part()
- part.set 'Content-Type', 'text/plain'
- part.set 'Content-Disposition', 'attachment; filename="hello.txt"'
- part.write 'hello world'
- req.end (res) ->
- res.should.have.status 200
- res.text.should.equal 'awesome upload received'
- done()
-
- describe 'progress', ->
- $ = {}
- before ->
- # uploadHandler = new uploadProgress.UploadHandler
- # $.server = app.createServer (req, res) ->
- # if req.url.match /\/upload\?X\-Progress\-ID=.+/
- # uploadHandler.upload req, res
- # else if req.url.match /\/progress\?X\-Progress\-ID=.+/
- # uploadHandler.progress req, res
- # $.server.listen(8080)
+describe 'app', ->
+ describe 'simple upload', ->
+ $ = {}
+ before ->
+ uploadHandler = new uploadProgress.UploadHandler
+ $.server = app.createServer (req, res) ->
+ uploadHandler.upload req, res
+ $.server.listen(8080)
- after ->
- # $.server.close()
+ after ->
+ $.server.close()
- it 'should return starting if not found', ->
- # req = request.get 'http://localhost:8080/progress?X-Progress-ID=1'
- # req.end (res) ->
- # res.should.have.status 404
- # res.text.should.equal 'not found'
+ it 'should upload a file', (done) ->
+ req = request.post 'http://localhost:8080/upload'
+ part = req.part()
+ part.set 'Content-Type', 'text/plain'
+ part.set 'Content-Disposition', 'attachment; filename="hello.txt"'
+ part.write 'hello world'
+ req.end (res) ->
+ res.should.have.status 200
+ res.text.should.equal 'upload received'
+ done()
+
+ describe 'simple upload with onEnd', ->
+ $ = {}
+ before ->
+ uploadHandler = new uploadProgress.UploadHandler
+ uploadHandler.configure ->
+ this.onEnd = (req, res)->
+ res.writeHead 200, 'Content-type': 'text/plain'
+ res.end 'awesome upload received'
+ $.server = app.createServer (req, res) ->
+ uploadHandler.upload req, res
+ $.server.listen(8080)
+
+ after ->
+ $.server.close()
+
+ it 'should response awesome upload received', (done) ->
+ req = request.post 'http://localhost:8080/upload'
+ part = req.part()
+ part.set 'Content-Type', 'text/plain'
+ part.set 'Content-Disposition', 'attachment; filename="hello.txt"'
+ part.write 'hello world'
+ req.end (res) ->
+ res.should.have.status 200
+ res.text.should.equal 'awesome upload received'
+ done()
+
+ describe 'progress', ->
+ $ = {}
+ before ->
+ # uploadHandler = new uploadProgress.UploadHandler
+ # $.server = app.createServer (req, res) ->
+ # if req.url.match /\/upload\?X\-Progress\-ID=.+/
+ # uploadHandler.upload req, res
+ # else if req.url.match /\/progress\?X\-Progress\-ID=.+/
+ # uploadHandler.progress req, res
+ # $.server.listen(8080)
+
+ after ->
+ # $.server.close()
+
+ it 'should return starting if not found', ->
+ # req = request.get 'http://localhost:8080/progress?X-Progress-ID=1'
+ # req.end (res) ->
+ # res.should.have.status 404
+ # res.text.should.equal 'not found'
View
20 test/unit/node-upload-progress_test.coffee
@@ -1,13 +1,13 @@
uploadProgress = require '../../lib/node-upload-progress'
describe 'UploadHandler', ->
- $ = {}
- beforeEach ->
- $.uploadHandler = new uploadProgress.UploadHandler
-
- describe 'configure', ->
- it 'should call configure', ->
- dirName = "#{__dirname}/uploads"
- $.uploadHandler.configure ->
- this.uploadDir = dirName
- $.uploadHandler.uploadDir.should.equal dirName
+ $ = {}
+ beforeEach ->
+ $.uploadHandler = new uploadProgress.UploadHandler
+
+ describe 'configure', ->
+ it 'should call configure', ->
+ dirName = "#{__dirname}/uploads"
+ $.uploadHandler.configure ->
+ this.uploadDir = dirName
+ $.uploadHandler.uploadDir.should.equal dirName
View
98 test/unit/upload_test.coffee
@@ -1,52 +1,52 @@
Upload = require '../../lib/upload'
describe 'Upload', ->
- $ = {}
-
- beforeEach ->
- $.upload = new Upload
-
- describe 'isDone', ->
- it 'should is done if received == expected', ->
- $.upload.updateProgress 10, 10
- $.upload.isDone().should.be.true
-
- it 'should is not done if received < expected', ->
- $.upload.updateProgress 1, 10
- $.upload.isDone().should.be.false
-
- it 'should return JSON', ->
- $.upload.updateProgress 1, 10
- $.upload.file = name: 'hello.txt', path: "#{__dirname}/hello.txt"
- $.upload.toJSON().should.equal '{"bytesReceived":1,"bytesExpected":10,"percent":10,"status":"uploading","fileName":"hello.txt","filePath":"' + __dirname + '/hello.txt"}'
-
- it 'should return JSON without file', ->
- $.upload.updateProgress 1, 10
- $.upload.toJSON().should.equal '{"bytesReceived":1,"bytesExpected":10,"percent":10,"status":"uploading"}'
-
- describe 'percent', ->
- it 'should return 100%', ->
- $.upload.updateProgress 10, 10
- $.upload.percent().should.equal 100
-
- it 'should return 50%', ->
- $.upload.updateProgress 5, 10
- $.upload.percent().should.equal 50
-
- it 'should return 0 if bytesExpected is 0', ->
- $.upload.updateProgress 0, 0
- $.upload.percent().should.equal 0
-
- describe 'status', ->
- it 'should be done if percent 100 and fileName is present', ->
- $.upload.updateProgress 10, 10
- $.upload.file = name: 'hello.txt', path: "#{__dirname}/hello.txt"
- $.upload.status().should.equal 'done'
-
- it 'should be uploading if percent < 100', ->
- $.upload.updateProgress 5, 10
- $.upload.status().should.equal 'uploading'
-
- it 'should be starting if is not uploading or done', ->
- $.upload.updateProgress 10, 10
- $.upload.status().should.equal 'starting'
+ $ = {}
+
+ beforeEach ->
+ $.upload = new Upload
+
+ describe 'isDone', ->
+ it 'should is done if received == expected', ->
+ $.upload.updateProgress 10, 10
+ $.upload.isDone().should.be.true
+
+ it 'should is not done if received < expected', ->
+ $.upload.updateProgress 1, 10
+ $.upload.isDone().should.be.false
+
+ it 'should return JSON', ->
+ $.upload.updateProgress 1, 10
+ $.upload.file = name: 'hello.txt', path: "#{__dirname}/hello.txt"
+ $.upload.toJSON().should.equal '{"bytesReceived":1,"bytesExpected":10,"percent":10,"status":"uploading","fileName":"hello.txt","filePath":"' + __dirname + '/hello.txt"}'
+
+ it 'should return JSON without file', ->
+ $.upload.updateProgress 1, 10
+ $.upload.toJSON().should.equal '{"bytesReceived":1,"bytesExpected":10,"percent":10,"status":"uploading"}'
+
+ describe 'percent', ->
+ it 'should return 100%', ->
+ $.upload.updateProgress 10, 10
+ $.upload.percent().should.equal 100
+
+ it 'should return 50%', ->
+ $.upload.updateProgress 5, 10
+ $.upload.percent().should.equal 50
+
+ it 'should return 0 if bytesExpected is 0', ->
+ $.upload.updateProgress 0, 0
+ $.upload.percent().should.equal 0
+
+ describe 'status', ->
+ it 'should be done if percent 100 and fileName is present', ->
+ $.upload.updateProgress 10, 10
+ $.upload.file = name: 'hello.txt', path: "#{__dirname}/hello.txt"
+ $.upload.status().should.equal 'done'
+
+ it 'should be uploading if percent < 100', ->
+ $.upload.updateProgress 5, 10
+ $.upload.status().should.equal 'uploading'
+
+ it 'should be starting if is not uploading or done', ->
+ $.upload.updateProgress 10, 10
+ $.upload.status().should.equal 'starting'
View
54 test/unit/uploads_test.coffee
@@ -3,35 +3,35 @@ Upload = require '../../lib/upload'
should = require 'should'
describe 'Uploads', ->
- $ = {}
+ $ = {}
- beforeEach ->
- $.uploads = new Uploads
- $.upload = new Upload
+ beforeEach ->
+ $.uploads = new Uploads
+ $.upload = new Upload
- describe 'add', ->
- it 'should add without upload', ->
- upload = $.uploads.add 1
- should.exist upload
+ describe 'add', ->
+ it 'should add without upload', ->
+ upload = $.uploads.add 1
+ should.exist upload
- it 'should add and get a new upload', ->
- $.uploads.add 1, $.upload
- $.uploads.get(1).should.equal $.upload
-
- it 'should add undefined', ->
- # undefined uuid occurs when X-Progress-ID is not passed in the querystring
- upload = $.uploads.add
- should.exist upload
+ it 'should add and get a new upload', ->
+ $.uploads.add 1, $.upload
+ $.uploads.get(1).should.equal $.upload
- describe 'get', ->
- it 'should return nil if the upload does not exist', ->
- should.not.exist $.uploads.get(1)
+ it 'should add undefined', ->
+ # undefined uuid occurs when X-Progress-ID is not passed in the querystring
+ upload = $.uploads.add
+ should.exist upload
- describe 'remove', ->
- it 'should remove an upload', (done) ->
- $.uploads.add 1, $.upload
- $.uploads.remove 1, 10
- setTimeout ->
- should.not.exist $.uploads.get(1)
- done()
- , 10
+ describe 'get', ->
+ it 'should return nil if the upload does not exist', ->
+ should.not.exist $.uploads.get(1)
+
+ describe 'remove', ->
+ it 'should remove an upload', (done) ->
+ $.uploads.add 1, $.upload
+ $.uploads.remove 1, 10
+ setTimeout ->
+ should.not.exist $.uploads.get(1)
+ done()
+ , 10
Please sign in to comment.
Something went wrong with that request. Please try again.