|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const http = require('http'); |
| 6 | + |
| 7 | +for (const path of [ |
| 8 | + '', |
| 9 | + 'example.com', |
| 10 | + 'example.com:0', |
| 11 | + 'example.com:65536', |
| 12 | + 'example.com:8080/example', |
| 13 | + 'evil.com:666/good.org:777', |
| 14 | + '/example.com', |
| 15 | +]) { |
| 16 | + assert.throws(() => http.request({ |
| 17 | + method: 'CONNECT', |
| 18 | + path, |
| 19 | + }), { |
| 20 | + code: 'ERR_INVALID_ARG_VALUE', |
| 21 | + name: 'TypeError', |
| 22 | + message: /^The property 'options\.path' must be a valid host:port combo\./, |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +{ |
| 27 | + const server = http.createServer(common.mustNotCall()); |
| 28 | + |
| 29 | + server.on('connect', common.mustCall((req, socket) => { |
| 30 | + assert.strictEqual(req.url, 'example.com:80'); |
| 31 | + socket.end('HTTP/1.1 501 Not Implemented\r\n\r\n'); |
| 32 | + })); |
| 33 | + |
| 34 | + server.listen(0, common.mustCall(() => { |
| 35 | + const port = server.address().port; |
| 36 | + const req = http.request( |
| 37 | + new URL(`http://localhost:${port}/example.com:80`), |
| 38 | + { method: 'CONNECT' }, |
| 39 | + ); |
| 40 | + |
| 41 | + req.on('connect', common.mustCall((res, socket) => { |
| 42 | + assert.strictEqual(res.statusCode, 501); |
| 43 | + socket.destroy(); |
| 44 | + server.close(); |
| 45 | + })); |
| 46 | + |
| 47 | + req.end(); |
| 48 | + })); |
| 49 | +} |
| 50 | + |
| 51 | +{ |
| 52 | + const server = http.createServer(common.mustNotCall()); |
| 53 | + |
| 54 | + server.on('connect', common.mustCall((req, socket) => { |
| 55 | + assert.strictEqual(req.url, '[2001:db8::1]:111'); |
| 56 | + socket.end('HTTP/1.1 501 Not Implemented\r\n\r\n'); |
| 57 | + })); |
| 58 | + |
| 59 | + server.listen(0, common.mustCall(() => { |
| 60 | + const req = http.request({ |
| 61 | + host: 'localhost', |
| 62 | + port: server.address().port, |
| 63 | + method: 'CONNECT', |
| 64 | + path: '[2001:db8::1]:111', |
| 65 | + }); |
| 66 | + |
| 67 | + req.on('connect', common.mustCall((res, socket) => { |
| 68 | + assert.strictEqual(res.statusCode, 501); |
| 69 | + socket.destroy(); |
| 70 | + server.close(); |
| 71 | + })); |
| 72 | + |
| 73 | + req.end(); |
| 74 | + })); |
| 75 | +} |
0 commit comments