1 | var server = require('./server') |
---|
2 | , events = require('events') |
---|
3 | , stream = require('stream') |
---|
4 | , assert = require('assert') |
---|
5 | , fs = require('fs') |
---|
6 | , request = require('../index') |
---|
7 | , path = require('path') |
---|
8 | , util = require('util') |
---|
9 | ; |
---|
10 | |
---|
11 | var s = server.createServer(3453); |
---|
12 | |
---|
13 | function ValidationStream(str) { |
---|
14 | this.str = str |
---|
15 | this.buf = '' |
---|
16 | this.on('data', function (data) { |
---|
17 | this.buf += data |
---|
18 | }) |
---|
19 | this.on('end', function () { |
---|
20 | assert.equal(this.str, this.buf) |
---|
21 | }) |
---|
22 | this.writable = true |
---|
23 | } |
---|
24 | util.inherits(ValidationStream, stream.Stream) |
---|
25 | ValidationStream.prototype.write = function (chunk) { |
---|
26 | this.emit('data', chunk) |
---|
27 | } |
---|
28 | ValidationStream.prototype.end = function (chunk) { |
---|
29 | if (chunk) emit('data', chunk) |
---|
30 | this.emit('end') |
---|
31 | } |
---|
32 | |
---|
33 | s.listen(s.port, function () { |
---|
34 | counter = 0; |
---|
35 | |
---|
36 | var check = function () { |
---|
37 | counter = counter - 1 |
---|
38 | if (counter === 0) { |
---|
39 | console.log('All tests passed.') |
---|
40 | setTimeout(function () { |
---|
41 | process.exit(); |
---|
42 | }, 500) |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | // Test pipeing to a request object |
---|
47 | s.once('/push', server.createPostValidator("mydata")); |
---|
48 | |
---|
49 | var mydata = new stream.Stream(); |
---|
50 | mydata.readable = true |
---|
51 | |
---|
52 | counter++ |
---|
53 | var r1 = request.put({url:'http://localhost:3453/push'}, function () { |
---|
54 | check(); |
---|
55 | }) |
---|
56 | mydata.pipe(r1) |
---|
57 | |
---|
58 | mydata.emit('data', 'mydata'); |
---|
59 | mydata.emit('end'); |
---|
60 | |
---|
61 | // Test pipeing to a request object with a json body |
---|
62 | s.once('/push-json', server.createPostValidator("{\"foo\":\"bar\"}", "application/json")); |
---|
63 | |
---|
64 | var mybodydata = new stream.Stream(); |
---|
65 | mybodydata.readable = true |
---|
66 | |
---|
67 | counter++ |
---|
68 | var r2 = request.put({url:'http://localhost:3453/push-json',json:true}, function () { |
---|
69 | check(); |
---|
70 | }) |
---|
71 | mybodydata.pipe(r2) |
---|
72 | |
---|
73 | mybodydata.emit('data', JSON.stringify({foo:"bar"})); |
---|
74 | mybodydata.emit('end'); |
---|
75 | |
---|
76 | // Test pipeing from a request object. |
---|
77 | s.once('/pull', server.createGetResponse("mypulldata")); |
---|
78 | |
---|
79 | var mypulldata = new stream.Stream(); |
---|
80 | mypulldata.writable = true |
---|
81 | |
---|
82 | counter++ |
---|
83 | request({url:'http://localhost:3453/pull'}).pipe(mypulldata) |
---|
84 | |
---|
85 | var d = ''; |
---|
86 | |
---|
87 | mypulldata.write = function (chunk) { |
---|
88 | d += chunk; |
---|
89 | } |
---|
90 | mypulldata.end = function () { |
---|
91 | assert.equal(d, 'mypulldata'); |
---|
92 | check(); |
---|
93 | }; |
---|
94 | |
---|
95 | |
---|
96 | s.on('/cat', function (req, resp) { |
---|
97 | if (req.method === "GET") { |
---|
98 | resp.writeHead(200, {'content-type':'text/plain-test', 'content-length':4}); |
---|
99 | resp.end('asdf') |
---|
100 | } else if (req.method === "PUT") { |
---|
101 | assert.equal(req.headers['content-type'], 'text/plain-test'); |
---|
102 | assert.equal(req.headers['content-length'], 4) |
---|
103 | var validate = ''; |
---|
104 | |
---|
105 | req.on('data', function (chunk) {validate += chunk}) |
---|
106 | req.on('end', function () { |
---|
107 | resp.writeHead(201); |
---|
108 | resp.end(); |
---|
109 | assert.equal(validate, 'asdf'); |
---|
110 | check(); |
---|
111 | }) |
---|
112 | } |
---|
113 | }) |
---|
114 | s.on('/pushjs', function (req, resp) { |
---|
115 | if (req.method === "PUT") { |
---|
116 | assert.equal(req.headers['content-type'], 'application/javascript'); |
---|
117 | check(); |
---|
118 | } |
---|
119 | }) |
---|
120 | s.on('/catresp', function (req, resp) { |
---|
121 | request.get('http://localhost:3453/cat').pipe(resp) |
---|
122 | }) |
---|
123 | s.on('/doodle', function (req, resp) { |
---|
124 | if (req.headers['x-oneline-proxy']) { |
---|
125 | resp.setHeader('x-oneline-proxy', 'yup') |
---|
126 | } |
---|
127 | resp.writeHead('200', {'content-type':'image/jpeg'}) |
---|
128 | fs.createReadStream(path.join(__dirname, 'googledoodle.jpg')).pipe(resp) |
---|
129 | }) |
---|
130 | s.on('/onelineproxy', function (req, resp) { |
---|
131 | var x = request('http://localhost:3453/doodle') |
---|
132 | req.pipe(x) |
---|
133 | x.pipe(resp) |
---|
134 | }) |
---|
135 | |
---|
136 | counter++ |
---|
137 | fs.createReadStream(__filename).pipe(request.put('http://localhost:3453/pushjs')) |
---|
138 | |
---|
139 | counter++ |
---|
140 | request.get('http://localhost:3453/cat').pipe(request.put('http://localhost:3453/cat')) |
---|
141 | |
---|
142 | counter++ |
---|
143 | request.get('http://localhost:3453/catresp', function (e, resp, body) { |
---|
144 | assert.equal(resp.headers['content-type'], 'text/plain-test'); |
---|
145 | assert.equal(resp.headers['content-length'], 4) |
---|
146 | check(); |
---|
147 | }) |
---|
148 | |
---|
149 | var doodleWrite = fs.createWriteStream(path.join(__dirname, 'test.jpg')) |
---|
150 | |
---|
151 | counter++ |
---|
152 | request.get('http://localhost:3453/doodle').pipe(doodleWrite) |
---|
153 | |
---|
154 | doodleWrite.on('close', function () { |
---|
155 | assert.deepEqual(fs.readFileSync(path.join(__dirname, 'googledoodle.jpg')), fs.readFileSync(path.join(__dirname, 'test.jpg'))) |
---|
156 | check() |
---|
157 | }) |
---|
158 | |
---|
159 | process.on('exit', function () { |
---|
160 | fs.unlinkSync(path.join(__dirname, 'test.jpg')) |
---|
161 | }) |
---|
162 | |
---|
163 | counter++ |
---|
164 | request.get({uri:'http://localhost:3453/onelineproxy', headers:{'x-oneline-proxy':'nope'}}, function (err, resp, body) { |
---|
165 | assert.equal(resp.headers['x-oneline-proxy'], 'yup') |
---|
166 | check() |
---|
167 | }) |
---|
168 | |
---|
169 | s.on('/afterresponse', function (req, resp) { |
---|
170 | resp.write('d') |
---|
171 | resp.end() |
---|
172 | }) |
---|
173 | |
---|
174 | counter++ |
---|
175 | var afterresp = request.post('http://localhost:3453/afterresponse').on('response', function () { |
---|
176 | var v = new ValidationStream('d') |
---|
177 | afterresp.pipe(v) |
---|
178 | v.on('end', check) |
---|
179 | }) |
---|
180 | |
---|
181 | s.on('/forward1', function (req, resp) { |
---|
182 | resp.writeHead(302, {location:'/forward2'}) |
---|
183 | resp.end() |
---|
184 | }) |
---|
185 | s.on('/forward2', function (req, resp) { |
---|
186 | resp.writeHead('200', {'content-type':'image/png'}) |
---|
187 | resp.write('d') |
---|
188 | resp.end() |
---|
189 | }) |
---|
190 | |
---|
191 | counter++ |
---|
192 | var validateForward = new ValidationStream('d') |
---|
193 | validateForward.on('end', check) |
---|
194 | request.get('http://localhost:3453/forward1').pipe(validateForward) |
---|
195 | |
---|
196 | // Test pipe options |
---|
197 | s.once('/opts', server.createGetResponse('opts response')); |
---|
198 | |
---|
199 | var optsStream = new stream.Stream(); |
---|
200 | optsStream.writable = true |
---|
201 | |
---|
202 | var optsData = ''; |
---|
203 | optsStream.write = function (buf) { |
---|
204 | optsData += buf; |
---|
205 | if (optsData === 'opts response') { |
---|
206 | setTimeout(check, 10); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | optsStream.end = function () { |
---|
211 | assert.fail('end called') |
---|
212 | }; |
---|
213 | |
---|
214 | counter++ |
---|
215 | request({url:'http://localhost:3453/opts'}).pipe(optsStream, { end : false }) |
---|
216 | }) |
---|