server-test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /* eslint-env mocha */
  2. var assert = require('assert')
  3. var tls = require('tls')
  4. var net = require('net')
  5. var https = require('https')
  6. var transport = require('spdy-transport')
  7. var util = require('util')
  8. var fixtures = require('./fixtures')
  9. var spdy = require('../')
  10. // Node.js 0.10 and 0.12 support
  11. Object.assign = process.versions.modules >= 46
  12. ? Object.assign // eslint-disable-next-line
  13. : util._extend
  14. describe('SPDY Server', function () {
  15. fixtures.everyConfig(function (protocol, npn, version, plain) {
  16. var server
  17. var client
  18. beforeEach(function (done) {
  19. server = spdy.createServer(Object.assign({
  20. spdy: {
  21. 'x-forwarded-for': true,
  22. plain: plain
  23. }
  24. }, fixtures.keys))
  25. server.listen(fixtures.port, function () {
  26. var socket = (plain ? net : tls).connect({
  27. rejectUnauthorized: false,
  28. port: fixtures.port,
  29. NPNProtocols: [ npn ]
  30. }, function () {
  31. client = transport.connection.create(socket, {
  32. protocol: protocol,
  33. isServer: false
  34. })
  35. client.start(version)
  36. done()
  37. })
  38. })
  39. })
  40. afterEach(function (done) {
  41. client.socket.destroy()
  42. server.close(done)
  43. })
  44. it('should process GET request', function (done) {
  45. var stream = client.request({
  46. method: 'GET',
  47. path: '/get',
  48. headers: {
  49. a: 'b'
  50. }
  51. }, function (err) {
  52. assert(!err)
  53. stream.on('response', function (status, headers) {
  54. assert.equal(status, 200)
  55. assert.equal(headers.ok, 'yes')
  56. fixtures.expectData(stream, 'response', done)
  57. })
  58. stream.end()
  59. })
  60. server.on('request', function (req, res) {
  61. assert.equal(req.isSpdy, res.isSpdy)
  62. assert.equal(req.spdyVersion, res.spdyVersion)
  63. assert(req.isSpdy)
  64. if (!plain) {
  65. assert(req.socket.encrypted)
  66. assert(req.socket.getPeerCertificate())
  67. }
  68. // Auto-detection
  69. if (version === 3.1) {
  70. assert(req.spdyVersion >= 3 && req.spdyVersion <= 3.1)
  71. } else {
  72. assert.equal(req.spdyVersion, version)
  73. }
  74. assert(req.spdyStream)
  75. assert(res.spdyStream)
  76. assert.equal(req.method, 'GET')
  77. assert.equal(req.url, '/get')
  78. assert.deepEqual(req.headers, { a: 'b', host: 'localhost' })
  79. req.on('end', function () {
  80. res.writeHead(200, {
  81. ok: 'yes'
  82. })
  83. res.end('response')
  84. assert(res.finished, 'res.finished should be set')
  85. })
  86. req.resume()
  87. })
  88. })
  89. it('should process POST request', function (done) {
  90. var stream = client.request({
  91. method: 'POST',
  92. path: '/post'
  93. }, function (err) {
  94. assert(!err)
  95. stream.on('response', function (status, headers) {
  96. assert.equal(status, 200)
  97. assert.equal(headers.ok, 'yes')
  98. fixtures.expectData(stream, 'response', next)
  99. })
  100. stream.end('request')
  101. })
  102. server.on('request', function (req, res) {
  103. assert.equal(req.method, 'POST')
  104. assert.equal(req.url, '/post')
  105. res.writeHead(200, {
  106. ok: 'yes'
  107. })
  108. res.end('response')
  109. fixtures.expectData(req, 'request', next)
  110. })
  111. var waiting = 2
  112. function next () {
  113. if (--waiting === 0) {
  114. return done()
  115. }
  116. }
  117. })
  118. it('should process expect-continue request', function (done) {
  119. var stream = client.request({
  120. method: 'GET',
  121. path: '/get',
  122. headers: {
  123. Expect: '100-continue'
  124. }
  125. }, function (err) {
  126. assert(!err)
  127. stream.on('response', function (status, headers) {
  128. assert.equal(status, 100)
  129. fixtures.expectData(stream, 'response', done)
  130. })
  131. stream.end()
  132. })
  133. server.on('request', function (req, res) {
  134. req.on('end', function () {
  135. res.end('response')
  136. })
  137. req.resume()
  138. })
  139. })
  140. it('should emit `checkContinue` request', function (done) {
  141. var stream = client.request({
  142. method: 'GET',
  143. path: '/get',
  144. headers: {
  145. Expect: '100-continue'
  146. }
  147. }, function (err) {
  148. assert(!err)
  149. stream.on('response', function (status, headers) {
  150. assert.equal(status, 100)
  151. fixtures.expectData(stream, 'response', done)
  152. })
  153. stream.end()
  154. })
  155. server.on('checkContinue', function (req, res) {
  156. req.on('end', function () {
  157. res.writeContinue()
  158. res.end('response')
  159. })
  160. req.resume()
  161. })
  162. })
  163. it('should send PUSH_PROMISE', function (done) {
  164. var stream = client.request({
  165. method: 'POST',
  166. path: '/page'
  167. }, function (err) {
  168. assert(!err)
  169. stream.on('pushPromise', function (push) {
  170. assert.equal(push.path, '/push')
  171. assert.equal(push.headers.yes, 'push')
  172. fixtures.expectData(push, 'push', next)
  173. fixtures.expectData(stream, 'response', next)
  174. })
  175. stream.end('request')
  176. })
  177. server.on('request', function (req, res) {
  178. assert.equal(req.method, 'POST')
  179. assert.equal(req.url, '/page')
  180. res.writeHead(200, {
  181. ok: 'yes'
  182. })
  183. var push = res.push('/push', {
  184. request: {
  185. yes: 'push'
  186. }
  187. })
  188. push.end('push')
  189. res.end('response')
  190. fixtures.expectData(req, 'request', next)
  191. })
  192. var waiting = 3
  193. function next () {
  194. if (--waiting === 0) {
  195. return done()
  196. }
  197. }
  198. })
  199. it('should receive trailing headers', function (done) {
  200. var stream = client.request({
  201. method: 'POST',
  202. path: '/post'
  203. }, function (err) {
  204. assert(!err)
  205. stream.sendHeaders({ trai: 'ler' })
  206. stream.end()
  207. stream.on('response', function (status, headers) {
  208. assert.equal(status, 200)
  209. assert.equal(headers.ok, 'yes')
  210. fixtures.expectData(stream, 'response', done)
  211. })
  212. })
  213. server.on('request', function (req, res) {
  214. var gotHeaders = false
  215. req.on('trailers', function (headers) {
  216. gotHeaders = true
  217. assert.equal(headers.trai, 'ler')
  218. })
  219. req.on('end', function () {
  220. assert(gotHeaders)
  221. res.writeHead(200, {
  222. ok: 'yes'
  223. })
  224. res.end('response')
  225. })
  226. req.resume()
  227. })
  228. })
  229. it('should call .writeHead() automatically', function (done) {
  230. var stream = client.request({
  231. method: 'POST',
  232. path: '/post'
  233. }, function (err) {
  234. assert(!err)
  235. stream.on('response', function (status, headers) {
  236. assert.equal(status, 300)
  237. fixtures.expectData(stream, 'response', done)
  238. })
  239. stream.end()
  240. })
  241. server.on('request', function (req, res) {
  242. req.on('end', function () {
  243. res.statusCode = 300
  244. res.end('response')
  245. })
  246. req.resume()
  247. })
  248. })
  249. it('should not crash on .writeHead() after socket close', function (done) {
  250. var stream = client.request({
  251. method: 'POST',
  252. path: '/post'
  253. }, function (err) {
  254. assert(!err)
  255. setTimeout(function () {
  256. client.socket.destroy()
  257. }, 50)
  258. stream.on('error', function () {})
  259. stream.end()
  260. })
  261. server.on('request', function (req, res) {
  262. req.connection.on('close', function () {
  263. assert.doesNotThrow(function () {
  264. res.writeHead(200)
  265. res.end('response')
  266. })
  267. done()
  268. })
  269. })
  270. })
  271. it('should not crash on .push() after socket close', function (done) {
  272. var stream = client.request({
  273. method: 'POST',
  274. path: '/post'
  275. }, function (err) {
  276. assert(!err)
  277. setTimeout(function () {
  278. client.socket.destroy()
  279. }, 50)
  280. stream.on('error', function () {})
  281. stream.end()
  282. })
  283. server.on('request', function (req, res) {
  284. req.connection.on('close', function () {
  285. assert.doesNotThrow(function () {
  286. assert.equal(res.push('/push', {}), undefined)
  287. res.end('response')
  288. })
  289. done()
  290. })
  291. })
  292. })
  293. it('should end response after writing everything down', function (done) {
  294. var stream = client.request({
  295. method: 'GET',
  296. path: '/post'
  297. }, function (err) {
  298. assert(!err)
  299. stream.on('response', function (status, headers) {
  300. assert.equal(status, 200)
  301. fixtures.expectData(stream, 'hello world, what\'s up?', done)
  302. })
  303. stream.end()
  304. })
  305. server.on('request', function (req, res) {
  306. req.resume()
  307. res.writeHead(200)
  308. res.write('hello ')
  309. res.write('world')
  310. res.write(', what\'s')
  311. res.write(' up?')
  312. res.end()
  313. })
  314. })
  315. it('should handle x-forwarded-for', function (done) {
  316. client.sendXForwardedFor('1.2.3.4')
  317. var stream = client.request({
  318. method: 'GET',
  319. path: '/post'
  320. }, function (err) {
  321. assert(!err)
  322. stream.resume()
  323. stream.on('end', done)
  324. stream.end()
  325. })
  326. server.on('request', function (req, res) {
  327. assert.equal(req.headers['x-forwarded-for'], '1.2.3.4')
  328. req.resume()
  329. res.end()
  330. })
  331. })
  332. it('should destroy request after end', function (done) {
  333. var stream = client.request({
  334. method: 'POST',
  335. path: '/post'
  336. }, function (err) {
  337. assert(!err)
  338. })
  339. stream.end()
  340. stream.on('error', function () {})
  341. server.on('request', function (req, res) {
  342. res.end()
  343. res.destroy()
  344. res.socket.on('close', function () {
  345. done()
  346. })
  347. })
  348. })
  349. })
  350. it('should respond to http/1.1', function (done) {
  351. var server = spdy.createServer(fixtures.keys, function (req, res) {
  352. assert.equal(req.isSpdy, res.isSpdy)
  353. assert.equal(req.spdyVersion, res.spdyVersion)
  354. assert(!req.isSpdy)
  355. assert.equal(req.spdyVersion, 1)
  356. res.writeHead(200)
  357. res.end()
  358. })
  359. server.listen(fixtures.port, function () {
  360. var req = https.request({
  361. agent: false,
  362. rejectUnauthorized: false,
  363. NPNProtocols: [ 'http/1.1' ],
  364. port: fixtures.port,
  365. method: 'GET',
  366. path: '/'
  367. }, function (res) {
  368. assert.equal(res.statusCode, 200)
  369. res.resume()
  370. res.on('end', function () {
  371. server.close(done)
  372. })
  373. })
  374. req.end()
  375. })
  376. })
  377. it('should support custom base', function (done) {
  378. function Pseuver (options, listener) {
  379. https.Server.call(this, options, listener)
  380. }
  381. util.inherits(Pseuver, https.Server)
  382. var server = spdy.createServer(Pseuver, fixtures.keys, function (req, res) {
  383. assert.equal(req.isSpdy, res.isSpdy)
  384. assert.equal(req.spdyVersion, res.spdyVersion)
  385. assert(!req.isSpdy)
  386. assert.equal(req.spdyVersion, 1)
  387. res.writeHead(200)
  388. res.end()
  389. })
  390. server.listen(fixtures.port, function () {
  391. var req = https.request({
  392. agent: false,
  393. rejectUnauthorized: false,
  394. NPNProtocols: [ 'http/1.1' ],
  395. port: fixtures.port,
  396. method: 'GET',
  397. path: '/'
  398. }, function (res) {
  399. assert.equal(res.statusCode, 200)
  400. res.resume()
  401. res.on('end', function () {
  402. server.close(done)
  403. })
  404. })
  405. req.end()
  406. })
  407. })
  408. })