testing/xpcshell/node-http2/test/http.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 var expect = require('chai').expect;
michael@0 2 var util = require('./util');
michael@0 3 var fs = require('fs');
michael@0 4 var path = require('path');
michael@0 5
michael@0 6 var http2 = require('../lib/http');
michael@0 7 var https = require('https');
michael@0 8
michael@0 9 process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
michael@0 10
michael@0 11 var options = {
michael@0 12 key: fs.readFileSync(path.join(__dirname, '../example/localhost.key')),
michael@0 13 cert: fs.readFileSync(path.join(__dirname, '../example/localhost.crt')),
michael@0 14 log: util.log
michael@0 15 };
michael@0 16
michael@0 17 http2.globalAgent = new http2.Agent({ log: util.log });
michael@0 18
michael@0 19 describe('http.js', function() {
michael@0 20 describe('Server', function() {
michael@0 21 describe('new Server(options)', function() {
michael@0 22 it('should throw if called without \'plain\' or TLS options', function() {
michael@0 23 expect(function() {
michael@0 24 new http2.Server();
michael@0 25 }).to.throw(Error);
michael@0 26 expect(function() {
michael@0 27 http2.createServer(util.noop);
michael@0 28 }).to.throw(Error);
michael@0 29 });
michael@0 30 });
michael@0 31 describe('property `timeout`', function() {
michael@0 32 it('should be a proxy for the backing HTTPS server\'s `timeout` property', function() {
michael@0 33 var server = new http2.Server(options);
michael@0 34 var backingServer = server._server;
michael@0 35 var newTimeout = 10;
michael@0 36 server.timeout = newTimeout;
michael@0 37 expect(server.timeout).to.be.equal(newTimeout);
michael@0 38 expect(backingServer.timeout).to.be.equal(newTimeout);
michael@0 39 });
michael@0 40 });
michael@0 41 describe('method `setTimeout(timeout, [callback])`', function() {
michael@0 42 it('should be a proxy for the backing HTTPS server\'s `setTimeout` method', function() {
michael@0 43 var server = new http2.Server(options);
michael@0 44 var backingServer = server._server;
michael@0 45 var newTimeout = 10;
michael@0 46 var newCallback = util.noop;
michael@0 47 backingServer.setTimeout = function(timeout, callback) {
michael@0 48 expect(timeout).to.be.equal(newTimeout);
michael@0 49 expect(callback).to.be.equal(newCallback);
michael@0 50 };
michael@0 51 server.setTimeout(newTimeout, newCallback);
michael@0 52 });
michael@0 53 });
michael@0 54 });
michael@0 55 describe('Agent', function() {
michael@0 56 describe('property `maxSockets`', function() {
michael@0 57 it('should be a proxy for the backing HTTPS agent\'s `maxSockets` property', function() {
michael@0 58 var agent = new http2.Agent({ log: util.log });
michael@0 59 var backingAgent = agent._httpsAgent;
michael@0 60 var newMaxSockets = backingAgent.maxSockets + 1;
michael@0 61 agent.maxSockets = newMaxSockets;
michael@0 62 expect(agent.maxSockets).to.be.equal(newMaxSockets);
michael@0 63 expect(backingAgent.maxSockets).to.be.equal(newMaxSockets);
michael@0 64 });
michael@0 65 });
michael@0 66 describe('method `request(options, [callback])`', function() {
michael@0 67 it('should throw when trying to use with \'http\' scheme', function() {
michael@0 68 expect(function() {
michael@0 69 var agent = new http2.Agent({ log: util.log });
michael@0 70 agent.request({ protocol: 'http:' });
michael@0 71 }).to.throw(Error);
michael@0 72 });
michael@0 73 });
michael@0 74 });
michael@0 75 describe('OutgoingRequest', function() {
michael@0 76 function testFallbackProxyMethod(name, originalArguments, done) {
michael@0 77 var request = new http2.OutgoingRequest();
michael@0 78
michael@0 79 // When in HTTP/2 mode, this call should be ignored
michael@0 80 request.stream = { reset: util.noop };
michael@0 81 request[name].apply(request, originalArguments);
michael@0 82 delete request.stream;
michael@0 83
michael@0 84 // When in fallback mode, this call should be forwarded
michael@0 85 request[name].apply(request, originalArguments);
michael@0 86 var mockFallbackRequest = { on: util.noop };
michael@0 87 mockFallbackRequest[name] = function() {
michael@0 88 expect(arguments).to.deep.equal(originalArguments);
michael@0 89 done();
michael@0 90 };
michael@0 91 request._fallback(mockFallbackRequest);
michael@0 92 }
michael@0 93 describe('method `setNoDelay(noDelay)`', function() {
michael@0 94 it('should act as a proxy for the backing HTTPS agent\'s `setNoDelay` method', function(done) {
michael@0 95 testFallbackProxyMethod('setNoDelay', [true], done);
michael@0 96 });
michael@0 97 });
michael@0 98 describe('method `setSocketKeepAlive(enable, initialDelay)`', function() {
michael@0 99 it('should act as a proxy for the backing HTTPS agent\'s `setSocketKeepAlive` method', function(done) {
michael@0 100 testFallbackProxyMethod('setSocketKeepAlive', [true, util.random(10, 100)], done);
michael@0 101 });
michael@0 102 });
michael@0 103 describe('method `setTimeout(timeout, [callback])`', function() {
michael@0 104 it('should act as a proxy for the backing HTTPS agent\'s `setTimeout` method', function(done) {
michael@0 105 testFallbackProxyMethod('setTimeout', [util.random(10, 100), util.noop], done);
michael@0 106 });
michael@0 107 });
michael@0 108 describe('method `abort()`', function() {
michael@0 109 it('should act as a proxy for the backing HTTPS agent\'s `abort` method', function(done) {
michael@0 110 testFallbackProxyMethod('abort', [], done);
michael@0 111 });
michael@0 112 });
michael@0 113 });
michael@0 114 describe('test scenario', function() {
michael@0 115 describe('simple request', function() {
michael@0 116 it('should work as expected', function(done) {
michael@0 117 var path = '/x';
michael@0 118 var message = 'Hello world';
michael@0 119
michael@0 120 var server = http2.createServer(options, function(request, response) {
michael@0 121 expect(request.url).to.equal(path);
michael@0 122 response.end(message);
michael@0 123 });
michael@0 124
michael@0 125 server.listen(1234, function() {
michael@0 126 http2.get('https://localhost:1234' + path, function(response) {
michael@0 127 response.on('readable', function() {
michael@0 128 expect(response.read().toString()).to.equal(message);
michael@0 129 server.close();
michael@0 130 done();
michael@0 131 });
michael@0 132 });
michael@0 133 });
michael@0 134 });
michael@0 135 });
michael@0 136 describe('request with payload', function() {
michael@0 137 it('should work as expected', function(done) {
michael@0 138 var path = '/x';
michael@0 139 var message = 'Hello world';
michael@0 140
michael@0 141 var server = http2.createServer(options, function(request, response) {
michael@0 142 expect(request.url).to.equal(path);
michael@0 143 request.once('readable', function() {
michael@0 144 expect(request.read().toString()).to.equal(message);
michael@0 145 response.end();
michael@0 146 });
michael@0 147 });
michael@0 148
michael@0 149 server.listen(1240, function() {
michael@0 150 var request = http2.request({
michael@0 151 host: 'localhost',
michael@0 152 port: 1240,
michael@0 153 path: path
michael@0 154 });
michael@0 155 request.write(message);
michael@0 156 request.end();
michael@0 157 request.on('response', function() {
michael@0 158 server.close();
michael@0 159 done();
michael@0 160 });
michael@0 161 });
michael@0 162 });
michael@0 163 });
michael@0 164 describe('request with custom status code and headers', function() {
michael@0 165 it('should work as expected', function(done) {
michael@0 166 var path = '/x';
michael@0 167 var message = 'Hello world';
michael@0 168 var headerName = 'name';
michael@0 169 var headerValue = 'value';
michael@0 170
michael@0 171 var server = http2.createServer(options, function(request, response) {
michael@0 172 // Request URL and headers
michael@0 173 expect(request.url).to.equal(path);
michael@0 174 expect(request.headers[headerName]).to.equal(headerValue);
michael@0 175
michael@0 176 // A header to be overwritten later
michael@0 177 response.setHeader(headerName, 'to be overwritten');
michael@0 178 expect(response.getHeader(headerName)).to.equal('to be overwritten');
michael@0 179
michael@0 180 // A header to be deleted
michael@0 181 response.setHeader('nonexistent', 'x');
michael@0 182 response.removeHeader('nonexistent');
michael@0 183 expect(response.getHeader('nonexistent')).to.equal(undefined);
michael@0 184
michael@0 185 // Don't send date
michael@0 186 response.sendDate = false;
michael@0 187
michael@0 188 // Specifying more headers, the status code and a reason phrase with `writeHead`
michael@0 189 var moreHeaders = {};
michael@0 190 moreHeaders[headerName] = headerValue;
michael@0 191 response.writeHead(600, 'to be discarded', moreHeaders);
michael@0 192 expect(response.getHeader(headerName)).to.equal(headerValue);
michael@0 193
michael@0 194 // Empty response body
michael@0 195 response.end(message);
michael@0 196 });
michael@0 197
michael@0 198 server.listen(1239, function() {
michael@0 199 var headers = {};
michael@0 200 headers[headerName] = headerValue;
michael@0 201 var request = http2.request({
michael@0 202 host: 'localhost',
michael@0 203 port: 1239,
michael@0 204 path: path,
michael@0 205 headers: headers
michael@0 206 });
michael@0 207 request.end();
michael@0 208 request.on('response', function(response) {
michael@0 209 expect(response.headers[headerName]).to.equal(headerValue);
michael@0 210 expect(response.headers['nonexistent']).to.equal(undefined);
michael@0 211 expect(response.headers['date']).to.equal(undefined);
michael@0 212 response.on('readable', function() {
michael@0 213 expect(response.read().toString()).to.equal(message);
michael@0 214 server.close();
michael@0 215 done();
michael@0 216 });
michael@0 217 });
michael@0 218 });
michael@0 219 });
michael@0 220 });
michael@0 221 describe('request over plain TCP', function() {
michael@0 222 it('should work as expected', function(done) {
michael@0 223 var path = '/x';
michael@0 224 var message = 'Hello world';
michael@0 225
michael@0 226 var server = http2.createServer({
michael@0 227 plain: true,
michael@0 228 log: util.log
michael@0 229 }, function(request, response) {
michael@0 230 expect(request.url).to.equal(path);
michael@0 231 response.end(message);
michael@0 232 });
michael@0 233
michael@0 234 server.listen(1237, function() {
michael@0 235 var request = http2.request({
michael@0 236 plain: true,
michael@0 237 host: 'localhost',
michael@0 238 port: 1237,
michael@0 239 path: path
michael@0 240 }, function(response) {
michael@0 241 response.on('readable', function() {
michael@0 242 expect(response.read().toString()).to.equal(message);
michael@0 243 server.close();
michael@0 244 done();
michael@0 245 });
michael@0 246 });
michael@0 247 request.end();
michael@0 248 });
michael@0 249 });
michael@0 250 });
michael@0 251 describe('request to an HTTPS/1 server', function() {
michael@0 252 it('should fall back to HTTPS/1 successfully', function(done) {
michael@0 253 var path = '/x';
michael@0 254 var message = 'Hello world';
michael@0 255
michael@0 256 var server = https.createServer(options, function(request, response) {
michael@0 257 expect(request.url).to.equal(path);
michael@0 258 response.end(message);
michael@0 259 });
michael@0 260
michael@0 261 server.listen(5678, function() {
michael@0 262 http2.get('https://localhost:5678' + path, function(response) {
michael@0 263 response.on('readable', function() {
michael@0 264 expect(response.read().toString()).to.equal(message);
michael@0 265 done();
michael@0 266 });
michael@0 267 });
michael@0 268 });
michael@0 269 });
michael@0 270 });
michael@0 271 describe('HTTPS/1 request to a HTTP/2 server', function() {
michael@0 272 it('should fall back to HTTPS/1 successfully', function(done) {
michael@0 273 var path = '/x';
michael@0 274 var message = 'Hello world';
michael@0 275
michael@0 276 var server = http2.createServer(options, function(request, response) {
michael@0 277 expect(request.url).to.equal(path);
michael@0 278 response.end(message);
michael@0 279 });
michael@0 280
michael@0 281 server.listen(1236, function() {
michael@0 282 https.get('https://localhost:1236' + path, function(response) {
michael@0 283 response.on('readable', function() {
michael@0 284 expect(response.read().toString()).to.equal(message);
michael@0 285 done();
michael@0 286 });
michael@0 287 });
michael@0 288 });
michael@0 289 });
michael@0 290 });
michael@0 291 describe('two parallel request', function() {
michael@0 292 it('should work as expected', function(done) {
michael@0 293 var path = '/x';
michael@0 294 var message = 'Hello world';
michael@0 295
michael@0 296 var server = http2.createServer(options, function(request, response) {
michael@0 297 expect(request.url).to.equal(path);
michael@0 298 response.end(message);
michael@0 299 });
michael@0 300
michael@0 301 server.listen(1237, function() {
michael@0 302 done = util.callNTimes(2, done);
michael@0 303 // 1. request
michael@0 304 http2.get('https://localhost:1237' + path, function(response) {
michael@0 305 response.on('readable', function() {
michael@0 306 expect(response.read().toString()).to.equal(message);
michael@0 307 done();
michael@0 308 });
michael@0 309 });
michael@0 310 // 2. request
michael@0 311 http2.get('https://localhost:1237' + path, function(response) {
michael@0 312 response.on('readable', function() {
michael@0 313 expect(response.read().toString()).to.equal(message);
michael@0 314 done();
michael@0 315 });
michael@0 316 });
michael@0 317 });
michael@0 318 });
michael@0 319 });
michael@0 320 describe('two subsequent request', function() {
michael@0 321 it('should use the same HTTP/2 connection', function(done) {
michael@0 322 var path = '/x';
michael@0 323 var message = 'Hello world';
michael@0 324
michael@0 325 var server = http2.createServer(options, function(request, response) {
michael@0 326 expect(request.url).to.equal(path);
michael@0 327 response.end(message);
michael@0 328 });
michael@0 329
michael@0 330 server.listen(1238, function() {
michael@0 331 // 1. request
michael@0 332 http2.get('https://localhost:1238' + path, function(response) {
michael@0 333 response.on('readable', function() {
michael@0 334 expect(response.read().toString()).to.equal(message);
michael@0 335
michael@0 336 // 2. request
michael@0 337 http2.get('https://localhost:1238' + path, function(response) {
michael@0 338 response.on('readable', function() {
michael@0 339 expect(response.read().toString()).to.equal(message);
michael@0 340 done();
michael@0 341 });
michael@0 342 });
michael@0 343 });
michael@0 344 });
michael@0 345 });
michael@0 346 });
michael@0 347 });
michael@0 348 describe('request and response with trailers', function() {
michael@0 349 it('should work as expected', function(done) {
michael@0 350 var path = '/x';
michael@0 351 var message = 'Hello world';
michael@0 352 var requestTrailers = { 'content-md5': 'x' };
michael@0 353 var responseTrailers = { 'content-md5': 'y' };
michael@0 354
michael@0 355 var server = http2.createServer(options, function(request, response) {
michael@0 356 expect(request.url).to.equal(path);
michael@0 357 request.on('data', util.noop);
michael@0 358 request.once('end', function() {
michael@0 359 expect(request.trailers).to.deep.equal(requestTrailers);
michael@0 360 response.write(message);
michael@0 361 response.addTrailers(responseTrailers);
michael@0 362 response.end();
michael@0 363 });
michael@0 364 });
michael@0 365
michael@0 366 server.listen(1241, function() {
michael@0 367 var request = http2.request('https://localhost:1241' + path);
michael@0 368 request.addTrailers(requestTrailers);
michael@0 369 request.end();
michael@0 370 request.on('response', function(response) {
michael@0 371 response.on('data', util.noop);
michael@0 372 response.once('end', function() {
michael@0 373 expect(response.trailers).to.deep.equal(responseTrailers);
michael@0 374 done();
michael@0 375 });
michael@0 376 });
michael@0 377 });
michael@0 378 });
michael@0 379 });
michael@0 380 describe('server push', function() {
michael@0 381 it('should work as expected', function(done) {
michael@0 382 var path = '/x';
michael@0 383 var message = 'Hello world';
michael@0 384 var pushedPath = '/y';
michael@0 385 var pushedMessage = 'Hello world 2';
michael@0 386
michael@0 387 var server = http2.createServer(options, function(request, response) {
michael@0 388 expect(request.url).to.equal(path);
michael@0 389 var push1 = response.push('/y');
michael@0 390 push1.end(pushedMessage);
michael@0 391 var push2 = response.push({ path: '/y', protocol: 'https:' });
michael@0 392 push2.end(pushedMessage);
michael@0 393 response.end(message);
michael@0 394 });
michael@0 395
michael@0 396 server.listen(1235, function() {
michael@0 397 var request = http2.get('https://localhost:1235' + path);
michael@0 398 done = util.callNTimes(5, done);
michael@0 399
michael@0 400 request.on('response', function(response) {
michael@0 401 response.on('readable', function() {
michael@0 402 expect(response.read().toString()).to.equal(message);
michael@0 403 done();
michael@0 404 });
michael@0 405 response.on('end', done);
michael@0 406 });
michael@0 407
michael@0 408 request.on('push', function(promise) {
michael@0 409 expect(promise.url).to.be.equal(pushedPath);
michael@0 410 promise.on('response', function(pushStream) {
michael@0 411 pushStream.on('readable', function() {
michael@0 412 expect(pushStream.read().toString()).to.equal(pushedMessage);
michael@0 413 done();
michael@0 414 });
michael@0 415 pushStream.on('end', done);
michael@0 416 });
michael@0 417 });
michael@0 418 });
michael@0 419 });
michael@0 420 });
michael@0 421 });
michael@0 422 });

mercurial