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.

145 lines
3.5 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
  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('log: common', function (t) {
  19. t.plan(1)
  20. const stream = PassThrough()
  21. stream.on('readable', () => {
  22. let chunk = stream.read()
  23. if (chunk) t.ok(/GET/.test(chunk.toString()))
  24. })
  25. const app = localWebServer({
  26. log: {
  27. format: 'common',
  28. options: {
  29. stream: stream
  30. }
  31. }
  32. })
  33. launchServer(app)
  34. })
  35. test('static', function (t) {
  36. t.plan(1)
  37. const app = localWebServer({
  38. log: { format: 'none' },
  39. static: {
  40. root: __dirname + '/fixture',
  41. options: {
  42. index: 'file.txt'
  43. }
  44. }
  45. })
  46. launchServer(app, { onSuccess: response => {
  47. t.strictEqual(response.data, 'test\n')
  48. }})
  49. })
  50. test('serve-index', function (t) {
  51. t.plan(2)
  52. const app = localWebServer({
  53. log: { format: 'none' },
  54. serveIndex: {
  55. path: __dirname + '/fixture',
  56. options: {
  57. icons: true
  58. }
  59. }
  60. })
  61. launchServer(app, { onSuccess: response => {
  62. t.ok(/listing directory/.test(response.data))
  63. t.ok(/class="icon/.test(response.data))
  64. }})
  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(1)
  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.ok(/text\/plain/.test(response.res.headers['content-type']))
  93. }})
  94. })
  95. test('forbid', function (t) {
  96. t.plan(2)
  97. const app = localWebServer({
  98. log: { format: 'none' },
  99. static: { root: __dirname + '/fixture' },
  100. forbid: [ /php$/, /html$/ ]
  101. })
  102. const server = launchServer(app, { leaveOpen: true })
  103. request('http://localhost:8100/something.php')
  104. .then(response => {
  105. t.strictEqual(response.res.statusCode, 403)
  106. request('http://localhost:8100/ajax.html')
  107. .then(response => {
  108. t.strictEqual(response.res.statusCode, 403)
  109. server.close()
  110. })
  111. })
  112. })
  113. test.skip('directories: should serve index and static files', function(t){
  114. t.plan(1)
  115. const app = localWebServer({
  116. log: { format: 'none' },
  117. directories: [
  118. __dirname + '/fixture/one'
  119. ]
  120. })
  121. launchServer(app, { path: '/something.php', onSuccess: response => {
  122. t.ok(/text\/plain/.test(response.res.headers['content-type']))
  123. }})
  124. })
  125. test('proxy', function(t){
  126. t.plan(1)
  127. const app = localWebServer({
  128. log: { format: 'none' },
  129. proxy: []
  130. })
  131. launchServer(app, { path: '/something.php', onSuccess: response => {
  132. t.ok(/text\/plain/.test(response.res.headers['content-type']))
  133. }})
  134. })