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

+ Fixing testcase to not fail if http://localhost/ exists.
  Reworking code to not rely on existence of server to validate the
  correct behavior of URI port parsing.
This commit is contained in:
Joakim Erdfelt 2011-12-20 13:01:23 -07:00
parent 8ea22a6a4c
commit 2ca897c1ea
2 changed files with 37 additions and 53 deletions

View File

@ -332,6 +332,25 @@ public class WebSocketClient
{
if (!_factory.isStarted())
throw new IllegalStateException("Factory !started");
InetSocketAddress address = toSocketAddress(uri);
SocketChannel channel = SocketChannel.open();
if (_bindAddress != null)
channel.socket().bind(_bindAddress);
channel.socket().setTcpNoDelay(true);
WebSocketFuture holder = new WebSocketFuture(websocket, uri, this, channel);
channel.configureBlocking(false);
channel.connect(address);
_factory.getSelectorManager().register(channel, holder);
return holder;
}
public static final InetSocketAddress toSocketAddress(URI uri)
{
String scheme = uri.getScheme();
if (!("ws".equalsIgnoreCase(scheme) || "wss".equalsIgnoreCase(scheme)))
throw new IllegalArgumentException("Bad WebSocket scheme: " + scheme);
@ -341,20 +360,8 @@ public class WebSocketClient
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(), port);
WebSocketFuture holder = new WebSocketFuture(websocket, uri, this, channel);
channel.configureBlocking(false);
channel.connect(address);
_factory.getSelectorManager().register(channel, holder);
return holder;
return address;
}
/* ------------------------------------------------------------ */
@ -486,6 +493,7 @@ public class WebSocketClient
return _maskGen;
}
@Override
public String toString()
{
return "[" + _uri + ","+_websocket+"]@"+hashCode();

View File

@ -15,12 +15,15 @@
*******************************************************************************/
package org.eclipse.jetty.websocket;
import static org.hamcrest.Matchers.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
@ -43,8 +46,6 @@ 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();
@ -711,44 +712,19 @@ public class WebSocketClientTest
@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);
}
URI uri = new URI("ws://localhost");
InetSocketAddress addr = WebSocketClient.toSocketAddress(uri);
Assert.assertThat("URI (" + uri + ").host", addr.getHostName(), is("localhost"));
Assert.assertThat("URI (" + uri + ").port", addr.getPort(), is(80));
}
@Test
public void testURIWithDefaultWSSPort() throws Exception
{
URI uri = new URI("wss://localhost");
InetSocketAddress addr = WebSocketClient.toSocketAddress(uri);
Assert.assertThat("URI (" + uri + ").host", addr.getHostName(), is("localhost"));
Assert.assertThat("URI (" + uri + ").port", addr.getPort(), is(443));
}
private void respondToClient(Socket connection, String serverResponse) throws IOException