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.

68 lines
1.5 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. const PassThrough = require('stream').PassThrough
  7. test('static', function (t) {
  8. t.plan(1)
  9. const app = localWebServer({
  10. static: {
  11. root: __dirname + '/static',
  12. options: {
  13. index: 'file.txt'
  14. }
  15. }
  16. })
  17. const server = http.createServer(app.callback())
  18. server.listen(8100)
  19. request('http://localhost:8100')
  20. .then(response => {
  21. t.strictEqual(response.data, 'test\n')
  22. })
  23. .then(() => server.close())
  24. })
  25. test('serve-index', function (t) {
  26. t.plan(2)
  27. const app = localWebServer({
  28. serveIndex: {
  29. path: __dirname + '/static',
  30. options: {
  31. icons: true
  32. }
  33. }
  34. })
  35. const server = http.createServer(app.callback())
  36. server.listen(8100)
  37. request('http://localhost:8100/')
  38. .then(response => {
  39. t.ok(/listing directory/.test(response.data))
  40. t.ok(/class="icon/.test(response.data))
  41. })
  42. .then(() => server.close())
  43. })
  44. test('log: common', function (t) {
  45. t.plan(1)
  46. const stream = PassThrough()
  47. stream.on('readable', () => {
  48. let chunk = stream.read()
  49. if (chunk) t.ok(/GET/.test(chunk.toString()))
  50. })
  51. const app = localWebServer({
  52. logger: {
  53. format: 'common',
  54. options: {
  55. stream: stream
  56. }
  57. }
  58. })
  59. const server = http.createServer(app.callback())
  60. server.listen(8100)
  61. request('http://localhost:8100/')
  62. .then(() => server.close())
  63. })