var expect = require('chai').expect; var util = require('./util'); var fs = require('fs'); var path = require('path'); var url = require('url'); var net = require('net');
var http2 = require('../lib/http'); var https = require('https');
describe('http.js', function() {
beforeEach(function() {
http2.globalAgent = globalAgent;
});
describe('Server', function() {
describe('new Server(options)', function() {
it('should throw if called without \'plain\' or TLS options', function() {
expect(function() { new http2.Server();
}).to.throw(Error);
expect(function() {
http2.createServer(util.noop);
}).to.throw(Error);
});
});
describe('method `listen()`', function () {
it('should emit `listening` event', function (done) { var server = http2.createServer(serverOptions);
server.on('listening', function () {
server.close();
done();
})
server.listen(0);
});
it('should emit `error` on failure', function (done) { var server = http2.createServer(serverOptions);
// This TCP server is used to explicitly take a port to make // server.listen() fails. var net = require('net').createServer();
server.on('error', function () {
net.close()
done();
});
net.listen(0, function () {
server.listen(this.address().port);
});
});
});
describe('property `timeout`', function() {
it('should be a proxy for the backing HTTPS server\'s `timeout` property', function() { var server = new http2.Server(serverOptions); var backingServer = server._server; var newTimeout = 10;
server.timeout = newTimeout;
expect(server.timeout).to.be.equal(newTimeout);
expect(backingServer.timeout).to.be.equal(newTimeout);
});
});
describe('method `setTimeout(timeout, [callback])`', function() {
it('should be a proxy for the backing HTTPS server\'s `setTimeout` method', function() { var server = new http2.Server(serverOptions); var backingServer = server._server; var newTimeout = 10; var newCallback = util.noop;
backingServer.setTimeout = function(timeout, callback) {
expect(timeout).to.be.equal(newTimeout);
expect(callback).to.be.equal(newCallback);
};
server.setTimeout(newTimeout, newCallback);
});
});
});
describe('Agent', function() {
describe('property `maxSockets`', function() {
it('should be a proxy for the backing HTTPS agent\'s `maxSockets` property', function() { var agent = new http2.Agent({ log: util.clientLog }); var backingAgent = agent._httpsAgent; var newMaxSockets = backingAgent.maxSockets + 1;
agent.maxSockets = newMaxSockets;
expect(agent.maxSockets).to.be.equal(newMaxSockets);
expect(backingAgent.maxSockets).to.be.equal(newMaxSockets);
});
});
describe('method `request(options, [callback])`', function() {
it('should use a new agent for request-specific TLS settings', function(done) { var path = '/x'; var message = 'Hello world';
var server = http2.createServer(serverOptions, function(request, response) {
expect(request.url).to.equal(path);
response.end(message);
});
http2.globalAgent = new http2.Agent({ log: util.clientLog });
http2.get(options, function(response) {
response.on('data', function(data) {
expect(data.toString()).to.equal(message);
server.close();
done();
});
});
});
});
it('should throw when trying to use with \'http\' scheme', function() {
expect(function() { var agent = new http2.Agent({ log: util.clientLog });
agent.request({ protocol: 'http:' });
}).to.throw(Error);
});
});
});
describe('OutgoingRequest', function() { function testFallbackProxyMethod(name, originalArguments, done) { var request = new http2.OutgoingRequest();
// When in HTTP/2 mode, this call should be ignored
request.stream = { reset: util.noop };
request[name].apply(request, originalArguments); delete request.stream;
// When in fallback mode, this call should be forwarded
request[name].apply(request, originalArguments); var mockFallbackRequest = { on: util.noop };
mockFallbackRequest[name] = function() {
expect(Array.prototype.slice.call(arguments)).to.deep.equal(originalArguments);
done();
};
request._fallback(mockFallbackRequest);
}
describe('method `setNoDelay(noDelay)`', function() {
it('should act as a proxy for the backing HTTPS agent\'s `setNoDelay` method', function(done) {
testFallbackProxyMethod('setNoDelay', [true], done);
});
});
describe('method `setSocketKeepAlive(enable, initialDelay)`', function() {
it('should act as a proxy for the backing HTTPS agent\'s `setSocketKeepAlive` method', function(done) {
testFallbackProxyMethod('setSocketKeepAlive', [true, util.random(10, 100)], done);
});
});
describe('method `setTimeout(timeout, [callback])`', function() {
it('should act as a proxy for the backing HTTPS agent\'s `setTimeout` method', function(done) {
testFallbackProxyMethod('setTimeout', [util.random(10, 100), util.noop], done);
});
});
describe('method `abort()`', function() {
it('should act as a proxy for the backing HTTPS agent\'s `abort` method', function(done) {
testFallbackProxyMethod('abort', [], done);
});
});
});
describe('OutgoingResponse', function() {
it('should throw error when writeHead is called multiple times on it', function() { var called = false; var stream = { _log: util.log, headers: function () { if (called) { thrownew Error('Should not send headers twice');
} else {
called = true;
}
}, once: util.noop }; var response = new http2.OutgoingResponse(stream);
response.writeHead(200);
response.writeHead(404);
});
it('field finished should be Boolean', function(){ var stream = { _log: util.log, headers: function () {}, once: util.noop }; var response = new http2.OutgoingResponse(stream);
expect(response.finished).to.be.a('Boolean');
});
it('field finished should initially be false and then go to true when response completes',function(done){ var res; var server = http2.createServer(serverOptions, function(request, response) {
res = response;
expect(res.finished).to.be.false;
response.end('HiThere');
});
server.listen(1236, function() {
http2.get('https://localhost:1236/finished-test', function(response) {
response.on('data', function(data){ var sink = data; //
});
response.on('end',function(){
expect(res.finished).to.be.true;
server.close();
done();
});
});
});
});
});
describe('test scenario', function() {
describe('simple request', function() {
it('should work as expected', function(done) { var path = '/x'; var message = 'Hello world';
var server = http2.createServer(serverOptions, function(request, response) {
expect(request.url).to.equal(path);
response.end(message);
});
server.listen(1234, function() {
http2.get('https://localhost:1234' + path, function(response) {
response.on('data', function(data) {
expect(data.toString()).to.equal(message);
server.close();
done();
});
});
});
});
});
describe('2 simple request in parallel', function() {
it('should work as expected', function(originalDone) { var path = '/x'; var message = 'Hello world'; var done = util.callNTimes(2, function() {
server.close();
originalDone();
});
var server = http2.createServer(serverOptions, function(request, response) {
expect(request.url).to.equal(path);
response.end(message);
});
server.listen(1234, function() {
http2.get('https://localhost:1234' + path, function(response) {
response.on('data', function(data) {
expect(data.toString()).to.equal(message);
done();
});
});
http2.get('https://localhost:1234' + path, function(response) {
response.on('data', function(data) {
expect(data.toString()).to.equal(message);
done();
});
});
});
});
});
describe('100 simple request in a series', function() {
it('should work as expected', function(done) { var path = '/x'; var message = 'Hello world';
var server = http2.createServer(serverOptions, function(request, response) {
expect(request.url).to.equal(path);
response.end(message);
});
var n = 100;
server.listen(1242, function() {
doRequest(); function doRequest() {
http2.get('https://localhost:1242' + path, function(response) {
response.on('data', function(data) {
expect(data.toString()).to.equal(message); if (n) {
n -= 1;
doRequest();
} else {
server.close();
done();
}
});
});
}
});
});
});
describe('request with payload', function() {
it('should work as expected', function(done) { var path = '/x'; var message = 'Hello world';
var server = http2.createServer(serverOptions, function(request, response) {
expect(request.url).to.equal(path);
request.once('data', function(data) {
expect(data.toString()).to.equal(message);
response.end();
});
});
server.listen(1240, function() { var request = http2.request({
host: 'localhost',
port: 1240,
path: path
});
request.write(message);
request.end();
request.on('response', function() {
server.close();
done();
});
});
});
});
describe('request with custom status code and headers', function() {
it('should work as expected', function(done) { var path = '/x'; var message = 'Hello world'; var headerName = 'name'; var headerValue = 'value';
var server = http2.createServer(serverOptions, function(request, response) { // Request URL and headers
expect(request.url).to.equal(path);
expect(request.headers[headerName]).to.equal(headerValue);
// A header to be overwritten later
response.setHeader(headerName, 'to be overwritten');
expect(response.getHeader(headerName)).to.equal('to be overwritten');
// A header to be deleted
response.setHeader('nonexistent', 'x');
response.removeHeader('nonexistent');
expect(response.getHeader('nonexistent')).to.equal(undefined);
// A set-cookie header which should always be an array
response.setHeader('set-cookie', 'foo');
// Don't send date
response.sendDate = false;
// Specifying more headers, the status code and a reason phrase with `writeHead` var moreHeaders = {};
moreHeaders[headerName] = headerValue;
response.writeHead(600, 'to be discarded', moreHeaders);
expect(response.getHeader(headerName)).to.equal(headerValue);
// Empty response body
response.end(message);
});
server.listen(1239, function() { var headers = {};
headers[headerName] = headerValue; var request = http2.request({
host: 'localhost',
port: 1239,
path: path,
headers: headers
});
request.end();
request.on('response', function(response) {
expect(response.headers[headerName]).to.equal(headerValue);
expect(response.headers['nonexistent']).to.equal(undefined);
expect(response.headers['set-cookie']).to.an.instanceof(Array)
expect(response.headers['set-cookie']).to.deep.equal(['foo'])
expect(response.headers['date']).to.equal(undefined);
response.on('data', function(data) {
expect(data.toString()).to.equal(message);
server.close();
done();
});
});
});
});
});
describe('request over plain TCP', function() {
it('should work as expected', function(done) { var path = '/x'; var message = 'Hello world';
var server = http2.raw.createServer({
log: util.serverLog
}, function(request, response) {
expect(request.url).to.equal(path);
response.end(message);
});
server.listen(1237, function() { var request = http2.raw.request({
plain: true,
host: 'localhost',
port: 1237,
path: path
}, function(response) {
response.on('data', function(data) {
expect(data.toString()).to.equal(message);
server.close();
done();
});
});
request.end();
});
});
});
describe('get over plain TCP', function() {
it('should work as expected', function(done) { var path = '/x'; var message = 'Hello world';
var server = http2.raw.createServer({
log: util.serverLog
}, function(request, response) {
expect(request.url).to.equal(path);
response.end(message);
});
server.listen(1237, function() {
var request = http2.raw.get('http://localhost:1237/x', function(response) {
response.on('data', function(data) {
expect(data.toString()).to.equal(message);
server.close();
done();
});
});
request.end();
});
});
});
describe('request to an HTTPS/1 server', function() {
it('should fall back to HTTPS/1 successfully', function(done) {
var path = '/x';
var message = 'Hello world';
var server = https.createServer(serverOptions, function(request, response) {
expect(request.url).to.equal(path);
response.end(message);
});
server.listen(5678, function() {
http2.get('https://localhost:5678' + path, function(response) {
response.on('data', function(data) {
expect(data.toString()).to.equal(message);
done();
});
});
});
});
});
describe('2 parallel request to an HTTPS/1 server', function() {
it('should fall back to HTTPS/1 successfully', function(originalDone) {
var path = '/x';
var message = 'Hello world';
var done = util.callNTimes(2, function() {
server.close();
originalDone();
});
var server = https.createServer(serverOptions, function(request, response) {
expect(request.url).to.equal(path);
response.end(message);
});
server.listen(6789, function() {
http2.get('https://localhost:6789' + path, function(response) {
response.on('data', function(data) {
expect(data.toString()).to.equal(message);
done();
});
});
http2.get('https://localhost:6789' + path, function(response) {
response.on('data', function(data) {
expect(data.toString()).to.equal(message);
done();
});
});
});
});
});
describe('HTTPS/1 request to a HTTP/2 server', function() {
it('should fall back to HTTPS/1 successfully', function(done) {
var path = '/x';
var message = 'Hello world';
var server = http2.createServer(serverOptions, function(request, response) {
expect(request.url).to.equal(path);
response.end(message);
});
server.listen(1236, function() {
var options = url.parse('https://localhost:1236' + path);
options.agent = new https.Agent(agentOptions);
https.get(options, function(response) {
response.on('data', function(data) {
expect(data.toString()).to.equal(message);
done();
});
});
});
});
});
describe('two parallel request', function() {
it('should work as expected', function(done) {
var path = '/x';
var message = 'Hello world';
var server = http2.createServer(serverOptions, function(request, response) {
expect(request.url).to.equal(path);
response.end(message);
});
request.on('error', function (err) {
expect(err.errno).to.equal('ECONNREFUSED');
done();
});
request.on('response', function (response) {
server._server._handle.destroy();
response.on('data', util.noop);
response.once('end', function () {
done(new Error('Request should have failed'));
});
});
request.end();
});
});
describe('server push', function() {
it('should work as expected', function(done) {
var path = '/x';
var message = 'Hello world';
var pushedPath = '/y';
var pushedMessage = 'Hello world 2';
var server = http2.createServer(serverOptions, function(request, response) {
expect(request.url).to.equal(path);
var push1 = response.push('/y');
push1.end(pushedMessage);
var push2 = response.push({ path: '/y', protocol: 'https:' });
push2.end(pushedMessage);
response.end(message);
});
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.