michael@0: var expect = require('chai').expect; michael@0: var util = require('./util'); michael@0: var fs = require('fs'); michael@0: var path = require('path'); michael@0: michael@0: var http2 = require('../lib/http'); michael@0: var https = require('https'); michael@0: michael@0: process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; michael@0: michael@0: var options = { michael@0: key: fs.readFileSync(path.join(__dirname, '../example/localhost.key')), michael@0: cert: fs.readFileSync(path.join(__dirname, '../example/localhost.crt')), michael@0: log: util.log michael@0: }; michael@0: michael@0: http2.globalAgent = new http2.Agent({ log: util.log }); michael@0: michael@0: describe('http.js', function() { michael@0: describe('Server', function() { michael@0: describe('new Server(options)', function() { michael@0: it('should throw if called without \'plain\' or TLS options', function() { michael@0: expect(function() { michael@0: new http2.Server(); michael@0: }).to.throw(Error); michael@0: expect(function() { michael@0: http2.createServer(util.noop); michael@0: }).to.throw(Error); michael@0: }); michael@0: }); michael@0: describe('property `timeout`', function() { michael@0: it('should be a proxy for the backing HTTPS server\'s `timeout` property', function() { michael@0: var server = new http2.Server(options); michael@0: var backingServer = server._server; michael@0: var newTimeout = 10; michael@0: server.timeout = newTimeout; michael@0: expect(server.timeout).to.be.equal(newTimeout); michael@0: expect(backingServer.timeout).to.be.equal(newTimeout); michael@0: }); michael@0: }); michael@0: describe('method `setTimeout(timeout, [callback])`', function() { michael@0: it('should be a proxy for the backing HTTPS server\'s `setTimeout` method', function() { michael@0: var server = new http2.Server(options); michael@0: var backingServer = server._server; michael@0: var newTimeout = 10; michael@0: var newCallback = util.noop; michael@0: backingServer.setTimeout = function(timeout, callback) { michael@0: expect(timeout).to.be.equal(newTimeout); michael@0: expect(callback).to.be.equal(newCallback); michael@0: }; michael@0: server.setTimeout(newTimeout, newCallback); michael@0: }); michael@0: }); michael@0: }); michael@0: describe('Agent', function() { michael@0: describe('property `maxSockets`', function() { michael@0: it('should be a proxy for the backing HTTPS agent\'s `maxSockets` property', function() { michael@0: var agent = new http2.Agent({ log: util.log }); michael@0: var backingAgent = agent._httpsAgent; michael@0: var newMaxSockets = backingAgent.maxSockets + 1; michael@0: agent.maxSockets = newMaxSockets; michael@0: expect(agent.maxSockets).to.be.equal(newMaxSockets); michael@0: expect(backingAgent.maxSockets).to.be.equal(newMaxSockets); michael@0: }); michael@0: }); michael@0: describe('method `request(options, [callback])`', function() { michael@0: it('should throw when trying to use with \'http\' scheme', function() { michael@0: expect(function() { michael@0: var agent = new http2.Agent({ log: util.log }); michael@0: agent.request({ protocol: 'http:' }); michael@0: }).to.throw(Error); michael@0: }); michael@0: }); michael@0: }); michael@0: describe('OutgoingRequest', function() { michael@0: function testFallbackProxyMethod(name, originalArguments, done) { michael@0: var request = new http2.OutgoingRequest(); michael@0: michael@0: // When in HTTP/2 mode, this call should be ignored michael@0: request.stream = { reset: util.noop }; michael@0: request[name].apply(request, originalArguments); michael@0: delete request.stream; michael@0: michael@0: // When in fallback mode, this call should be forwarded michael@0: request[name].apply(request, originalArguments); michael@0: var mockFallbackRequest = { on: util.noop }; michael@0: mockFallbackRequest[name] = function() { michael@0: expect(arguments).to.deep.equal(originalArguments); michael@0: done(); michael@0: }; michael@0: request._fallback(mockFallbackRequest); michael@0: } michael@0: describe('method `setNoDelay(noDelay)`', function() { michael@0: it('should act as a proxy for the backing HTTPS agent\'s `setNoDelay` method', function(done) { michael@0: testFallbackProxyMethod('setNoDelay', [true], done); michael@0: }); michael@0: }); michael@0: describe('method `setSocketKeepAlive(enable, initialDelay)`', function() { michael@0: it('should act as a proxy for the backing HTTPS agent\'s `setSocketKeepAlive` method', function(done) { michael@0: testFallbackProxyMethod('setSocketKeepAlive', [true, util.random(10, 100)], done); michael@0: }); michael@0: }); michael@0: describe('method `setTimeout(timeout, [callback])`', function() { michael@0: it('should act as a proxy for the backing HTTPS agent\'s `setTimeout` method', function(done) { michael@0: testFallbackProxyMethod('setTimeout', [util.random(10, 100), util.noop], done); michael@0: }); michael@0: }); michael@0: describe('method `abort()`', function() { michael@0: it('should act as a proxy for the backing HTTPS agent\'s `abort` method', function(done) { michael@0: testFallbackProxyMethod('abort', [], done); michael@0: }); michael@0: }); michael@0: }); michael@0: describe('test scenario', function() { michael@0: describe('simple request', function() { michael@0: it('should work as expected', function(done) { michael@0: var path = '/x'; michael@0: var message = 'Hello world'; michael@0: michael@0: var server = http2.createServer(options, function(request, response) { michael@0: expect(request.url).to.equal(path); michael@0: response.end(message); michael@0: }); michael@0: michael@0: server.listen(1234, function() { michael@0: http2.get('https://localhost:1234' + path, function(response) { michael@0: response.on('readable', function() { michael@0: expect(response.read().toString()).to.equal(message); michael@0: server.close(); michael@0: done(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: describe('request with payload', function() { michael@0: it('should work as expected', function(done) { michael@0: var path = '/x'; michael@0: var message = 'Hello world'; michael@0: michael@0: var server = http2.createServer(options, function(request, response) { michael@0: expect(request.url).to.equal(path); michael@0: request.once('readable', function() { michael@0: expect(request.read().toString()).to.equal(message); michael@0: response.end(); michael@0: }); michael@0: }); michael@0: michael@0: server.listen(1240, function() { michael@0: var request = http2.request({ michael@0: host: 'localhost', michael@0: port: 1240, michael@0: path: path michael@0: }); michael@0: request.write(message); michael@0: request.end(); michael@0: request.on('response', function() { michael@0: server.close(); michael@0: done(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: describe('request with custom status code and headers', function() { michael@0: it('should work as expected', function(done) { michael@0: var path = '/x'; michael@0: var message = 'Hello world'; michael@0: var headerName = 'name'; michael@0: var headerValue = 'value'; michael@0: michael@0: var server = http2.createServer(options, function(request, response) { michael@0: // Request URL and headers michael@0: expect(request.url).to.equal(path); michael@0: expect(request.headers[headerName]).to.equal(headerValue); michael@0: michael@0: // A header to be overwritten later michael@0: response.setHeader(headerName, 'to be overwritten'); michael@0: expect(response.getHeader(headerName)).to.equal('to be overwritten'); michael@0: michael@0: // A header to be deleted michael@0: response.setHeader('nonexistent', 'x'); michael@0: response.removeHeader('nonexistent'); michael@0: expect(response.getHeader('nonexistent')).to.equal(undefined); michael@0: michael@0: // Don't send date michael@0: response.sendDate = false; michael@0: michael@0: // Specifying more headers, the status code and a reason phrase with `writeHead` michael@0: var moreHeaders = {}; michael@0: moreHeaders[headerName] = headerValue; michael@0: response.writeHead(600, 'to be discarded', moreHeaders); michael@0: expect(response.getHeader(headerName)).to.equal(headerValue); michael@0: michael@0: // Empty response body michael@0: response.end(message); michael@0: }); michael@0: michael@0: server.listen(1239, function() { michael@0: var headers = {}; michael@0: headers[headerName] = headerValue; michael@0: var request = http2.request({ michael@0: host: 'localhost', michael@0: port: 1239, michael@0: path: path, michael@0: headers: headers michael@0: }); michael@0: request.end(); michael@0: request.on('response', function(response) { michael@0: expect(response.headers[headerName]).to.equal(headerValue); michael@0: expect(response.headers['nonexistent']).to.equal(undefined); michael@0: expect(response.headers['date']).to.equal(undefined); michael@0: response.on('readable', function() { michael@0: expect(response.read().toString()).to.equal(message); michael@0: server.close(); michael@0: done(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: describe('request over plain TCP', function() { michael@0: it('should work as expected', function(done) { michael@0: var path = '/x'; michael@0: var message = 'Hello world'; michael@0: michael@0: var server = http2.createServer({ michael@0: plain: true, michael@0: log: util.log michael@0: }, function(request, response) { michael@0: expect(request.url).to.equal(path); michael@0: response.end(message); michael@0: }); michael@0: michael@0: server.listen(1237, function() { michael@0: var request = http2.request({ michael@0: plain: true, michael@0: host: 'localhost', michael@0: port: 1237, michael@0: path: path michael@0: }, function(response) { michael@0: response.on('readable', function() { michael@0: expect(response.read().toString()).to.equal(message); michael@0: server.close(); michael@0: done(); michael@0: }); michael@0: }); michael@0: request.end(); michael@0: }); michael@0: }); michael@0: }); michael@0: describe('request to an HTTPS/1 server', function() { michael@0: it('should fall back to HTTPS/1 successfully', function(done) { michael@0: var path = '/x'; michael@0: var message = 'Hello world'; michael@0: michael@0: var server = https.createServer(options, function(request, response) { michael@0: expect(request.url).to.equal(path); michael@0: response.end(message); michael@0: }); michael@0: michael@0: server.listen(5678, function() { michael@0: http2.get('https://localhost:5678' + path, function(response) { michael@0: response.on('readable', function() { michael@0: expect(response.read().toString()).to.equal(message); michael@0: done(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: describe('HTTPS/1 request to a HTTP/2 server', function() { michael@0: it('should fall back to HTTPS/1 successfully', function(done) { michael@0: var path = '/x'; michael@0: var message = 'Hello world'; michael@0: michael@0: var server = http2.createServer(options, function(request, response) { michael@0: expect(request.url).to.equal(path); michael@0: response.end(message); michael@0: }); michael@0: michael@0: server.listen(1236, function() { michael@0: https.get('https://localhost:1236' + path, function(response) { michael@0: response.on('readable', function() { michael@0: expect(response.read().toString()).to.equal(message); michael@0: done(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: describe('two parallel request', function() { michael@0: it('should work as expected', function(done) { michael@0: var path = '/x'; michael@0: var message = 'Hello world'; michael@0: michael@0: var server = http2.createServer(options, function(request, response) { michael@0: expect(request.url).to.equal(path); michael@0: response.end(message); michael@0: }); michael@0: michael@0: server.listen(1237, function() { michael@0: done = util.callNTimes(2, done); michael@0: // 1. request michael@0: http2.get('https://localhost:1237' + path, function(response) { michael@0: response.on('readable', function() { michael@0: expect(response.read().toString()).to.equal(message); michael@0: done(); michael@0: }); michael@0: }); michael@0: // 2. request michael@0: http2.get('https://localhost:1237' + path, function(response) { michael@0: response.on('readable', function() { michael@0: expect(response.read().toString()).to.equal(message); michael@0: done(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: describe('two subsequent request', function() { michael@0: it('should use the same HTTP/2 connection', function(done) { michael@0: var path = '/x'; michael@0: var message = 'Hello world'; michael@0: michael@0: var server = http2.createServer(options, function(request, response) { michael@0: expect(request.url).to.equal(path); michael@0: response.end(message); michael@0: }); michael@0: michael@0: server.listen(1238, function() { michael@0: // 1. request michael@0: http2.get('https://localhost:1238' + path, function(response) { michael@0: response.on('readable', function() { michael@0: expect(response.read().toString()).to.equal(message); michael@0: michael@0: // 2. request michael@0: http2.get('https://localhost:1238' + path, function(response) { michael@0: response.on('readable', function() { michael@0: expect(response.read().toString()).to.equal(message); michael@0: done(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: describe('request and response with trailers', function() { michael@0: it('should work as expected', function(done) { michael@0: var path = '/x'; michael@0: var message = 'Hello world'; michael@0: var requestTrailers = { 'content-md5': 'x' }; michael@0: var responseTrailers = { 'content-md5': 'y' }; michael@0: michael@0: var server = http2.createServer(options, function(request, response) { michael@0: expect(request.url).to.equal(path); michael@0: request.on('data', util.noop); michael@0: request.once('end', function() { michael@0: expect(request.trailers).to.deep.equal(requestTrailers); michael@0: response.write(message); michael@0: response.addTrailers(responseTrailers); michael@0: response.end(); michael@0: }); michael@0: }); michael@0: michael@0: server.listen(1241, function() { michael@0: var request = http2.request('https://localhost:1241' + path); michael@0: request.addTrailers(requestTrailers); michael@0: request.end(); michael@0: request.on('response', function(response) { michael@0: response.on('data', util.noop); michael@0: response.once('end', function() { michael@0: expect(response.trailers).to.deep.equal(responseTrailers); michael@0: done(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: describe('server push', function() { michael@0: it('should work as expected', function(done) { michael@0: var path = '/x'; michael@0: var message = 'Hello world'; michael@0: var pushedPath = '/y'; michael@0: var pushedMessage = 'Hello world 2'; michael@0: michael@0: var server = http2.createServer(options, function(request, response) { michael@0: expect(request.url).to.equal(path); michael@0: var push1 = response.push('/y'); michael@0: push1.end(pushedMessage); michael@0: var push2 = response.push({ path: '/y', protocol: 'https:' }); michael@0: push2.end(pushedMessage); michael@0: response.end(message); michael@0: }); michael@0: michael@0: server.listen(1235, function() { michael@0: var request = http2.get('https://localhost:1235' + path); michael@0: done = util.callNTimes(5, done); michael@0: michael@0: request.on('response', function(response) { michael@0: response.on('readable', function() { michael@0: expect(response.read().toString()).to.equal(message); michael@0: done(); michael@0: }); michael@0: response.on('end', done); michael@0: }); michael@0: michael@0: request.on('push', function(promise) { michael@0: expect(promise.url).to.be.equal(pushedPath); michael@0: promise.on('response', function(pushStream) { michael@0: pushStream.on('readable', function() { michael@0: expect(pushStream.read().toString()).to.equal(pushedMessage); michael@0: done(); michael@0: }); michael@0: pushStream.on('end', done); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: });