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.

299 lines
7.8 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
  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. function checkResponse (t, status, body) {
  19. return function (response) {
  20. if (status) t.strictEqual(response.res.statusCode, status)
  21. if (body) t.ok(body.test(response.data))
  22. }
  23. }
  24. test('serve-index', function (t) {
  25. t.plan(2)
  26. const app = localWebServer({
  27. log: { format: 'none' },
  28. serveIndex: {
  29. path: __dirname + '/fixture',
  30. options: {
  31. icons: true
  32. }
  33. }
  34. })
  35. launchServer(app, { onSuccess: response => {
  36. t.ok(/listing directory/.test(response.data))
  37. t.ok(/class="icon/.test(response.data))
  38. }})
  39. })
  40. test('single page app', function (t) {
  41. t.plan(4)
  42. const app = localWebServer({
  43. log: { format: 'none' },
  44. static: { root: __dirname + '/fixture/spa' },
  45. spa: 'one.txt'
  46. })
  47. const server = launchServer(app, { leaveOpen: true })
  48. request('http://localhost:8100/test').then(response => {
  49. t.strictEqual(response.res.statusCode, 200)
  50. t.ok(/one/.test(response.data))
  51. request('http://localhost:8100/two.txt').then(response => {
  52. t.strictEqual(response.res.statusCode, 200)
  53. t.ok(/two/.test(response.data))
  54. server.close()
  55. })
  56. })
  57. })
  58. test('log: common', function (t) {
  59. t.plan(1)
  60. const stream = PassThrough()
  61. stream.on('readable', () => {
  62. let chunk = stream.read()
  63. if (chunk) t.ok(/GET/.test(chunk.toString()))
  64. })
  65. const app = localWebServer({
  66. log: {
  67. format: 'common',
  68. options: {
  69. stream: stream
  70. }
  71. }
  72. })
  73. launchServer(app)
  74. })
  75. test('compress', function (t) {
  76. t.plan(1)
  77. const app = localWebServer({
  78. compress: true,
  79. log: { format: 'none' },
  80. static: { root: __dirname + '/fixture' }
  81. })
  82. launchServer(
  83. app,
  84. {
  85. reqOptions: { headers: { 'Accept-Encoding': 'gzip' } },
  86. path: '/big-file.txt',
  87. onSuccess: response => {
  88. t.strictEqual(response.res.headers['content-encoding'], 'gzip')
  89. }
  90. }
  91. )
  92. })
  93. test('mime', function (t) {
  94. t.plan(2)
  95. const app = localWebServer({
  96. log: { format: 'none' },
  97. static: { root: __dirname + '/fixture' },
  98. mime: { 'text/plain': [ 'php' ] }
  99. })
  100. launchServer(app, { path: '/something.php', onSuccess: response => {
  101. t.strictEqual(response.res.statusCode, 200)
  102. t.ok(/text\/plain/.test(response.res.headers['content-type']))
  103. }})
  104. })
  105. test('forbid', function (t) {
  106. t.plan(2)
  107. const app = localWebServer({
  108. log: { format: 'none' },
  109. static: { root: __dirname + '/fixture/forbid' },
  110. forbid: [ '*.php', '*.html' ]
  111. })
  112. const server = launchServer(app, { leaveOpen: true })
  113. request('http://localhost:8100/two.php')
  114. .then(response => {
  115. t.strictEqual(response.res.statusCode, 403)
  116. request('http://localhost:8100/one.html')
  117. .then(response => {
  118. t.strictEqual(response.res.statusCode, 403)
  119. server.close()
  120. })
  121. })
  122. })
  123. test('rewrite: local', function (t) {
  124. t.plan(1)
  125. const app = localWebServer({
  126. log: { format: 'none' },
  127. static: { root: __dirname + '/fixture/rewrite' },
  128. rewrite: [ { from: '/two.html', to: '/one.html' } ]
  129. })
  130. launchServer(app, { path: '/two.html', onSuccess: response => {
  131. t.ok(/one/.test(response.data))
  132. }})
  133. })
  134. test('mock: simple response', function (t) {
  135. t.plan(2)
  136. const app = localWebServer({
  137. log: { format: 'none' },
  138. mocks: [
  139. { route: '/test', response: { body: 'test' } }
  140. ]
  141. })
  142. launchServer(app, { path: '/test', onSuccess: response => {
  143. t.strictEqual(response.res.statusCode, 200)
  144. t.ok(/test/.test(response.data))
  145. }})
  146. })
  147. test('mock: method request filter', function (t) {
  148. t.plan(3)
  149. const app = localWebServer({
  150. log: { format: 'none' },
  151. mocks: [
  152. {
  153. route: '/test',
  154. request: { method: 'POST' },
  155. response: { body: 'test' }
  156. }
  157. ]
  158. })
  159. const server = http.createServer(app.callback())
  160. server.listen(8100, () => {
  161. request('http://localhost:8100/test')
  162. .then(checkResponse(t, 404))
  163. .then(() => request('http://localhost:8100/test', { data: 'something' }))
  164. .then(checkResponse(t, 200, /test/))
  165. .then(server.close.bind(server))
  166. })
  167. })
  168. test('mock: accepts request filter', function (t) {
  169. t.plan(3)
  170. const app = localWebServer({
  171. log: { format: 'none' },
  172. mocks: [
  173. {
  174. route: '/test',
  175. request: { accepts: 'text' },
  176. response: { body: 'test' }
  177. }
  178. ]
  179. })
  180. const server = http.createServer(app.callback())
  181. server.listen(8100, () => {
  182. request('http://localhost:8100/test', { headers: { Accept: '*/json' } })
  183. .then(checkResponse(t, 404))
  184. .then(() => request('http://localhost:8100/test', { headers: { Accept: 'text/plain' } }))
  185. .then(checkResponse(t, 200, /test/))
  186. .then(server.close.bind(server))
  187. })
  188. })
  189. test('mock: responses array', function (t) {
  190. t.plan(4)
  191. const app = localWebServer({
  192. log: { format: 'none' },
  193. mocks: [
  194. {
  195. route: '/test',
  196. responses: [
  197. { request: { method: 'GET' }, response: { body: 'get' } },
  198. { request: { method: 'POST' }, response: { body: 'post' } }
  199. ]
  200. }
  201. ]
  202. })
  203. const server = http.createServer(app.callback())
  204. server.listen(8100, () => {
  205. request('http://localhost:8100/test')
  206. .then(checkResponse(t, 200, /get/))
  207. .then(() => request('http://localhost:8100/test', { method: 'POST' }))
  208. .then(checkResponse(t, 200, /post/))
  209. .then(server.close.bind(server))
  210. })
  211. })
  212. test('mock: response function', function (t) {
  213. t.plan(4)
  214. const app = localWebServer({
  215. log: { format: 'none' },
  216. mocks: [
  217. {
  218. route: '/test',
  219. responses: [
  220. { request: { method: 'GET' }, response: ctx => ctx.body = 'get' },
  221. { request: { method: 'POST' }, response: ctx => ctx.body = 'post' }
  222. ]
  223. }
  224. ]
  225. })
  226. const server = http.createServer(app.callback())
  227. server.listen(8100, () => {
  228. request('http://localhost:8100/test')
  229. .then(checkResponse(t, 200, /get/))
  230. .then(() => request('http://localhost:8100/test', { method: 'POST' }))
  231. .then(checkResponse(t, 200, /post/))
  232. .then(server.close.bind(server))
  233. })
  234. })
  235. test('mock: response function args', function (t) {
  236. t.plan(2)
  237. const app = localWebServer({
  238. log: { format: 'none' },
  239. mocks: [
  240. {
  241. route: '/test/:one',
  242. responses: [
  243. { request: { method: 'GET' }, response: (ctx, one) => ctx.body = one }
  244. ]
  245. }
  246. ]
  247. })
  248. const server = http.createServer(app.callback())
  249. server.listen(8100, () => {
  250. request('http://localhost:8100/test/yeah')
  251. .then(checkResponse(t, 200, /yeah/))
  252. .then(server.close.bind(server))
  253. })
  254. })
  255. test('mock: async response function', function (t) {
  256. t.plan(2)
  257. const app = localWebServer({
  258. log: { format: 'none' },
  259. mocks: [
  260. {
  261. route: '/test',
  262. responses: {
  263. response: function (ctx) {
  264. return new Promise((resolve, reject) => {
  265. setTimeout(() => {
  266. ctx.body = 'test'
  267. resolve()
  268. }, 10)
  269. })
  270. }
  271. }
  272. }
  273. ]
  274. })
  275. const server = http.createServer(app.callback())
  276. server.listen(8100, () => {
  277. request('http://localhost:8100/test')
  278. .then(checkResponse(t, 200, /test/))
  279. .then(server.close.bind(server))
  280. })
  281. })