367219 - WebSocketClient.open() fails when URI uses default ports.

This commit is contained in:
Simone Bordet 2011-12-20 18:01:45 +01:00
parent ecb88f836e
commit 8ea22a6a4c
2 changed files with 59 additions and 18 deletions

View File

@ -332,18 +332,23 @@ public class WebSocketClient
{
if (!_factory.isStarted())
throw new IllegalStateException("Factory !started");
String scheme=uri.getScheme();
String scheme = uri.getScheme();
if (!("ws".equalsIgnoreCase(scheme) || "wss".equalsIgnoreCase(scheme)))
throw new IllegalArgumentException("Bad WebSocket scheme '"+scheme+"'");
throw new IllegalArgumentException("Bad WebSocket scheme: " + scheme);
int port = uri.getPort();
if (port == 0)
throw new IllegalArgumentException("Bad WebSocket port: " + port);
if (port < 0)
port = "ws".equals(scheme) ? 80 : 443;
SocketChannel channel = SocketChannel.open();
if (_bindAddress != null)
channel.socket().bind(_bindAddress);
channel.socket().setTcpNoDelay(true);
InetSocketAddress address=new InetSocketAddress(uri.getHost(),uri.getPort());
InetSocketAddress address = new InetSocketAddress(uri.getHost(), port);
final WebSocketFuture holder=new WebSocketFuture(websocket,uri,this,channel);
WebSocketFuture holder = new WebSocketFuture(websocket, uri, this, channel);
channel.configureBlocking(false);
channel.connect(address);

View File

@ -15,8 +15,6 @@
*******************************************************************************/
package org.eclipse.jetty.websocket;
import static org.hamcrest.Matchers.greaterThan;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@ -45,6 +43,8 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.Matchers.greaterThan;
public class WebSocketClientTest
{
private WebSocketClientFactory _factory = new WebSocketClientFactory();
@ -148,7 +148,6 @@ public class WebSocketClientTest
Assert.assertFalse(open.get());
}
@Test
public void testAsyncConnectionRefused() throws Exception
{
@ -187,8 +186,6 @@ public class WebSocketClientTest
}
@Test
public void testConnectionNotAccepted() throws Exception
{
@ -266,7 +263,6 @@ public class WebSocketClientTest
}
@Test
public void testBadHandshake() throws Exception
{
@ -424,7 +420,6 @@ public class WebSocketClientTest
Assert.assertEquals(WebSocketConnectionRFC6455.CLOSE_NORMAL,close.get());
}
@Test
public void testNotIdle() throws Exception
{
@ -499,7 +494,6 @@ public class WebSocketClientTest
Assert.assertEquals("Invalid close code 1111", closeMessage.toString());
}
@Test
public void testBlockSending() throws Exception
{
@ -581,18 +575,17 @@ public class WebSocketClientTest
long writeDur = (System.currentTimeMillis() - start);
// wait for consumer to complete
while (totalB.get()<messages*(mesg.length()+6L))
while (totalB.get()<messages*(mesg.length()+6L))
{
Thread.sleep(10);
}
Assert.assertThat("write duration", writeDur, greaterThan(1000L)); // writing was blocked
Assert.assertEquals(messages*(mesg.length()+6L),totalB.get());
consumer.interrupt();
}
@Test
public void testBlockReceiving() throws Exception
{
@ -692,12 +685,12 @@ public class WebSocketClientTest
socket.getOutputStream().write(send,0,send.length);
socket.getOutputStream().flush();
}
while(consumer.isAlive())
while(consumer.isAlive())
{
Thread.sleep(10);
}
// Duration of the read operation.
long readDur = (System.currentTimeMillis() - start);
@ -715,6 +708,49 @@ public class WebSocketClientTest
Assert.assertEquals("Invalid close code 1111", closeMessage.toString());
}
@Test
public void testURIWithDefaultPort() throws Exception
{
WebSocketClient client = new WebSocketClient(_factory);
try
{
client.open(new URI("ws://localhost"), new WebSocket()
{
public void onOpen(Connection connection)
{
}
public void onClose(int closeCode, String message)
{
System.out.println("closeCode = " + closeCode);
}
}).get(5, TimeUnit.SECONDS);
}
catch (ExecutionException x)
{
Assert.assertTrue(x.getCause() instanceof ConnectException);
}
try
{
client.open(new URI("wss://localhost"), new WebSocket()
{
public void onOpen(Connection connection)
{
}
public void onClose(int closeCode, String message)
{
}
}).get(5, TimeUnit.SECONDS);
}
catch (ExecutionException x)
{
Assert.assertTrue(x.getCause() instanceof ConnectException);
}
}
private void respondToClient(Socket connection, String serverResponse) throws IOException
{
InputStream in = null;