source: trip-planner-front/node_modules/needle/test/response_stream_spec.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.6 KB
Line 
1var should = require('should'),
2 needle = require('./../'),
3 http = require('http'),
4 stream = require('stream'),
5 fs = require('fs'),
6 port = 11111,
7 server;
8
9describe('response streams', function() {
10
11 describe('when the server sends back json', function(){
12
13 before(function(done) {
14 server = http.createServer(function(req, res) {
15 res.setHeader('Content-Type', 'application/json')
16 res.end('{"foo":"bar"}')
17 }).listen(port, done);
18 });
19
20 after(function(done) {
21 server.close(done);
22 })
23
24 describe('and the client uses streams', function(){
25
26 it('creates a proper streams2 stream', function(done) {
27 var stream = needle.get('localhost:' + port)
28
29 // newer node versions set this to null instead of false
30 var bool = !!stream._readableState.flowing;
31 should.equal(false, bool);
32
33 var readableCalled = false;
34 stream.on('readable', function() {
35 readableCalled = true;
36 })
37
38 stream.on('finish', function() {
39 readableCalled.should.be.true;
40 done();
41 });
42
43 stream.resume();
44 })
45
46 it('emits a single data item which is our JSON object', function(done) {
47 var stream = needle.get('localhost:' + port)
48
49 var chunks = [];
50 stream.on('readable', function () {
51 while (chunk = this.read()) {
52 chunk.should.be.an.Object;
53 chunks.push(chunk);
54 }
55 })
56
57 stream.on('done', function () {
58 chunks.should.have.length(1)
59 chunks[0].should.have.property('foo', 'bar');
60 done();
61 });
62 })
63
64 it('emits a raw buffer if we do not want to parse JSON', function(done) {
65 var stream = needle.get('localhost:' + port, { parse: false })
66
67 var chunks = [];
68 stream.on('readable', function () {
69 while (chunk = this.read()) {
70 Buffer.isBuffer(chunk).should.be.true;
71 chunks.push(chunk);
72 }
73 })
74
75 stream.on('done', function() {
76 var body = Buffer.concat(chunks).toString();
77 body.should.equal('{"foo":"bar"}')
78 done();
79 });
80 })
81
82 })
83 })
84
85 describe('when the server sends back what was posted to it', function () {
86 var file = 'asdf.txt';
87
88 before(function(done){
89 server = http.createServer(function(req, res) {
90 res.setHeader('Content-Type', 'application/octet')
91 req.pipe(res);
92 }).listen(port);
93
94 fs.writeFile(file, 'contents of stream', done);
95 });
96
97 after(function(done){
98 server.close();
99 fs.unlink(file, done);
100 })
101
102 it('can PUT a stream', function (done) {
103 var stream = needle.put('localhost:' + port, fs.createReadStream(file), { stream: true });
104
105 var chunks = [];
106 stream.on('readable', function () {
107 while (chunk = this.read()) {
108 Buffer.isBuffer(chunk).should.be.true;
109 chunks.push(chunk);
110 }
111 })
112
113 stream.on('end', function () {
114 var body = Buffer.concat(chunks).toString();
115 body.should.equal('contents of stream')
116 done();
117 });
118 });
119
120 it('can PATCH a stream', function (done) {
121 var stream = needle.patch('localhost:' + port, fs.createReadStream(file), { stream: true });
122
123 var chunks = [];
124 stream.on('readable', function () {
125 while (chunk = this.read()) {
126 Buffer.isBuffer(chunk).should.be.true;
127 chunks.push(chunk);
128 }
129 })
130
131 stream.on('end', function () {
132 var body = Buffer.concat(chunks).toString();
133 body.should.equal('contents of stream')
134 done();
135 });
136 });
137 })
138})
Note: See TracBrowser for help on using the repository browser.