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'; 23const common = require('../common'); 24 25if (!common.hasCrypto) 26 common.skip('missing crypto'); 27 28const fixtures = require('../common/fixtures'); 29const https = require('https'); 30const http = require('http'); 31 32const options = { 33 key: fixtures.readKey('agent1-key.pem'), 34 cert: fixtures.readKey('agent1-cert.pem') 35}; 36 37const body = 'hello world\n'; 38 39// Try first with http server 40 41const server_http = http.createServer(function(req, res) { 42 console.log('got HTTP request'); 43 res.writeHead(200, { 'content-type': 'text/plain' }); 44 res.end(body); 45}); 46 47 48server_http.listen(0, function() { 49 const req = http.request({ 50 port: this.address().port, 51 rejectUnauthorized: false 52 }, function(res) { 53 server_http.close(); 54 res.resume(); 55 }); 56 // These methods should exist on the request and get passed down to the socket 57 req.setNoDelay(true); 58 req.setTimeout(1000, () => {}); 59 req.setSocketKeepAlive(true, 1000); 60 req.end(); 61}); 62 63// Then try https server (requires functions to be 64// mirrored in tls.js's CryptoStream) 65 66const server_https = https.createServer(options, function(req, res) { 67 console.log('got HTTPS request'); 68 res.writeHead(200, { 'content-type': 'text/plain' }); 69 res.end(body); 70}); 71 72server_https.listen(0, function() { 73 const req = https.request({ 74 port: this.address().port, 75 rejectUnauthorized: false 76 }, function(res) { 77 server_https.close(); 78 res.resume(); 79 }); 80 // These methods should exist on the request and get passed down to the socket 81 req.setNoDelay(true); 82 req.setTimeout(1000, () => {}); 83 req.setSocketKeepAlive(true, 1000); 84 req.end(); 85}); 86