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.

147 lines
3.7 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
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 PassThrough = require('stream').PassThrough
  7. function launchServer (app, options) {
  8. options = options || {}
  9. const path = `http://localhost:8100${options.path || '/'}`
  10. const server = http.createServer(app.callback())
  11. return server.listen(8100, () => {
  12. const req = request(path, options.reqOptions)
  13. if (options.onSuccess) req.then(options.onSuccess)
  14. if (!options.leaveOpen) req.then(() => server.close())
  15. req.catch(err => console.error('LAUNCH ERROR', err.stack))
  16. })
  17. }
  18. test('static', function (t) {
  19. t.plan(1)
  20. const app = localWebServer({
  21. log: { format: 'none' },
  22. static: {
  23. root: __dirname + '/fixture',
  24. options: {
  25. index: 'file.txt'
  26. }
  27. }
  28. })
  29. launchServer(app, { onSuccess: response => {
  30. t.strictEqual(response.data, 'test\n')
  31. }})
  32. })
  33. test('serve-index', function (t) {
  34. t.plan(2)
  35. const app = localWebServer({
  36. log: { format: 'none' },
  37. serveIndex: {
  38. path: __dirname + '/fixture',
  39. options: {
  40. icons: true
  41. }
  42. }
  43. })
  44. launchServer(app, { onSuccess: response => {
  45. t.ok(/listing directory/.test(response.data))
  46. t.ok(/class="icon/.test(response.data))
  47. }})
  48. })
  49. test('log: common', function (t) {
  50. t.plan(1)
  51. const stream = PassThrough()
  52. stream.on('readable', () => {
  53. let chunk = stream.read()
  54. if (chunk) t.ok(/GET/.test(chunk.toString()))
  55. })
  56. const app = localWebServer({
  57. log: {
  58. format: 'common',
  59. options: {
  60. stream: stream
  61. }
  62. }
  63. })
  64. launchServer(app)
  65. })
  66. test('compress', function(t){
  67. t.plan(1)
  68. const app = localWebServer({
  69. compress: true,
  70. log: { format: 'none' },
  71. static: { root: __dirname + '/fixture' }
  72. })
  73. launchServer(
  74. app,
  75. {
  76. reqOptions: { headers: { 'Accept-Encoding': 'gzip' } },
  77. path: '/big-file.txt',
  78. onSuccess: response => {
  79. t.strictEqual(response.res.headers['content-encoding'], 'gzip')
  80. }
  81. }
  82. )
  83. })
  84. test('mime', function(t){
  85. t.plan(2)
  86. const app = localWebServer({
  87. log: { format: 'none' },
  88. static: { root: __dirname + '/fixture' },
  89. mime: { 'text/plain': [ 'php' ]}
  90. })
  91. launchServer(app, { path: '/something.php', onSuccess: response => {
  92. t.strictEqual(response.res.statusCode, 200)
  93. t.ok(/text\/plain/.test(response.res.headers['content-type']))
  94. }})
  95. })
  96. test('forbid', function (t) {
  97. t.plan(2)
  98. const app = localWebServer({
  99. log: { format: 'none' },
  100. static: { root: __dirname + '/fixture/forbid' },
  101. forbid: [ '*.php', '*.html' ]
  102. })
  103. const server = launchServer(app, { leaveOpen: true })
  104. request('http://localhost:8100/two.php')
  105. .then(response => {
  106. t.strictEqual(response.res.statusCode, 403)
  107. request('http://localhost:8100/one.html')
  108. .then(response => {
  109. t.strictEqual(response.res.statusCode, 403)
  110. server.close()
  111. })
  112. })
  113. })
  114. test('rewrite: local', function(t){
  115. t.plan(1)
  116. const app = localWebServer({
  117. log: { format: 'none' },
  118. static: { root: __dirname + '/fixture/rewrite' },
  119. rewrite: [ { from: '/two.html', to: '/one.html'} ]
  120. })
  121. launchServer(app, { path: '/two.html', onSuccess: response => {
  122. t.strictEqual(response.data, 'one\n')
  123. }})
  124. })
  125. test('rewrite: proxy', function(t){
  126. t.plan(2)
  127. const app = localWebServer({
  128. log: { format: 'none' },
  129. static: { root: __dirname + '/fixture/rewrite' },
  130. rewrite: [ { from: '/test/*', to: 'http://registry.npmjs.org/$1'} ]
  131. })
  132. launchServer(app, { path: '/test/', onSuccess: response => {
  133. t.strictEqual(response.res.statusCode, 200)
  134. t.ok(/db_name/.test(response.data))
  135. }})
  136. })