Fixed proxy test.

This commit is contained in:
Simone Bordet 2013-07-25 17:20:17 +02:00
parent 3b18490ead
commit 06504f2c65
1 changed files with 34 additions and 25 deletions

View File

@ -36,12 +36,12 @@ public class ProxyFakeTunnelTest extends ProxyTunnellingTest
{ {
return _proxySocket.getLocalPort(); return _proxySocket.getLocalPort();
} }
protected void startProxy() throws Exception protected void startProxy() throws Exception
{ {
_proxySocket = new ServerSocket(0); _proxySocket = new ServerSocket(0);
_proxyThread = new Thread() _proxyThread = new Thread()
{ {
@Override @Override
@ -63,19 +63,28 @@ public class ProxyFakeTunnelTest extends ProxyTunnellingTest
}; };
_proxyThread.setDaemon(true); _proxyThread.setDaemon(true);
_proxyThread.start(); _proxyThread.start();
} }
protected void stopProxy() throws Exception protected void stopProxy() throws Exception
{ {
_proxySocket.close(); if (_proxySocket != null)
_proxyThread.interrupt(); {
_proxySocket.close();
_proxyThread.interrupt();
}
}
@Override
public void testExternalProxy() throws Exception
{
// Do not execute this test, since it won't hit the fake proxy
} }
static class FakeProxy extends Thread static class FakeProxy extends Thread
{ {
Socket _socket; Socket _socket;
public FakeProxy(Socket socket) public FakeProxy(Socket socket)
{ {
_socket=socket; _socket=socket;
@ -86,50 +95,50 @@ public class ProxyFakeTunnelTest extends ProxyTunnellingTest
Socket toserver=null; Socket toserver=null;
final InputStream in; final InputStream in;
final OutputStream out; final OutputStream out;
try try
{ {
in = _socket.getInputStream(); in = _socket.getInputStream();
out = _socket.getOutputStream(); out = _socket.getOutputStream();
String address=""; String address="";
int state=0; int state=0;
for (int b=in.read();b>=0;b=in.read()) for (int b=in.read();b>=0;b=in.read())
{ {
switch(state) switch(state)
{ {
case 0: case 0:
if (' '==b) if (' '==b)
state=1; state=1;
break; break;
case 1: case 1:
if (' '==b) if (' '==b)
state=2; state=2;
else else
address+=(char)b; address+=(char)b;
break; break;
case 2: case 2:
if ('\r'==b) if ('\r'==b)
state=3; state=3;
break; break;
case 3: case 3:
if ('\n'==b) if ('\n'==b)
state=4; state=4;
else else
state=2; state=2;
break; break;
case 4: case 4:
if ('\r'==b) if ('\r'==b)
state=5; state=5;
else else
state=2; state=2;
break; break;
case 5: case 5:
if ('\n'==b) if ('\n'==b)
{ {
@ -142,7 +151,7 @@ public class ProxyFakeTunnelTest extends ProxyTunnellingTest
out.write(( out.write((
"HTTP/1.1 200 OK\r\n"+ "HTTP/1.1 200 OK\r\n"+
"Server: fake\r\n"+ "Server: fake\r\n"+
// "Content-Length: 0\r\n"+ // "Content-Length: 0\r\n"+
"\r\n" "\r\n"
).getBytes()); ).getBytes());
} }
@ -151,12 +160,12 @@ public class ProxyFakeTunnelTest extends ProxyTunnellingTest
out.write(( out.write((
"HTTP/1.1 503 Unavailable\r\n"+ "HTTP/1.1 503 Unavailable\r\n"+
"Server: fake\r\n"+ "Server: fake\r\n"+
"Content-Length: 0\r\n"+ "Content-Length: 0\r\n"+
"\r\n" "\r\n"
).getBytes()); ).getBytes());
} }
out.flush(); out.flush();
if (toserver!=null) if (toserver!=null)
{ {
final InputStream from = toserver.getInputStream(); final InputStream from = toserver.getInputStream();
@ -188,7 +197,7 @@ public class ProxyFakeTunnelTest extends ProxyTunnellingTest
copy.setDaemon(true); copy.setDaemon(true);
copy.start(); copy.start();
} }
} }
else else
state=2; state=2;
@ -196,11 +205,11 @@ public class ProxyFakeTunnelTest extends ProxyTunnellingTest
case 6: case 6:
toserver.getOutputStream().write((byte)b); toserver.getOutputStream().write((byte)b);
} }
} }
} }
catch (IOException e) catch (IOException e)
{ {
@ -221,8 +230,8 @@ public class ProxyFakeTunnelTest extends ProxyTunnellingTest
} }
} }
} }
} }
} }