• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23// Uploading a big file via HTTPS causes node to drop out of the event loop.
24// https://github.com/joyent/node/issues/892
25// In this test we set up an HTTPS in this process and launch a subprocess
26// to POST a 32mb file to us. A bug in the pause/resume functionality of the
27// TLS server causes the child process to exit cleanly before having sent
28// the entire buffer.
29const common = require('../common');
30if (!common.hasCrypto)
31  common.skip('missing crypto');
32
33const assert = require('assert');
34const spawn = require('child_process').spawn;
35const https = require('https');
36const fixtures = require('../common/fixtures');
37
38const bytesExpected = 1024 * 1024 * 32;
39
40let started = false;
41
42const childScript = fixtures.path('GH-892-request.js');
43
44function makeRequest() {
45  if (started) return;
46  started = true;
47
48  let stderrBuffer = '';
49
50  // Pass along --trace-deprecation/--throw-deprecation in
51  // process.execArgv to track down nextTick recursion errors
52  // more easily.  Also, this is handy when using this test to
53  // view V8 opt/deopt behavior.
54  const args = process.execArgv.concat([ childScript,
55                                         server.address().port,
56                                         bytesExpected ]);
57
58  const child = spawn(process.execPath, args);
59
60  child.on('exit', function(code) {
61    assert.ok(/DONE/.test(stderrBuffer));
62    assert.strictEqual(code, 0);
63  });
64
65  // The following two lines forward the stdio from the child
66  // to parent process for debugging.
67  child.stderr.pipe(process.stderr);
68  child.stdout.pipe(process.stdout);
69
70
71  // Buffer the stderr so that we can check that it got 'DONE'
72  child.stderr.setEncoding('ascii');
73  child.stderr.on('data', function(d) {
74    stderrBuffer += d;
75  });
76}
77
78
79const serverOptions = {
80  key: fixtures.readKey('agent1-key.pem'),
81  cert: fixtures.readKey('agent1-cert.pem')
82};
83
84let uploadCount = 0;
85
86const server = https.Server(serverOptions, function(req, res) {
87  // Close the server immediately. This test is only doing a single upload.
88  // We need to make sure the server isn't keeping the event loop alive
89  // while the upload is in progress.
90  server.close();
91
92  req.on('data', function(d) {
93    process.stderr.write('.');
94    uploadCount += d.length;
95  });
96
97  req.on('end', function() {
98    assert.strictEqual(uploadCount, bytesExpected);
99    res.writeHead(200, { 'content-type': 'text/plain' });
100    res.end('successful upload\n');
101  });
102});
103
104server.listen(0, function() {
105  console.log(`expecting ${bytesExpected} bytes`);
106  makeRequest();
107});
108
109process.on('exit', function() {
110  console.error(`got ${uploadCount} bytes`);
111  assert.strictEqual(uploadCount, bytesExpected);
112});
113