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.

242 lines
5.9 KiB

4 years ago
  1. /* eslint-env mocha */
  2. var assert = require('assert')
  3. var https = require('https')
  4. var http = require('http')
  5. var util = require('util')
  6. var fixtures = require('./fixtures')
  7. var spdy = require('../')
  8. // Node.js 0.10 and 0.12 support
  9. Object.assign = process.versions.modules >= 46
  10. ? Object.assign // eslint-disable-next-line
  11. : util._extend
  12. describe('SPDY Client', function () {
  13. describe('regular', function () {
  14. fixtures.everyConfig(function (protocol, alpn, version, plain) {
  15. var server
  16. var agent
  17. var hmodule
  18. beforeEach(function (done) {
  19. hmodule = plain ? http : https
  20. var options = Object.assign({
  21. spdy: {
  22. plain: plain
  23. }
  24. }, fixtures.keys)
  25. server = spdy.createServer(options, function (req, res) {
  26. var body = ''
  27. req.on('data', function (chunk) {
  28. body += chunk
  29. })
  30. req.on('end', function () {
  31. res.writeHead(200, req.headers)
  32. res.addTrailers({ trai: 'ler' })
  33. var push = res.push('/push', {
  34. request: {
  35. push: 'yes'
  36. }
  37. }, function (err) {
  38. assert(!err)
  39. push.end('push')
  40. push.on('error', function () {
  41. })
  42. res.end(body || 'okay')
  43. })
  44. })
  45. })
  46. server.listen(fixtures.port, function () {
  47. agent = spdy.createAgent({
  48. rejectUnauthorized: false,
  49. port: fixtures.port,
  50. spdy: {
  51. plain: plain,
  52. protocol: plain ? alpn : null,
  53. protocols: [alpn]
  54. }
  55. })
  56. done()
  57. })
  58. })
  59. afterEach(function (done) {
  60. var waiting = 2
  61. agent.close(next)
  62. server.close(next)
  63. function next () {
  64. if (--waiting === 0) {
  65. done()
  66. }
  67. }
  68. })
  69. it('should send GET request', function (done) {
  70. var req = hmodule.request({
  71. agent: agent,
  72. method: 'GET',
  73. path: '/get',
  74. headers: {
  75. a: 'b'
  76. }
  77. }, function (res) {
  78. assert.strictEqual(res.statusCode, 200)
  79. assert.strictEqual(res.headers.a, 'b')
  80. fixtures.expectData(res, 'okay', done)
  81. })
  82. req.end()
  83. })
  84. it('should send POST request', function (done) {
  85. var req = hmodule.request({
  86. agent: agent,
  87. method: 'POST',
  88. path: '/post',
  89. headers: {
  90. post: 'headers'
  91. }
  92. }, function (res) {
  93. assert.strictEqual(res.statusCode, 200)
  94. assert.strictEqual(res.headers.post, 'headers')
  95. fixtures.expectData(res, 'post body', done)
  96. })
  97. agent._spdyState.socket.once(plain ? 'connect' : 'secureConnect',
  98. function () {
  99. req.end('post body')
  100. })
  101. })
  102. it('should receive PUSH_PROMISE', function (done) {
  103. var req = hmodule.request({
  104. agent: agent,
  105. method: 'GET',
  106. path: '/get'
  107. }, function (res) {
  108. assert.strictEqual(res.statusCode, 200)
  109. res.resume()
  110. })
  111. req.on('push', function (push) {
  112. assert.strictEqual(push.path, '/push')
  113. assert.strictEqual(push.headers.push, 'yes')
  114. push.resume()
  115. push.once('end', done)
  116. })
  117. req.end()
  118. })
  119. it('should receive trailing headers', function (done) {
  120. var req = hmodule.request({
  121. agent: agent,
  122. method: 'GET',
  123. path: '/get'
  124. }, function (res) {
  125. assert.strictEqual(res.statusCode, 200)
  126. res.on('trailers', function (headers) {
  127. assert.strictEqual(headers.trai, 'ler')
  128. fixtures.expectData(res, 'okay', done)
  129. })
  130. })
  131. req.end()
  132. })
  133. })
  134. })
  135. describe('x-forwarded-for', function () {
  136. fixtures.everyConfig(function (protocol, alpn, version, plain) {
  137. var server
  138. var agent
  139. var hmodule
  140. // The underlying spdy Connection created by the agent.
  141. var connection
  142. beforeEach(function (done) {
  143. hmodule = plain ? http : https
  144. var options = Object.assign({
  145. spdy: {
  146. plain: plain,
  147. 'x-forwarded-for': true
  148. }
  149. }, fixtures.keys)
  150. server = spdy.createServer(options, function (req, res) {
  151. res.writeHead(200, req.headers)
  152. res.end()
  153. })
  154. server.listen(fixtures.port, function () {
  155. agent = spdy.createAgent({
  156. rejectUnauthorized: false,
  157. port: fixtures.port,
  158. spdy: {
  159. 'x-forwarded-for': '1.2.3.4',
  160. plain: plain,
  161. protocol: plain ? alpn : null,
  162. protocols: [alpn]
  163. }
  164. })
  165. // Once aagent has connection, keep a copy for testing.
  166. agent.once('_connect', function () {
  167. connection = agent._spdyState.connection
  168. done()
  169. })
  170. })
  171. })
  172. afterEach(function (done) {
  173. var waiting = 2
  174. agent.close(next)
  175. server.close(next)
  176. function next () {
  177. if (--waiting === 0) {
  178. done()
  179. }
  180. }
  181. })
  182. it('should send x-forwarded-for', function (done) {
  183. var req = hmodule.request({
  184. agent: agent,
  185. method: 'GET',
  186. path: '/get'
  187. }, function (res) {
  188. assert.strictEqual(res.statusCode, 200)
  189. assert.strictEqual(res.headers['x-forwarded-for'], '1.2.3.4')
  190. res.resume()
  191. res.once('end', done)
  192. })
  193. req.end()
  194. })
  195. it('agent should emit connection level errors', function (done) {
  196. agent.once('error', function (err) {
  197. assert.strictEqual(err.message, 'mock error')
  198. done()
  199. })
  200. connection.emit('error', new Error('mock error'))
  201. })
  202. })
  203. })
  204. })