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.

89 lines
2.8 KiB

  1. 'use strict'
  2. const test = require('tape')
  3. const request = require('req-then')
  4. const localWebServer = require('../')
  5. const http = require('http')
  6. function launchServer (app, options) {
  7. options = options || {}
  8. const path = `http://localhost:8100${options.path || '/'}`
  9. const server = http.createServer(app.callback())
  10. return server.listen(options.port || 8100, () => {
  11. const req = request(path, options.reqOptions)
  12. if (options.onSuccess) req.then(options.onSuccess)
  13. if (!options.leaveOpen) req.then(() => server.close())
  14. req.catch(err => console.error('LAUNCH ERROR', err.stack))
  15. })
  16. }
  17. function checkResponse (t, status, body) {
  18. return function (response) {
  19. if (status) t.strictEqual(response.res.statusCode, status)
  20. if (body) t.ok(body.test(response.data))
  21. }
  22. }
  23. test('rewrite: proxy', function (t) {
  24. t.plan(2)
  25. const app = localWebServer({
  26. log: { format: 'none' },
  27. static: { root: __dirname + '/fixture/rewrite' },
  28. rewrite: [ { from: '/test/*', to: 'http://registry.npmjs.org/$1' } ]
  29. })
  30. launchServer(app, { path: '/test/', onSuccess: response => {
  31. t.strictEqual(response.res.statusCode, 200)
  32. t.ok(/db_name/.test(response.data))
  33. }})
  34. })
  35. test('rewrite: proxy, POST', function (t) {
  36. t.plan(1)
  37. const app = localWebServer({
  38. log: { format: 'none' },
  39. static: { root: __dirname + '/fixture/rewrite' },
  40. rewrite: [ { from: '/test/*', to: 'http://registry.npmjs.org/' } ]
  41. })
  42. const server = http.createServer(app.callback())
  43. server.listen(8100, () => {
  44. request('http://localhost:8100/test/', { data: {} })
  45. .then(checkResponse(t, 405))
  46. .then(server.close.bind(server))
  47. })
  48. })
  49. test('rewrite: proxy, two url tokens', function (t) {
  50. t.plan(2)
  51. const app = localWebServer({
  52. log: { format: 'none' },
  53. rewrite: [ { from: '/:package/:version', to: 'http://registry.npmjs.org/:package/:version' } ]
  54. })
  55. launchServer(app, { path: '/command-line-args/1.0.0', onSuccess: response => {
  56. t.strictEqual(response.res.statusCode, 200)
  57. t.ok(/command-line-args/.test(response.data))
  58. }})
  59. })
  60. test('rewrite: proxy with port', function (t) {
  61. t.plan(2)
  62. const one = localWebServer({
  63. log: { format: 'none' },
  64. static: { root: __dirname + '/fixture/one' }
  65. })
  66. const two = localWebServer({
  67. log: { format: 'none' },
  68. static: { root: __dirname + '/fixture/spa' },
  69. rewrite: [ { from: '/test/*', to: 'http://localhost:9000/$1' } ]
  70. })
  71. const server1 = http.createServer(one.callback())
  72. const server2 = http.createServer(two.callback())
  73. server1.listen(9000, () => {
  74. server2.listen(8100, () => {
  75. request('http://localhost:8100/test/file.txt').then(response => {
  76. t.strictEqual(response.res.statusCode, 200)
  77. t.ok(/one/.test(response.data))
  78. server1.close()
  79. server2.close()
  80. })
  81. })
  82. })
  83. })