client-test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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, npn, 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 ? npn : null,
  53. protocols: [ npn ]
  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.equal(res.statusCode, 200)
  79. assert.equal(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.equal(res.statusCode, 200)
  94. assert.equal(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.equal(res.statusCode, 200)
  109. res.resume()
  110. })
  111. req.on('push', function (push) {
  112. assert.equal(push.path, '/push')
  113. assert.equal(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.equal(res.statusCode, 200)
  126. res.on('trailers', function (headers) {
  127. assert.equal(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, npn, version, plain) {
  137. var server
  138. var agent
  139. var hmodule
  140. beforeEach(function (done) {
  141. hmodule = plain ? http : https
  142. var options = Object.assign({
  143. spdy: {
  144. plain: plain,
  145. 'x-forwarded-for': true
  146. }
  147. }, fixtures.keys)
  148. server = spdy.createServer(options, function (req, res) {
  149. res.writeHead(200, req.headers)
  150. res.end()
  151. })
  152. server.listen(fixtures.port, function () {
  153. agent = spdy.createAgent({
  154. rejectUnauthorized: false,
  155. port: fixtures.port,
  156. spdy: {
  157. 'x-forwarded-for': '1.2.3.4',
  158. plain: plain,
  159. protocol: plain ? npn : null,
  160. protocols: [ npn ]
  161. }
  162. })
  163. done()
  164. })
  165. })
  166. afterEach(function (done) {
  167. var waiting = 2
  168. agent.close(next)
  169. server.close(next)
  170. function next () {
  171. if (--waiting === 0) {
  172. done()
  173. }
  174. }
  175. })
  176. it('should send x-forwarded-for', function (done) {
  177. var req = hmodule.request({
  178. agent: agent,
  179. method: 'GET',
  180. path: '/get'
  181. }, function (res) {
  182. assert.equal(res.statusCode, 200)
  183. assert.equal(res.headers['x-forwarded-for'], '1.2.3.4')
  184. res.resume()
  185. res.once('end', done)
  186. })
  187. req.end()
  188. })
  189. })
  190. })
  191. })