forked from request/request
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-onelineproxy.js
More file actions
46 lines (41 loc) · 1.13 KB
/
Copy pathtest-onelineproxy.js
File metadata and controls
46 lines (41 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
var http = require('http')
, assert = require('assert')
, request = require('../index')
;
var server = http.createServer(function (req, resp) {
resp.statusCode = 200
if (req.url === '/get') {
assert.equal(req.method, 'GET')
resp.write('content')
resp.end()
return
}
if (req.url === '/put') {
var x = ''
assert.equal(req.method, 'PUT')
req.on('data', function (chunk) {
x += chunk
})
req.on('end', function () {
assert.equal(x, 'content')
resp.write('success')
resp.end()
})
return
}
if (req.url === '/proxy') {
assert.equal(req.method, 'PUT')
return req.pipe(request('http://localhost:8080/put')).pipe(resp)
}
if (req.url === '/test') {
return request('http://localhost:8080/get').pipe(request.put('http://localhost:8080/proxy')).pipe(resp)
}
throw new Error('Unknown url', req.url)
}).listen(8080, function () {
request('http://localhost:8080/test', function (e, resp, body) {
if (e) throw e
if (resp.statusCode !== 200) throw new Error('statusCode not 200 ' + resp.statusCode)
assert.equal(body, 'success')
server.close()
})
})