forked from forwardemail/superagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.js
More file actions
51 lines (38 loc) · 1.13 KB
/
pipe.js
File metadata and controls
51 lines (38 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var request = require('../../')
, express = require('express')
, app = express()
, fs = require('fs')
, bodyParser = require('body-parser');
app.use(bodyParser.json());
app.post('/', function(req, res){
res.send(req.body);
});
app.listen(3020);
describe('request pipe', function(){
afterEach(removeTmpfile);
it('should act as a writable stream', function(done){
var req = request.post('http://localhost:3020');
var stream = fs.createReadStream('test/node/fixtures/user.json');
req.type('json');
req.on('response', function(res){
res.body.should.eql({ name: 'tobi' });
done();
});
stream.pipe(req);
})
it('should act as a readable stream', function(done){
var stream = fs.createWriteStream('test/node/fixtures/tmp.json');
var req = request.get('http://localhost:3025');
req.type('json');
req.on('end', function(){
JSON.parse(fs.readFileSync('test/node/fixtures/tmp.json', 'utf8')).should.eql({ name: 'tobi' });
done();
});
req.pipe(stream);
})
});
function removeTmpfile(done){
fs.unlink('test/node/fixtures/tmp.json', function(err){
done();
});
}