|
| 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 | +// Uploading a big file via HTTPS causes node to drop out of the event loop. |
| 23 | +// https://github.com/joyent/node/issues/892 |
| 24 | +// In this test we set up an HTTPS in this process and launch a subprocess |
| 25 | +// to POST a 32mb file to us. A bug in the pause/resume functionality of the |
| 26 | +// TLS server causes the child process to exit cleanly before having sent |
| 27 | +// the entire buffer. |
| 28 | +var common = require('../common'); |
| 29 | +var assert = require('assert'); |
| 30 | +var spawn = require('child_process').spawn; |
| 31 | +var https = require('https'); |
| 32 | +var fs = require('fs'); |
| 33 | + |
| 34 | +var PORT = 8000 |
| 35 | + |
| 36 | + |
| 37 | +var bytesExpected = 1024 * 1024 * 32; |
| 38 | +var gotResponse = false; |
| 39 | + |
| 40 | +var started = false; |
| 41 | + |
| 42 | +var childScript = require('path').join(common.fixturesDir, 'GH-892-request.js'); |
| 43 | + |
| 44 | +function makeRequest() { |
| 45 | + if (started) return; |
| 46 | + started = true; |
| 47 | + |
| 48 | + var stderrBuffer = ''; |
| 49 | + |
| 50 | + var child = spawn(process.execPath, |
| 51 | + [ childScript, common.PORT, bytesExpected ]); |
| 52 | + |
| 53 | + child.on('exit', function(code) { |
| 54 | + assert.ok(/DONE/.test(stderrBuffer)); |
| 55 | + assert.equal(0, code); |
| 56 | + }); |
| 57 | + |
| 58 | + // The following two lines forward the stdio from the child |
| 59 | + // to parent process for debugging. |
| 60 | + child.stderr.pipe(process.stderr); |
| 61 | + child.stdout.pipe(process.stdout); |
| 62 | + |
| 63 | + |
| 64 | + // Buffer the stderr so that we can check that it got 'DONE' |
| 65 | + child.stderr.setEncoding('ascii'); |
| 66 | + child.stderr.on('data', function(d) { |
| 67 | + stderrBuffer += d; |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +var serverOptions = { |
| 73 | + key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), |
| 74 | + cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem') |
| 75 | +}; |
| 76 | + |
| 77 | +var uploadCount = 0; |
| 78 | + |
| 79 | +var server = https.Server(serverOptions, function(req, res) { |
| 80 | + // Close the server immediately. This test is only doing a single upload. |
| 81 | + // We need to make sure the server isn't keeping the event loop alive |
| 82 | + // while the upload is in progress. |
| 83 | + server.close(); |
| 84 | + |
| 85 | + req.on('data', function(d) { |
| 86 | + process.stderr.write('.'); |
| 87 | + uploadCount += d.length; |
| 88 | + }); |
| 89 | + |
| 90 | + req.on('end', function() { |
| 91 | + assert.equal(bytesExpected, uploadCount); |
| 92 | + res.writeHead(200, {'content-type': 'text/plain'}); |
| 93 | + res.end('successful upload\n'); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +server.listen(common.PORT, function() { |
| 98 | + console.log("expecting %d bytes", bytesExpected); |
| 99 | + makeRequest(); |
| 100 | +}); |
| 101 | + |
| 102 | +process.on('exit', function() { |
| 103 | + console.error("got %d bytes", uploadCount); |
| 104 | + assert.equal(uploadCount, bytesExpected); |
| 105 | +}); |
0 commit comments