Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add #findUrlContentHash and tests

  • Loading branch information...
commit a47426986b25212972706c82133b70908581c060 1 parent b6329b2
Anton Podviaznikov podviaznikov authored
Showing with 45 additions and 6 deletions.
  1. +2 −0  .gitignore
  2. +19 −0 lib/files.coffee
  3. +24 −6 test/files.test.coffee
2  .gitignore
View
@@ -19,3 +19,5 @@ node-files.sublime-workspace
test.zip
*.zip
test.zip*
+
+.DS_Store
19 lib/files.coffee
View
@@ -49,6 +49,25 @@ exports.fetchFromUrlToHash = (url, path, extension, callback) ->
catch error
callback {code: "invalidURL", message: "'#{url}' is invalid file URL"} # error callback
+
+# Method for finding content hash for file behind url.
+exports.findUrlContentHash = (url, callback) ->
+ if !url
+ callback {code: "invalidArguments", message: "Mandatory arguments weren't specified"}
+ return
+ try
+ check(url).isUrl() # check url for validity
+ md5sum = crypto.createHash("md5")
+ req = request url, (err, response, body) ->
+ if err or response.statusCode != 200
+ callback {code: "fileNotFound", message: "File wasn't found on URL '#{url}'"} # error callback
+ return
+ callback undefined, md5sum.digest("hex") # success callback
+ req.on "data", (d) ->
+ md5sum.update d
+ catch error
+ callback {code: "invalidURL", message: "'#{url}' is invalid file URL"} # error callback
+
# Get file name from the path.
exports.getFileName = (path) ->
if !path
30 test/files.test.coffee
View
@@ -44,16 +44,34 @@ describe "files", ->
err.code.should.eql "fileNotFound"
err.message.should.eql "File wasn't found on URL 'https://circuithub-invalid-url.com/file'"
should.not.exist path
- done()
- describe "#fetchFromUrlToHash", ->
+ done()
+ describe "#findUrlContentHash", ->
+ it "should return valid content hash for valid input", (done) ->
+ files.findUrlContentHash "http://cloud.github.com/downloads/circuithub/node-files/index.coffee", (err, hash) ->
+ should.not.exist err
+ hash.should.eql "37f53f3f9825245ab746f24655adf75c"
+ done()
+ it "should fail for null url", (done) ->
+ files.findUrlContentHash null, (err, hash) ->
+ err.code.should.eql "invalidArguments"
+ err.message.should.eql "Mandatory arguments weren't specified"
+ should.not.exist hash
+ done()
+ it "should fail for undefined url", (done) ->
+ files.findUrlContentHash undefined, (err, hash) ->
+ err.code.should.eql "invalidArguments"
+ err.message.should.eql "Mandatory arguments weren't specified"
+ should.not.exist hash
+ done()
+ describe "#fetchFromUrlToHash", ->
it "should fetch for valid input", (done) ->
- files.fetchFromUrlToHash "https://github.com/circuithub/node-files/zipball/master", "./", ".zip", (err, path) ->
+ files.fetchFromUrlToHash "http://cloud.github.com/downloads/circuithub/node-files/index.coffee", "./", ".zip", (err, path) ->
should.not.exist err
- path.should.eql "./1bfb996ba0e8a99521889318ec025eb5.zip"
+ path.should.eql "./37f53f3f9825245ab746f24655adf75c.zip"
done()
it "should fail for invalid url", (done) ->
files.fetchFromUrlToHash "not-an-url", "./test.zip", ".zip", (err, path) ->
- err.code.should.eql "fileNotFound"
+ err.code.should.eql "invalidURL"
err.message.should.eql "\'not-an-url\' is invalid file URL"
should.not.exist path
done()
@@ -89,7 +107,7 @@ describe "files", ->
done()
it "should fail for valid url that doesn't exist", (done) ->
files.fetchFromUrlToHash "https://circuithub-invalid-url.com/file", "./test.zip", ".zip", (err, path) ->
- err.code.should.eql "invalidURL"
+ err.code.should.eql "fileNotFound"
err.message.should.eql "File wasn't found on URL 'https://circuithub-invalid-url.com/file'"
should.not.exist path
done()
Please sign in to comment.
Something went wrong with that request. Please try again.