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.

203 lines
5.4 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
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(options.port || 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.ok(/test/.test(response.data))
  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('single page app', function (t) {
  50. t.plan(4)
  51. const app = localWebServer({
  52. log: { format: 'none' },
  53. static: { root: __dirname + '/fixture/spa' },
  54. spa: 'one.txt'
  55. })
  56. const server = launchServer(app, { leaveOpen: true })
  57. request('http://localhost:8100/test').then(response => {
  58. t.strictEqual(response.res.statusCode, 200)
  59. t.ok(/one/.test(response.data))
  60. request('http://localhost:8100/two.txt').then(response => {
  61. t.strictEqual(response.res.statusCode, 200)
  62. t.ok(/two/.test(response.data))
  63. server.close()
  64. })
  65. })
  66. })
  67. test('log: common', function (t) {
  68. t.plan(1)
  69. const stream = PassThrough()
  70. stream.on('readable', () => {
  71. let chunk = stream.read()
  72. if (chunk) t.ok(/GET/.test(chunk.toString()))
  73. })
  74. const app = localWebServer({
  75. log: {
  76. format: 'common',
  77. options: {
  78. stream: stream
  79. }
  80. }
  81. })
  82. launchServer(app)
  83. })
  84. test('compress', function (t) {
  85. t.plan(1)
  86. const app = localWebServer({
  87. compress: true,
  88. log: { format: 'none' },
  89. static: { root: __dirname + '/fixture' }
  90. })
  91. launchServer(
  92. app,
  93. {
  94. reqOptions: { headers: { 'Accept-Encoding': 'gzip' } },
  95. path: '/big-file.txt',
  96. onSuccess: response => {
  97. t.strictEqual(response.res.headers['content-encoding'], 'gzip')
  98. }
  99. }
  100. )
  101. })
  102. test('mime', function (t) {
  103. t.plan(2)
  104. const app = localWebServer({
  105. log: { format: 'none' },
  106. static: { root: __dirname + '/fixture' },
  107. mime: { 'text/plain': [ 'php' ] }
  108. })
  109. launchServer(app, { path: '/something.php', onSuccess: response => {
  110. t.strictEqual(response.res.statusCode, 200)
  111. t.ok(/text\/plain/.test(response.res.headers['content-type']))
  112. }})
  113. })
  114. test('forbid', function (t) {
  115. t.plan(2)
  116. const app = localWebServer({
  117. log: { format: 'none' },
  118. static: { root: __dirname + '/fixture/forbid' },
  119. forbid: [ '*.php', '*.html' ]
  120. })
  121. const server = launchServer(app, { leaveOpen: true })
  122. request('http://localhost:8100/two.php')
  123. .then(response => {
  124. t.strictEqual(response.res.statusCode, 403)
  125. request('http://localhost:8100/one.html')
  126. .then(response => {
  127. t.strictEqual(response.res.statusCode, 403)
  128. server.close()
  129. })
  130. })
  131. })
  132. test('rewrite: local', function (t) {
  133. t.plan(1)
  134. const app = localWebServer({
  135. log: { format: 'none' },
  136. static: { root: __dirname + '/fixture/rewrite' },
  137. rewrite: [ { from: '/two.html', to: '/one.html' } ]
  138. })
  139. launchServer(app, { path: '/two.html', onSuccess: response => {
  140. t.ok(/one/.test(response.data))
  141. }})
  142. })
  143. test('rewrite: proxy', function (t) {
  144. t.plan(2)
  145. const app = localWebServer({
  146. log: { format: 'none' },
  147. static: { root: __dirname + '/fixture/rewrite' },
  148. rewrite: [ { from: '/test/*', to: 'http://registry.npmjs.org/$1' } ]
  149. })
  150. launchServer(app, { path: '/test/', onSuccess: response => {
  151. t.strictEqual(response.res.statusCode, 200)
  152. t.ok(/db_name/.test(response.data))
  153. }})
  154. })
  155. test('rewrite: proxy, two url tokens', function (t) {
  156. t.plan(2)
  157. const app = localWebServer({
  158. log: { format: 'none' },
  159. rewrite: [ { from: '/:package/:version', to: 'http://registry.npmjs.org/:package/:version' } ]
  160. })
  161. launchServer(app, { path: '/command-line-args/1.0.0', onSuccess: response => {
  162. t.strictEqual(response.res.statusCode, 200)
  163. t.ok(/command-line-args/.test(response.data))
  164. }})
  165. })
  166. test('rewrite: proxy with port', function (t) {
  167. t.plan(2)
  168. const one = localWebServer({
  169. log: { format: 'none' },
  170. static: { root: __dirname + '/fixture/one' }
  171. })
  172. const two = localWebServer({
  173. log: { format: 'none' },
  174. static: { root: __dirname + '/fixture/spa' },
  175. rewrite: [ { from: '/test/*', to: 'http://localhost:9000/$1' } ]
  176. })
  177. const server1 = http.createServer(one.callback())
  178. const server2 = http.createServer(two.callback())
  179. server1.listen(9000, () => {
  180. server2.listen(8100, () => {
  181. request('http://localhost:8100/test/file.txt').then(response => {
  182. t.strictEqual(response.res.statusCode, 200)
  183. t.ok(/one/.test(response.data))
  184. server1.close()
  185. server2.close()
  186. })
  187. })
  188. })
  189. })