1717
1818import com .hierynomus .sshj .test .HttpServer ;
1919import com .hierynomus .sshj .test .SshServerExtension ;
20- import com .hierynomus .sshj .test .util .FileUtil ;
2120import net .schmizz .sshj .SSHClient ;
2221import net .schmizz .sshj .connection .ConnectionException ;
2322import net .schmizz .sshj .connection .channel .forwarded .RemotePortForwarder ;
2726import org .junit .jupiter .api .Test ;
2827import org .junit .jupiter .api .extension .RegisterExtension ;
2928
30- import java .io .File ;
3129import java .io .IOException ;
3230import java .net .HttpURLConnection ;
3331import java .net .InetSocketAddress ;
32+ import java .net .URI ;
3433import java .net .URL ;
35- import java .nio .file .Files ;
3634
3735import static org .junit .jupiter .api .Assertions .assertEquals ;
3836
3937public class RemotePortForwarderTest {
4038 private static final PortRange RANGE = new PortRange (9000 , 9999 );
4139 private static final String LOCALHOST = "127.0.0.1" ;
42- private static final String LOCALHOST_URL_FORMAT = "http://127.0.0.1:%d" ;
43- private static final InetSocketAddress HTTP_SERVER_SOCKET_ADDR = new InetSocketAddress (LOCALHOST , 8080 );
40+ private static final String URL_FORMAT = "http://%s:%d" ;
4441
4542 @ RegisterExtension
4643 public SshServerExtension fixture = new SshServerExtension ();
@@ -49,21 +46,21 @@ public class RemotePortForwarderTest {
4946 public HttpServer httpServer = new HttpServer ();
5047
5148 @ BeforeEach
52- public void setUp () throws IOException {
49+ public void setUp () {
5350 fixture .getServer ().setForwardingFilter (new AcceptAllForwardingFilter ());
54- File file = Files .createFile (httpServer .getDocRoot ().toPath ().resolve ("index.html" )).toFile ();
55- FileUtil .writeToFile (file , "<html><head/><body><h1>Hi!</h1></body></html>" );
5651 }
5752
5853 @ Test
5954 public void shouldHaveWorkingHttpServer () throws IOException {
60- assertEquals (200 , httpGet (8080 ));
55+ final URI serverUrl = httpServer .getServerUrl ();
56+
57+ assertEquals (HttpURLConnection .HTTP_NOT_FOUND , httpGet (serverUrl .getHost (), serverUrl .getPort ()));
6158 }
6259
6360 @ Test
6461 public void shouldDynamicallyForwardPortForLocalhost () throws IOException {
6562 SSHClient sshClient = getFixtureClient ();
66- RemotePortForwarder .Forward bind = forwardPort (sshClient , "127.0.0.1" , new SinglePort (0 ));
63+ RemotePortForwarder .Forward bind = forwardPort (sshClient , LOCALHOST , new SinglePort (0 ));
6764 assertHttpGetSuccess (bind );
6865 }
6966
@@ -84,7 +81,7 @@ public void shouldDynamicallyForwardPortForAllProtocols() throws IOException {
8481 @ Test
8582 public void shouldForwardPortForLocalhost () throws IOException {
8683 SSHClient sshClient = getFixtureClient ();
87- RemotePortForwarder .Forward bind = forwardPort (sshClient , "127.0.0.1" , RANGE );
84+ RemotePortForwarder .Forward bind = forwardPort (sshClient , LOCALHOST , RANGE );
8885 assertHttpGetSuccess (bind );
8986 }
9087
@@ -103,17 +100,22 @@ public void shouldForwardPortForAllProtocols() throws IOException {
103100 }
104101
105102 private void assertHttpGetSuccess (final RemotePortForwarder .Forward bind ) throws IOException {
106- assertEquals (200 , httpGet (bind .getPort ()));
103+ final String bindAddress = bind .getAddress ();
104+ final String address = bindAddress .isEmpty () ? LOCALHOST : bindAddress ;
105+ final int port = bind .getPort ();
106+ assertEquals (HttpURLConnection .HTTP_NOT_FOUND , httpGet (address , port ));
107107 }
108108
109109 private RemotePortForwarder .Forward forwardPort (SSHClient sshClient , String address , PortRange portRange ) throws IOException {
110110 while (true ) {
111+ final URI serverUrl = httpServer .getServerUrl ();
112+ final InetSocketAddress serverAddress = new InetSocketAddress (serverUrl .getHost (), serverUrl .getPort ());
111113 try {
112114 return sshClient .getRemotePortForwarder ().bind (
113115 // where the server should listen
114116 new RemotePortForwarder .Forward (address , portRange .nextPort ()),
115117 // what we do with incoming connections that are forwarded to us
116- new SocketForwardingConnectListener (HTTP_SERVER_SOCKET_ADDR ));
118+ new SocketForwardingConnectListener (serverAddress ));
117119 } catch (ConnectionException ce ) {
118120 if (!portRange .hasNext ()) {
119121 throw ce ;
@@ -122,8 +124,8 @@ private RemotePortForwarder.Forward forwardPort(SSHClient sshClient, String addr
122124 }
123125 }
124126
125- private int httpGet (final int port ) throws IOException {
126- final URL url = new URL (String .format (LOCALHOST_URL_FORMAT , port ));
127+ private int httpGet (final String address , final int port ) throws IOException {
128+ final URL url = new URL (String .format (URL_FORMAT , address , port ));
127129 final HttpURLConnection urlConnection = (HttpURLConnection ) url .openConnection ();
128130 urlConnection .setConnectTimeout (3000 );
129131 urlConnection .setRequestMethod ("GET" );
0 commit comments