[6a3a178] | 1 | var should = require('should'),
|
---|
| 2 | needle = require('./../'),
|
---|
| 3 | http = require('http'),
|
---|
| 4 | sinon = require('sinon'),
|
---|
| 5 | stream = require('stream'),
|
---|
| 6 | fs = require('fs'),
|
---|
| 7 | port = 11111,
|
---|
| 8 | server;
|
---|
| 9 |
|
---|
| 10 | describe('with output option', function() {
|
---|
| 11 |
|
---|
| 12 | var server, handler, file = '/tmp/foobar.out';
|
---|
| 13 |
|
---|
| 14 | function send_request_cb(where, cb) {
|
---|
| 15 | var url = 'http://localhost:' + port + '/whatever.file';
|
---|
| 16 | return needle.get(url, { output: where }, cb);
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | function send_request_stream(where, cb) {
|
---|
| 20 | var url = 'http://localhost:' + port + '/whatever.file';
|
---|
| 21 | var stream = needle.get(url, { output: where });
|
---|
| 22 | stream.on('end', cb);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | // this will only work in UNICES
|
---|
| 26 | function get_open_file_descriptors() {
|
---|
| 27 | var list = fs.readdirSync('/proc/self/fd');
|
---|
| 28 | return list.length;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | var send_request = send_request_cb;
|
---|
| 32 |
|
---|
| 33 | before(function(){
|
---|
| 34 | server = http.createServer(function(req, res) {
|
---|
| 35 | handler(req, res);
|
---|
| 36 | }).listen(port);
|
---|
| 37 | });
|
---|
| 38 |
|
---|
| 39 | after(function() {
|
---|
| 40 | server.close();
|
---|
| 41 | })
|
---|
| 42 |
|
---|
| 43 | beforeEach(function() {
|
---|
| 44 | try { fs.unlinkSync(file) } catch(e) { };
|
---|
| 45 | })
|
---|
| 46 |
|
---|
| 47 | describe('and a 404 response', function() {
|
---|
| 48 |
|
---|
| 49 | before(function() {
|
---|
| 50 | handler = function(req, res) {
|
---|
| 51 | res.writeHead(404, {'Content-Type': 'text/plain' });
|
---|
| 52 | res.end();
|
---|
| 53 | }
|
---|
| 54 | })
|
---|
| 55 |
|
---|
| 56 | it('doesnt attempt to write a file', function(done) {
|
---|
| 57 | var spy = sinon.spy(fs, 'createWriteStream');
|
---|
| 58 | send_request(file, function(err, resp) {
|
---|
| 59 | resp.statusCode.should.eql(404);
|
---|
| 60 | spy.called.should.eql(false);
|
---|
| 61 | spy.restore();
|
---|
| 62 | done();
|
---|
| 63 | })
|
---|
| 64 | })
|
---|
| 65 |
|
---|
| 66 | it('doesnt actually write a file', function(done) {
|
---|
| 67 | send_request(file, function(err, resp) {
|
---|
| 68 | resp.statusCode.should.eql(404);
|
---|
| 69 | fs.existsSync(file).should.eql(false);
|
---|
| 70 | done();
|
---|
| 71 | })
|
---|
| 72 | })
|
---|
| 73 |
|
---|
| 74 | })
|
---|
| 75 |
|
---|
| 76 | describe('and a 200 response', function() {
|
---|
| 77 |
|
---|
| 78 | describe('for an empty response', function() {
|
---|
| 79 |
|
---|
| 80 | before(function() {
|
---|
| 81 | handler = function(req, res) {
|
---|
| 82 | res.writeHead(200, { 'Content-Type': 'text/plain' });
|
---|
| 83 | res.end();
|
---|
| 84 | }
|
---|
| 85 | })
|
---|
| 86 |
|
---|
| 87 | it('uses a writableStream', function(done) {
|
---|
| 88 | var spy = sinon.spy(fs, 'createWriteStream');
|
---|
| 89 | send_request(file, function(err, resp) {
|
---|
| 90 | resp.statusCode.should.eql(200);
|
---|
| 91 | spy.called.should.eql(true);
|
---|
| 92 | spy.restore();
|
---|
| 93 | done();
|
---|
| 94 | })
|
---|
| 95 | })
|
---|
| 96 |
|
---|
| 97 | it('writes a file', function(done) {
|
---|
| 98 | fs.existsSync(file).should.eql(false);
|
---|
| 99 | send_request(file, function(err, resp) {
|
---|
| 100 | fs.existsSync(file).should.eql(true);
|
---|
| 101 | done();
|
---|
| 102 | })
|
---|
| 103 | })
|
---|
| 104 |
|
---|
| 105 | it('file is zero bytes in length', function(done) {
|
---|
| 106 | send_request(file, function(err, resp) {
|
---|
| 107 | fs.statSync(file).size.should.equal(0);
|
---|
| 108 | done();
|
---|
| 109 | })
|
---|
| 110 | })
|
---|
| 111 |
|
---|
| 112 | if (process.platform != 'win32') {
|
---|
| 113 | it('closes the file descriptor', function(done) {
|
---|
| 114 | var open_descriptors = get_open_file_descriptors();
|
---|
| 115 | send_request(file + Math.random(), function(err, resp) {
|
---|
| 116 | var current_descriptors = get_open_file_descriptors();
|
---|
| 117 | open_descriptors.should.eql(current_descriptors);
|
---|
| 118 | done()
|
---|
| 119 | })
|
---|
| 120 | })
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | })
|
---|
| 124 |
|
---|
| 125 | describe('for a JSON response', function() {
|
---|
| 126 |
|
---|
| 127 | before(function() {
|
---|
| 128 | handler = function(req, res) {
|
---|
| 129 | res.writeHead(200, { 'Content-Type': 'application/javascript' });
|
---|
| 130 | res.end(JSON.stringify({foo: 'bar'}));
|
---|
| 131 | }
|
---|
| 132 | })
|
---|
| 133 |
|
---|
| 134 | it('uses a writableStream', function(done) {
|
---|
| 135 | var spy = sinon.spy(fs, 'createWriteStream');
|
---|
| 136 | send_request(file, function(err, resp) {
|
---|
| 137 | resp.statusCode.should.eql(200);
|
---|
| 138 | spy.called.should.eql(true);
|
---|
| 139 | spy.restore();
|
---|
| 140 | done();
|
---|
| 141 | })
|
---|
| 142 | })
|
---|
| 143 |
|
---|
| 144 | it('writes a file', function(done) {
|
---|
| 145 | fs.existsSync(file).should.eql(false);
|
---|
| 146 | send_request(file, function(err, resp) {
|
---|
| 147 | fs.existsSync(file).should.eql(true);
|
---|
| 148 | done();
|
---|
| 149 | })
|
---|
| 150 | })
|
---|
| 151 |
|
---|
| 152 | it('file size equals response length', function(done) {
|
---|
| 153 | send_request(file, function(err, resp) {
|
---|
| 154 | fs.statSync(file).size.should.equal(resp.bytes);
|
---|
| 155 | done();
|
---|
| 156 | })
|
---|
| 157 | })
|
---|
| 158 |
|
---|
| 159 | it('response pipeline is honoured (JSON is decoded by default)', function(done) {
|
---|
| 160 | send_request_stream(file, function(err, resp) {
|
---|
| 161 | // we need to wait a bit since writing to config.output
|
---|
| 162 | // happens independently of needle's callback logic.
|
---|
| 163 | setTimeout(function() {
|
---|
| 164 | fs.readFileSync(file).toString().should.eql('{\"foo\":\"bar\"}');
|
---|
| 165 | done();
|
---|
| 166 | }, 20);
|
---|
| 167 | })
|
---|
| 168 | })
|
---|
| 169 |
|
---|
| 170 | it('closes the file descriptor', function(done) {
|
---|
| 171 | var open_descriptors = get_open_file_descriptors();
|
---|
| 172 | send_request(file + Math.random(), function(err, resp) {
|
---|
| 173 | var current_descriptors = get_open_file_descriptors();
|
---|
| 174 | open_descriptors.should.eql(current_descriptors);
|
---|
| 175 | done()
|
---|
| 176 | })
|
---|
| 177 | })
|
---|
| 178 |
|
---|
| 179 | })
|
---|
| 180 |
|
---|
| 181 | describe('for a binary file', function() {
|
---|
| 182 |
|
---|
| 183 | var pixel = Buffer.from("base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs", "base64");
|
---|
| 184 |
|
---|
| 185 | before(function() {
|
---|
| 186 | handler = function(req, res) {
|
---|
| 187 | res.writeHead(200, { 'Content-Type': 'application/octet-stream', 'Transfer-Encoding': 'chunked' });
|
---|
| 188 | res.write(pixel.slice(0, 10));
|
---|
| 189 | res.write(pixel.slice(10, 20));
|
---|
| 190 | res.write(pixel.slice(20, 30));
|
---|
| 191 | res.write(pixel.slice(30));
|
---|
| 192 | res.end();
|
---|
| 193 | }
|
---|
| 194 | })
|
---|
| 195 |
|
---|
| 196 | it('uses a writableStream', function(done) {
|
---|
| 197 | var spy = sinon.spy(fs, 'createWriteStream');
|
---|
| 198 | send_request(file, function(err, resp) {
|
---|
| 199 | resp.statusCode.should.eql(200);
|
---|
| 200 | spy.called.should.eql(true);
|
---|
| 201 | spy.restore();
|
---|
| 202 | done();
|
---|
| 203 | })
|
---|
| 204 | })
|
---|
| 205 |
|
---|
| 206 | it('writes a file', function(done) {
|
---|
| 207 | fs.existsSync(file).should.eql(false);
|
---|
| 208 | send_request(file, function(err, resp) {
|
---|
| 209 | fs.existsSync(file).should.eql(true);
|
---|
| 210 | done();
|
---|
| 211 | })
|
---|
| 212 | })
|
---|
| 213 |
|
---|
| 214 | it('file size equals response length', function(done) {
|
---|
| 215 | send_request(file, function(err, resp) {
|
---|
| 216 | fs.statSync(file).size.should.equal(resp.bytes);
|
---|
| 217 | done();
|
---|
| 218 | })
|
---|
| 219 | })
|
---|
| 220 |
|
---|
| 221 | it('file is equal to original buffer', function(done) {
|
---|
| 222 | send_request(file, function(err, resp) {
|
---|
| 223 | // we need to wait a bit since writing to config.output
|
---|
| 224 | // happens independently of needle's callback logic.
|
---|
| 225 | setTimeout(function() {
|
---|
| 226 | fs.readFileSync(file).should.eql(pixel);
|
---|
| 227 | done();
|
---|
| 228 | }, 20);
|
---|
| 229 | })
|
---|
| 230 | })
|
---|
| 231 |
|
---|
| 232 | it('returns the data in resp.body too', function(done) {
|
---|
| 233 | send_request(file, function(err, resp) {
|
---|
| 234 | resp.body.should.eql(pixel);
|
---|
| 235 | done();
|
---|
| 236 | })
|
---|
| 237 | })
|
---|
| 238 |
|
---|
| 239 | if (process.platform != 'win32') {
|
---|
| 240 | it('closes the file descriptor', function(done) {
|
---|
| 241 | var open_descriptors = get_open_file_descriptors();
|
---|
| 242 | send_request(file + Math.random(), function(err, resp) {
|
---|
| 243 | var current_descriptors = get_open_file_descriptors();
|
---|
| 244 | open_descriptors.should.eql(current_descriptors);
|
---|
| 245 | done()
|
---|
| 246 | })
|
---|
| 247 | })
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | })
|
---|
| 251 |
|
---|
| 252 | })
|
---|
| 253 |
|
---|
| 254 | })
|
---|