You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.0 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. 'use strict'
  2. const test = require('tape')
  3. const request = require('req-then')
  4. const LocalWebServer = require('../../')
  5. const http = require('http')
  6. const c = require('../common')
  7. test('rewrite: proxy', function (t) {
  8. t.plan(2)
  9. const ws = new LocalWebServer()
  10. ws.addRewrite([
  11. { from: '/test/*', to: 'http://registry.npmjs.org/$1' }
  12. ])
  13. const server = ws.getServer()
  14. server.listen(8100, () => {
  15. request('http://localhost:8100/test/')
  16. .then(c.checkResponse(t, 200, /db_name/))
  17. .then(server.close.bind(server))
  18. .catch(c.fail(t))
  19. })
  20. })
  21. test('rewrite: proxy, POST', function (t) {
  22. t.plan(1)
  23. const ws = new LocalWebServer()
  24. ws.addRewrite([
  25. { from: '/test/*', to: 'http://registry.npmjs.org/' }
  26. ])
  27. const server = ws.getServer()
  28. server.listen(8100, () => {
  29. request('http://localhost:8100/test/', { data: {} })
  30. .then(c.checkResponse(t, 405))
  31. .then(server.close.bind(server))
  32. .catch(c.fail(t))
  33. })
  34. })
  35. test('rewrite: proxy, two url tokens', function (t) {
  36. t.plan(2)
  37. const ws = new LocalWebServer()
  38. ws.addRewrite([
  39. { from: '/:package/:version', to: 'http://registry.npmjs.org/:package/:version' }
  40. ])
  41. const server = ws.getServer()
  42. server.listen(8100, () => {
  43. request('http://localhost:8100/command-line-args/1.0.0')
  44. .then(c.checkResponse(t, 200, /command-line-args/))
  45. .then(server.close.bind(server))
  46. .catch(c.fail(t))
  47. })
  48. })
  49. test('rewrite: proxy with port', function (t) {
  50. t.plan(2)
  51. const ws1 = new LocalWebServer()
  52. ws1.addStatic(__dirname)
  53. const ws2 = new LocalWebServer()
  54. ws2.addRewrite([
  55. { from: '/test/*', to: 'http://localhost:9000/$1' }
  56. ])
  57. const server1 = ws1.getServer()
  58. const server2 = ws2.getServer()
  59. server1.listen(9000, () => {
  60. server2.listen(8100, () => {
  61. request('http://localhost:8100/test/file.txt')
  62. .then(c.checkResponse(t, 200, /one/))
  63. .then(server1.close.bind(server1))
  64. .then(server2.close.bind(server2))
  65. .catch(c.fail(t))
  66. })
  67. })
  68. })