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.

150 lines
4.0 KiB

9 years ago
  1. 'use strict'
  2. const test = require('tape')
  3. const request = require('req-then')
  4. const LocalWebServer = require('../../')
  5. const c = require('../common')
  6. test('mock: simple response', function (t) {
  7. t.plan(2)
  8. const ws = new LocalWebServer()
  9. ws.addMockResponses([
  10. { route: '/test', response: { body: 'test' } }
  11. ])
  12. const server = ws.getServer()
  13. server.listen(8100, () => {
  14. request('http://localhost:8100/test')
  15. .then(c.checkResponse(t, 200, /test/))
  16. .then(server.close.bind(server))
  17. .catch(c.fail(t))
  18. })
  19. })
  20. test('mock: method request filter', function (t) {
  21. t.plan(3)
  22. const ws = new LocalWebServer()
  23. ws.addMockResponses([
  24. {
  25. route: '/test',
  26. request: { method: 'POST' },
  27. response: { body: 'test' }
  28. }
  29. ])
  30. const server = ws.getServer()
  31. server.listen(8100, () => {
  32. request('http://localhost:8100/test')
  33. .then(c.checkResponse(t, 404))
  34. .then(() => request('http://localhost:8100/test', { data: 'something' }))
  35. .then(c.checkResponse(t, 200, /test/))
  36. .then(server.close.bind(server))
  37. .catch(c.fail(t))
  38. })
  39. })
  40. test('mock: accepts request filter', function (t) {
  41. t.plan(3)
  42. const ws = new LocalWebServer()
  43. ws.addMockResponses([
  44. {
  45. route: '/test',
  46. request: { accepts: 'text' },
  47. response: { body: 'test' }
  48. }
  49. ])
  50. const server = ws.getServer()
  51. server.listen(8100, () => {
  52. request('http://localhost:8100/test', { headers: { Accept: '*/json' } })
  53. .then(c.checkResponse(t, 404))
  54. .then(() => request('http://localhost:8100/test', { headers: { Accept: 'text/plain' } }))
  55. .then(c.checkResponse(t, 200, /test/))
  56. .then(server.close.bind(server))
  57. })
  58. })
  59. test('mock: responses array', function (t) {
  60. t.plan(4)
  61. const ws = new LocalWebServer()
  62. ws.addMockResponses([
  63. {
  64. route: '/test',
  65. responses: [
  66. { request: { method: 'GET' }, response: { body: 'get' } },
  67. { request: { method: 'POST' }, response: { body: 'post' } }
  68. ]
  69. }
  70. ])
  71. const server = ws.getServer()
  72. server.listen(8100, () => {
  73. request('http://localhost:8100/test')
  74. .then(c.checkResponse(t, 200, /get/))
  75. .then(() => request('http://localhost:8100/test', { method: 'POST' }))
  76. .then(c.checkResponse(t, 200, /post/))
  77. .then(server.close.bind(server))
  78. })
  79. })
  80. test('mock: response function', function (t) {
  81. t.plan(4)
  82. const ws = new LocalWebServer()
  83. ws.addMockResponses([
  84. {
  85. route: '/test',
  86. responses: [
  87. { request: { method: 'GET' }, response: ctx => ctx.body = 'get' },
  88. { request: { method: 'POST' }, response: ctx => ctx.body = 'post' }
  89. ]
  90. }
  91. ])
  92. const server = ws.getServer()
  93. server.listen(8100, () => {
  94. request('http://localhost:8100/test')
  95. .then(c.checkResponse(t, 200, /get/))
  96. .then(() => request('http://localhost:8100/test', { method: 'POST' }))
  97. .then(c.checkResponse(t, 200, /post/))
  98. .then(server.close.bind(server))
  99. })
  100. })
  101. test('mock: response function args', function (t) {
  102. t.plan(2)
  103. const ws = new LocalWebServer()
  104. ws.addMockResponses([
  105. {
  106. route: '/test/:one',
  107. responses: [
  108. { request: { method: 'GET' }, response: (ctx, one) => ctx.body = one }
  109. ]
  110. }
  111. ])
  112. const server = ws.getServer()
  113. server.listen(8100, () => {
  114. request('http://localhost:8100/test/yeah')
  115. .then(c.checkResponse(t, 200, /yeah/))
  116. .then(server.close.bind(server))
  117. })
  118. })
  119. test('mock: async response function', function (t) {
  120. t.plan(2)
  121. const ws = new LocalWebServer()
  122. ws.addMockResponses([
  123. {
  124. route: '/test',
  125. responses: {
  126. response: function (ctx) {
  127. return new Promise((resolve, reject) => {
  128. setTimeout(() => {
  129. ctx.body = 'test'
  130. resolve()
  131. }, 10)
  132. })
  133. }
  134. }
  135. }
  136. ])
  137. const server = ws.getServer()
  138. server.listen(8100, () => {
  139. request('http://localhost:8100/test')
  140. .then(c.checkResponse(t, 200, /test/))
  141. .then(server.close.bind(server))
  142. })
  143. })