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