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.

94 lines
2.2 KiB

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, reqOptions, path, onSuccess) {
  8. path = `http://localhost:8100${path || '/'}`
  9. const server = http.createServer(app.callback())
  10. server.listen(8100, () => {
  11. const req = request(path, reqOptions)
  12. if (onSuccess) req.then(onSuccess)
  13. req.then(() => server.close())
  14. req.catch(err => console.error('LAUNCH ERROR', err.stack))
  15. })
  16. }
  17. test('log: common', function (t) {
  18. t.plan(1)
  19. const stream = PassThrough()
  20. stream.on('readable', () => {
  21. let chunk = stream.read()
  22. if (chunk) t.ok(/GET/.test(chunk.toString()))
  23. })
  24. const app = localWebServer({
  25. log: {
  26. format: 'common',
  27. options: {
  28. stream: stream
  29. }
  30. }
  31. })
  32. launchServer(app)
  33. })
  34. test('static', function (t) {
  35. t.plan(1)
  36. const app = localWebServer({
  37. log: { format: 'none' },
  38. static: {
  39. root: __dirname + '/fixture',
  40. options: {
  41. index: 'file.txt'
  42. }
  43. }
  44. })
  45. launchServer(app, null, '/', response => {
  46. t.strictEqual(response.data, 'test\n')
  47. })
  48. })
  49. test('serve-index', function (t) {
  50. t.plan(2)
  51. const app = localWebServer({
  52. log: { format: 'none' },
  53. serveIndex: {
  54. path: __dirname + '/fixture',
  55. options: {
  56. icons: true
  57. }
  58. }
  59. })
  60. launchServer(app, null, null, response => {
  61. t.ok(/listing directory/.test(response.data))
  62. t.ok(/class="icon/.test(response.data))
  63. })
  64. })
  65. test('compress', function(t){
  66. t.plan(1)
  67. const app = localWebServer({
  68. compress: true,
  69. log: { format: 'none' },
  70. static: { root: __dirname + '/fixture' }
  71. })
  72. launchServer(app, { headers: { 'Accept-Encoding': 'gzip' } }, '/big-file.txt', response => {
  73. t.strictEqual(response.res.headers['content-encoding'], 'gzip')
  74. })
  75. })
  76. test('mime', function(t){
  77. t.plan(1)
  78. const app = localWebServer({
  79. log: { format: 'none' },
  80. static: { root: __dirname + '/fixture' },
  81. mime: { 'text/plain': [ 'php' ]}
  82. })
  83. launchServer(app, null, '/something.php', response => {
  84. t.ok(/text\/plain/.test(response.res.headers['content-type']))
  85. })
  86. })