123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- /* eslint-env mocha */
- var assert = require('assert')
- var https = require('https')
- var http = require('http')
- var util = require('util')
- var fixtures = require('./fixtures')
- var spdy = require('../')
- // Node.js 0.10 and 0.12 support
- Object.assign = process.versions.modules >= 46
- ? Object.assign // eslint-disable-next-line
- : util._extend
- describe('SPDY Client', function () {
- describe('regular', function () {
- fixtures.everyConfig(function (protocol, npn, version, plain) {
- var server
- var agent
- var hmodule
- beforeEach(function (done) {
- hmodule = plain ? http : https
- var options = Object.assign({
- spdy: {
- plain: plain
- }
- }, fixtures.keys)
- server = spdy.createServer(options, function (req, res) {
- var body = ''
- req.on('data', function (chunk) {
- body += chunk
- })
- req.on('end', function () {
- res.writeHead(200, req.headers)
- res.addTrailers({ trai: 'ler' })
- var push = res.push('/push', {
- request: {
- push: 'yes'
- }
- }, function (err) {
- assert(!err)
- push.end('push')
- push.on('error', function () {
- })
- res.end(body || 'okay')
- })
- })
- })
- server.listen(fixtures.port, function () {
- agent = spdy.createAgent({
- rejectUnauthorized: false,
- port: fixtures.port,
- spdy: {
- plain: plain,
- protocol: plain ? npn : null,
- protocols: [ npn ]
- }
- })
- done()
- })
- })
- afterEach(function (done) {
- var waiting = 2
- agent.close(next)
- server.close(next)
- function next () {
- if (--waiting === 0) {
- done()
- }
- }
- })
- it('should send GET request', function (done) {
- var req = hmodule.request({
- agent: agent,
- method: 'GET',
- path: '/get',
- headers: {
- a: 'b'
- }
- }, function (res) {
- assert.equal(res.statusCode, 200)
- assert.equal(res.headers.a, 'b')
- fixtures.expectData(res, 'okay', done)
- })
- req.end()
- })
- it('should send POST request', function (done) {
- var req = hmodule.request({
- agent: agent,
- method: 'POST',
- path: '/post',
- headers: {
- post: 'headers'
- }
- }, function (res) {
- assert.equal(res.statusCode, 200)
- assert.equal(res.headers.post, 'headers')
- fixtures.expectData(res, 'post body', done)
- })
- agent._spdyState.socket.once(plain ? 'connect' : 'secureConnect',
- function () {
- req.end('post body')
- })
- })
- it('should receive PUSH_PROMISE', function (done) {
- var req = hmodule.request({
- agent: agent,
- method: 'GET',
- path: '/get'
- }, function (res) {
- assert.equal(res.statusCode, 200)
- res.resume()
- })
- req.on('push', function (push) {
- assert.equal(push.path, '/push')
- assert.equal(push.headers.push, 'yes')
- push.resume()
- push.once('end', done)
- })
- req.end()
- })
- it('should receive trailing headers', function (done) {
- var req = hmodule.request({
- agent: agent,
- method: 'GET',
- path: '/get'
- }, function (res) {
- assert.equal(res.statusCode, 200)
- res.on('trailers', function (headers) {
- assert.equal(headers.trai, 'ler')
- fixtures.expectData(res, 'okay', done)
- })
- })
- req.end()
- })
- })
- })
- describe('x-forwarded-for', function () {
- fixtures.everyConfig(function (protocol, npn, version, plain) {
- var server
- var agent
- var hmodule
- beforeEach(function (done) {
- hmodule = plain ? http : https
- var options = Object.assign({
- spdy: {
- plain: plain,
- 'x-forwarded-for': true
- }
- }, fixtures.keys)
- server = spdy.createServer(options, function (req, res) {
- res.writeHead(200, req.headers)
- res.end()
- })
- server.listen(fixtures.port, function () {
- agent = spdy.createAgent({
- rejectUnauthorized: false,
- port: fixtures.port,
- spdy: {
- 'x-forwarded-for': '1.2.3.4',
- plain: plain,
- protocol: plain ? npn : null,
- protocols: [ npn ]
- }
- })
- done()
- })
- })
- afterEach(function (done) {
- var waiting = 2
- agent.close(next)
- server.close(next)
- function next () {
- if (--waiting === 0) {
- done()
- }
- }
- })
- it('should send x-forwarded-for', function (done) {
- var req = hmodule.request({
- agent: agent,
- method: 'GET',
- path: '/get'
- }, function (res) {
- assert.equal(res.statusCode, 200)
- assert.equal(res.headers['x-forwarded-for'], '1.2.3.4')
- res.resume()
- res.once('end', done)
- })
- req.end()
- })
- })
- })
- })
|