test-onelineproxy.js 1.1 KB

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