c239f8921cf45720f254ea29225e6c500d6dfa0c
[platform/upstream/nodejs.git] / deps / npm / node_modules / request / tests / test-onelineproxy.js
1 var http = require('http')
2   , assert = require('assert')
3   , request = require('../index')
4   ;
5
6 var server = http.createServer(function (req, resp) {
7   resp.statusCode = 200
8   if (req.url === '/get') {
9     assert.equal(req.method, 'GET')
10     resp.write('content')
11     resp.end()
12     return
13   }
14   if (req.url === '/put') {
15     var x = ''
16     assert.equal(req.method, 'PUT')
17     req.on('data', function (chunk) {
18       x += chunk
19     })
20     req.on('end', function () {
21       assert.equal(x, 'content')
22       resp.write('success')
23       resp.end()
24     })
25     return
26   }
27   if (req.url === '/proxy') {
28     assert.equal(req.method, 'PUT')
29     return req.pipe(request('http://localhost:8080/put')).pipe(resp)
30   }
31
32   if (req.url === '/test') {
33     return request('http://localhost:8080/get').pipe(request.put('http://localhost:8080/proxy')).pipe(resp)
34   }
35   throw new Error('Unknown url', req.url)
36 }).listen(8080, function () {
37   request('http://localhost:8080/test', function (e, resp, body) {
38     if (e) throw e
39     if (resp.statusCode !== 200) throw new Error('statusCode not 200 ' + resp.statusCode)
40     assert.equal(body, 'success')
41     server.close()
42   })
43 })
44
45
46