Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

switch tests to mocha

  • Loading branch information...
commit 4ba0f7b76b7e5928a88fe2c91b39b0ccf7935dee 1 parent 590753c
Konstantin Käfer authored February 14, 2012
8  Makefile
@@ -2,14 +2,8 @@
2 2
 # Run all tests
3 3
 #
4 4
 
5  
-ifndef only
6 5
 test:
7  
-	./node_modules/.bin/expresso -I lib test/*.test.js
8  
-else
9  
-test:
10  
-	rm -rf test_data/files_*
11  
-	./node_modules/.bin/expresso -I lib test/${only}.test.js
12  
-endif
  6
+	@mocha -R spec
13 7
 
14 8
 lint:
15 9
 	./node_modules/.bin/jshint lib/node-get/*.js
8  lib/node-get/node-get.js
@@ -201,9 +201,11 @@ Get.prototype.asString = function(callback) {
201 201
     this.perform(function pipeResponseToString(err, response) {
202 202
         if (err) return callback(err);
203 203
 
204  
-        switch ((response.headers['content-type'] || '').toLowerCase()) {
205  
-            case 'binary/octet-stream':
206  
-            case 'application/octet-stream':
  204
+        switch ((response.headers['content-type'] || '').toLowerCase().split('/')[0]) {
  205
+            case 'binary':
  206
+            case 'application':
  207
+            case 'image':
  208
+            case 'video':
207 209
                 return callback(new Error("Can't download binary file as string"));
208 210
             default:
209 211
                 // TODO: respect Content-Transfer-Encoding header
2  package.json
@@ -17,7 +17,7 @@
17 17
         "type": "BSD"
18 18
     }],
19 19
     "devDependencies": {
20  
-        "expresso": "0.9.x",
  20
+        "mocha": ">= 0.11",
21 21
         "jshint": "0.2.x",
22 22
         "underscore": "1.2.x",
23 23
         "docco": "0.3.x"
285  test/basic.test.js
... ...
@@ -1,157 +1,162 @@
1  
-var path = require('path'),
2  
-    util = require('util'),
3  
-    assert = require('assert'),
4  
-    crypto = require('crypto'),
5  
-    _ = require('underscore'),
6  
-    exec = require('child_process').exec,
7  
-    fs = require('fs');
  1
+var path = require('path');
  2
+var assert = require('assert');
  3
+var crypto = require('crypto');
  4
+var exec = require('child_process').exec;
  5
+var fs = require('fs');
  6
+var get = require('..');
8 7
 
9  
-function md5(str) {
10  
-    return crypto.createHash('md5').update(str).digest('hex');
11  
-}
12  
-
13  
-var get = require('../lib/node-get');
14  
-
15  
-
16  
-// Ensure that we start over with a clean test_data directory.
17  
-exec('rm -rf ./test_data && mkdir ./test_data', function(err, stdout, stderr) {
18  
-if (err) throw err;
19 8
 
20  
-exports['test invalid url'] = function() {
21  
-    var req = {
22  
-        uri: 'http://\\/',
23  
-        headers: {
24  
-            'User-Agent': 'tombot'
25  
-        }
26  
-    };
27  
-    new get(req).toDisk('test_data/file_tmp', function(err, result) {
28  
-        assert.equal(err.message, 'Invalid URL');
29  
-    });
30  
-};
  9
+function md5(obj) {
  10
+    if (!Array.isArray(obj)) obj = [ obj ];
  11
+    var hash = crypto.createHash('md5');
  12
+    obj.forEach(hash.update.bind(hash));
  13
+    return hash.digest('hex');
  14
+}
31 15
 
32  
-exports['test constructor'] = function(beforeExit) {
33  
-    // Verify callback completion.
34  
-    var completed = {
35  
-        perform: 0,
36  
-        toDisk: 0,
37  
-        asString: 0,
38  
-        asBuffer: 0
39  
-    };
  16
+var files = [
  17
+    {
  18
+        url: 'https://docs.google.com/a/dbsgeo.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0AqV4OJpywingdFNYLXpKMmxqMG1lWTJzNE45ZUVnNlE&single=true&gid=0&output=csv',
  19
+        bin: false,
  20
+        redirects: 1,
  21
+        md5: 'a4d019d2bfedc55e84447f833ed71dff'
  22
+    },
  23
+    {
  24
+        url: 'http://tilemill-data.s3.amazonaws.com/images/paperfolds_256.png',
  25
+        bin: true,
  26
+        md5: 'a99502016d4e2124cf2dc4775aafc256'
  27
+    },
  28
+    {
  29
+        url: 'http://tilemill-data.s3.amazonaws.com/test_data/ipsum.json',
  30
+        bin: true,
  31
+        md5: '651ea0ff31786e9be9012112b21573be'
  32
+    },
  33
+    {
  34
+        url: 'http://tilemill-data.s3.amazonaws.com/test_data/README.txt',
  35
+        bin: false,
  36
+        md5: '46d883d71e795d7c8a65c9b1a6673dd2'
  37
+    },
  38
+    {
  39
+        url: 'http://tilemill-data.s3.amazonaws.com/test_data/shape_demo.zip',
  40
+        bin: true,
  41
+        md5: 'b5ff3545922cc9e0c750fb7263a7a3d3'
  42
+    },
  43
+    {
  44
+        url: 'http://tilemill-data.s3.amazonaws.com/nasa_raster/lasvegas_tm5_12jan09_crop_geo_merc_small.tif',
  45
+        bin: true,
  46
+        md5: '3b97134839042fcab0d0ee8ea76f73c4'
  47
+    },
  48
+    {
  49
+        url: 'http://dcatlas.dcgis.dc.gov/catalog/download.asp?downloadID=2315&downloadTYPE=ESRI',
  50
+        bin: true,
  51
+        md5: 'ac629823b373e7308087264d9f00ef5c'
  52
+    }
  53
+];
40 54
 
41  
-    var files = [
42  
-        {
43  
-            url: 'https://docs.google.com/a/dbsgeo.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0AqV4OJpywingdFNYLXpKMmxqMG1lWTJzNE45ZUVnNlE&single=true&gid=0&output=csv',
44  
-            bin: false,
45  
-            redirects: 1,
46  
-            md5: 'a4d019d2bfedc55e84447f833ed71dff'
47  
-        },
48  
-        {
49  
-            url: 'http://tilemill-data.s3.amazonaws.com/images/paperfolds_256.png',
50  
-            bin: true,
51  
-            md5: 'a99502016d4e2124cf2dc4775aafc256'
52  
-        },
53  
-        {
54  
-            url: 'http://tilemill-data.s3.amazonaws.com/test_data/ipsum.json',
55  
-            bin: true,
56  
-            md5: '651ea0ff31786e9be9012112b21573be'
57  
-        },
58  
-        {
59  
-            url: 'http://tilemill-data.s3.amazonaws.com/test_data/README.txt',
60  
-            bin: false,
61  
-            md5: '46d883d71e795d7c8a65c9b1a6673dd2'
62  
-        },
63  
-        {
64  
-            url: 'http://tilemill-data.s3.amazonaws.com/test_data/shape_demo.zip',
65  
-            bin: true,
66  
-            md5: 'b5ff3545922cc9e0c750fb7263a7a3d3'
67  
-        },
68  
-        {
69  
-            url: 'http://tilemill-data.s3.amazonaws.com/nasa_raster/lasvegas_tm5_12jan09_crop_geo_merc_small.tif',
70  
-            bin: true,
71  
-            md5: '3b97134839042fcab0d0ee8ea76f73c4'
72  
-        },
73  
-        {
74  
-            url: 'http://dcatlas.dcgis.dc.gov/catalog/download.asp?downloadID=2315&downloadTYPE=ESRI',
75  
-            bin: true,
76  
-            md5: 'ac629823b373e7308087264d9f00ef5c'
77  
-        }
78  
-    ];
79 55
 
80  
-    files.forEach(function(reference, i) {
81  
-        var req = {
82  
-            uri: reference.url,
83  
-            headers: {
84  
-                'User-Agent': 'tombot'
85  
-            }
86  
-        };
87 56
 
88  
-        new get(req).perform(function(err, result) {
89  
-            var body = [];
90  
-            if (reference.error) {
91  
-                assert.eql(err, reference.error, 'This should have had an error');
92  
-            } else if (err) {
93  
-                throw new Error(err);
94  
-            } else {
95  
-                result.on('data', function(chunk) {
96  
-                    body.push(chunk);
97  
-                });
98  
-                result.on('end', function() {
99  
-                    completed.perform++;
100  
-                    assert.ok(body.length);
101  
-                });
102  
-                result.on('close', function() {
103  
-                    completed.perform++;
104  
-                    assert.ok(body.length);
105  
-                });
106  
-            }
107  
-        });
  57
+// Ensure that we start over with a clean test_data directory.
  58
+before(function(done) {
  59
+    exec('rm -rf ./test_data && mkdir ./test_data', done);
  60
+});
108 61
 
109  
-        new get(req).toDisk('test_data/file_' + i, function(err, result) {
110  
-            completed.toDisk++;
111  
-            if (reference.error) {
112  
-                assert.equal(err.message, reference.error);
113  
-            } else if (err) {
114  
-                throw new Error(err);
115  
-            } else {
116  
-                assert.equal(md5(fs.readFileSync(result)), reference.md5);
117  
-            }
  62
+describe('get().perform', function() {
  63
+    files.forEach(function(reference) {
  64
+        it('should return request object for ' + reference.url, function(done) {
  65
+            this.timeout(0);
  66
+            new get({
  67
+                uri: reference.url,
  68
+                headers: { 'User-Agent': 'tombot' }
  69
+            }).perform(function(err, result) {
  70
+                var body = [];
  71
+                if (reference.error) {
  72
+                    assert.ok(err);
  73
+                    assert.eql(err.message, reference.error);
  74
+                    done();
  75
+                } else if (err) {
  76
+                    done(err);
  77
+                } else {
  78
+                    result.on('error', done);
  79
+                    result.on('data', function(chunk) {
  80
+                        body.push(chunk);
  81
+                    });
  82
+                    result.on('end', function() {
  83
+                        assert.equal(md5(body), reference.md5);
  84
+                        done();
  85
+                    });
  86
+                }
  87
+            });
118 88
         });
  89
+    });
  90
+});
119 91
 
120  
-        new get(req).asString(function(err, result) {
121  
-            completed.asString++;
122  
-
123  
-            if (reference.bin && err) {
124  
-                assert.equal(err.message, "Can't download binary file as string");
125  
-            } else if (reference.error) {
126  
-                assert.equal(err.message, reference.error);
127  
-            } else if (err) {
128  
-                throw new Error(err);
129  
-            } else {
130  
-                assert.equal(md5(result), reference.md5);
131  
-            }
  92
+describe('get().toDisk', function() {
  93
+    files.forEach(function(reference) {
  94
+        it('should save to disk for ' + reference.url, function(done) {
  95
+            this.timeout(0);
  96
+            new get({
  97
+                uri: reference.url,
  98
+                headers: { 'User-Agent': 'tombot' }
  99
+            }).toDisk('test_data/file_' + reference.md5, function(err, result) {
  100
+                if (reference.error) {
  101
+                    assert.ok(err);
  102
+                    assert.equal(err.message, reference.error);
  103
+                    done();
  104
+                } else if (err) {
  105
+                    done(err);
  106
+                } else {
  107
+                    assert.equal(md5(fs.readFileSync(result)), reference.md5);
  108
+                    done();
  109
+                }
  110
+            });
132 111
         });
  112
+    });
  113
+});
133 114
 
134  
-        new get(req).asBuffer(function(err, result) {
135  
-            completed.asBuffer++;
136  
-
137  
-            if (reference.error) {
138  
-                assert.equal(err.message, reference.error);
139  
-            } else if (err) {
140  
-                throw err;
141  
-            } else {
142  
-                assert.equal(md5(result), reference.md5);
143  
-            }
  115
+describe('get().asString', function() {
  116
+    files.forEach(function(reference) {
  117
+        it('should return as string for ' + reference.url, function(done) {
  118
+            this.timeout(0);
  119
+            new get({
  120
+                uri: reference.url,
  121
+                headers: { 'User-Agent': 'tombot' }
  122
+            }).asString(function(err, result) {
  123
+                if (reference.bin && err) {
  124
+                    assert.equal(err.message, "Can't download binary file as string");
  125
+                    done();
  126
+                } else if (reference.bin) {
  127
+                    done(new Error('Should not return binary files as string'));
  128
+                } else if (reference.error) {
  129
+                    assert.equal(err.message, reference.error);
  130
+                    done();
  131
+                } else if (err) {
  132
+                    done(err);
  133
+                } else {
  134
+                    assert.equal(md5(result), reference.md5);
  135
+                    done();
  136
+                }
  137
+            });
144 138
         });
145 139
     });
  140
+});
146 141
 
147  
-    beforeExit(function() {
148  
-        assert.deepEqual(completed, {
149  
-            perform: files.length /* + _.reduce(_.pluck(files, 'redirects'),
150  
-                function(memo, num) { return memo + (num || 0); }, 0) */,
151  
-            toDisk: files.length,
152  
-            asString: files.length,
153  
-            asBuffer: files.length
  142
+describe('get().asBuffer', function() {
  143
+    files.forEach(function(reference) {
  144
+        it('should return as buffer for ' + reference.url, function(done) {
  145
+            this.timeout(0);
  146
+            new get({
  147
+                uri: reference.url,
  148
+                headers: { 'User-Agent': 'tombot' }
  149
+            }).asBuffer(function(err, result) {
  150
+                if (reference.error) {
  151
+                    assert.equal(err.message, reference.error);
  152
+                    done();
  153
+                } else if (err) {
  154
+                    done(err);
  155
+                } else {
  156
+                    assert.equal(md5(result), reference.md5);
  157
+                    done();
  158
+                }
  159
+            });
154 160
         });
155 161
     });
156  
-};
157 162
 });

0 notes on commit 4ba0f7b

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