1 | var http = require('http');
|
2 | var net = require('net');
|
3 | var should = require('should');
|
4 | var tunnel = require('../index');
|
5 |
|
6 | describe('HTTP over HTTP internal server error', function() {
|
7 | it('should finish without error', function(done) {
|
8 | var serverPort = 3010;
|
9 | var proxyPort = 3011;
|
10 | var poolSize = 3;
|
11 | var proxyConnect = 0;
|
12 | var clientConnect = 0;
|
13 | var proxyClosed = false;
|
14 | var clientClosed = false;
|
15 | var clientGotSocket = false;
|
16 | var clientGotError = false;
|
17 | var proxy;
|
18 | var agent;
|
19 |
|
20 | proxy = http.createServer(function(req, res) {
|
21 | should.fail();
|
22 | });
|
23 | proxy.on('upgrade', onConnect);
|
24 | proxy.on('connect', onConnect);
|
25 |
|
26 | function onConnect(req, clientSocket, head) {
|
27 | tunnel.debug('PROXY: got CONNECT request');
|
28 |
|
29 | req.method.should.equal('CONNECT');
|
30 | req.url.should.equal('localhost:' + serverPort);
|
31 | req.headers.should.not.have.property('transfer-encoding');
|
32 | req.headers.should.have.property('proxy-authorization',
|
33 | 'Basic ' + new Buffer('user:password').toString('base64'));
|
34 | ++proxyConnect;
|
35 |
|
36 | clientSocket.on('close', function() {
|
37 | proxyClosed = true;
|
38 | proxy.close();
|
39 | });
|
40 | tunnel.debug('PROXY: returning 500');
|
41 | clientSocket.write('HTTP/1.1 500 Internal Server Error\r\n\r\n');
|
42 | clientSocket.end();
|
43 | }
|
44 | proxy.listen(proxyPort, setupClient);
|
45 |
|
46 | function setupClient() {
|
47 | agent = tunnel.httpOverHttp({
|
48 | maxSockets: poolSize,
|
49 | proxy: {
|
50 | port: proxyPort,
|
51 | proxyAuth: 'user:password'
|
52 | }
|
53 | });
|
54 |
|
55 | tunnel.debug('CLIENT: Making HTTP request');
|
56 | var req = http.get({
|
57 | port: serverPort,
|
58 | path: '/',
|
59 | agent: agent
|
60 | }, function(res) {
|
61 | tunnel.debug('CLIENT: got HTTP response');
|
62 | ++clientConnect;
|
63 | });
|
64 | req.on('socket', function(socket) {
|
65 | clientGotSocket = true;
|
66 | });
|
67 | req.on('error', function(err) {
|
68 | clientGotError = true;
|
69 | err.code.should.equal('ECONNRESET');
|
70 | });
|
71 | }
|
72 |
|
73 | proxy.on('close', function() {
|
74 | proxyConnect.should.equal(1);
|
75 | proxyClosed.should.ok();
|
76 |
|
77 | clientConnect.should.equal(0);
|
78 | clientGotSocket.should.not.ok();
|
79 | clientGotError.should.ok();
|
80 |
|
81 | agent.sockets.should.be.empty;
|
82 | agent.requests.should.be.empty;
|
83 |
|
84 | done();
|
85 | });
|
86 | });
|
87 | });
|