1818package org .openqa .selenium .environment .webserver ;
1919
2020import com .google .common .collect .ImmutableMap ;
21+ import org .openqa .selenium .WebDriverException ;
2122import org .openqa .selenium .grid .config .MapConfig ;
23+ import org .openqa .selenium .grid .config .MemoizedConfig ;
2224import org .openqa .selenium .grid .server .BaseServerOptions ;
2325import org .openqa .selenium .grid .server .Server ;
2426import org .openqa .selenium .internal .Require ;
27+ import org .openqa .selenium .io .TemporaryFilesystem ;
2528import org .openqa .selenium .json .Json ;
29+ import org .openqa .selenium .net .NetworkUtils ;
2630import org .openqa .selenium .net .PortProber ;
2731import org .openqa .selenium .netty .server .NettyServer ;
2832import org .openqa .selenium .remote .http .HttpClient ;
2933import org .openqa .selenium .remote .http .HttpHandler ;
3034import org .openqa .selenium .remote .http .HttpMethod ;
3135import org .openqa .selenium .remote .http .HttpRequest ;
3236import org .openqa .selenium .remote .http .HttpResponse ;
33- import org .openqa .selenium .remote .http .Route ;
3437
38+ import java .io .File ;
3539import java .io .IOException ;
3640import java .io .UncheckedIOException ;
3741import java .net .MalformedURLException ;
4347import static java .util .Collections .singletonMap ;
4448import static org .openqa .selenium .remote .http .Contents .bytes ;
4549import static org .openqa .selenium .remote .http .Contents .string ;
46- import static org .openqa .selenium .remote .http .Route .get ;
47- import static org .openqa .selenium .remote .http .Route .matching ;
48- import static org .openqa .selenium .remote .http .Route .post ;
4950
5051public class NettyAppServer implements AppServer {
5152
53+ private static final String ALTERNATIVE_HOSTNAME_FOR_TEST_ENV_NAME = "ALTERNATIVE_HOSTNAME" ;
54+
5255 private final Server <?> server ;
5356
5457 public NettyAppServer () {
55- this (emulateJettyAppServer ());
58+ MemoizedConfig config = new MemoizedConfig (new MapConfig (singletonMap ("server" , singletonMap ("port" , PortProber .findFreePort ()))));
59+ BaseServerOptions options = new BaseServerOptions (config );
60+
61+ File tempDir = TemporaryFilesystem .getDefaultTmpFS ().createTempDir ("generated" , "pages" );
62+
63+ HttpHandler handler = new HandlersForTests (
64+ options .getHostname ().orElse ("localhost" ),
65+ options .getPort (),
66+ tempDir .toPath ());
67+
68+ server = new NettyServer (options , handler );
5669 }
5770
5871 public NettyAppServer (HttpHandler handler ) {
@@ -64,16 +77,6 @@ public NettyAppServer(HttpHandler handler) {
6477 handler );
6578 }
6679
67- private static Route emulateJettyAppServer () {
68- return Route .combine (
69- new CommonWebResources (),
70- get ("/encoding" ).to (EncodingHandler ::new ),
71- matching (req -> req .getUri ().startsWith ("/page/" )).to (PageHandler ::new ),
72- get ("/redirect" ).to (RedirectHandler ::new ),
73- get ("/sleep" ).to (SleepingHandler ::new ),
74- post ("/upload" ).to (UploadHandler ::new ));
75- }
76-
7780 @ Override
7881 public void start () {
7982 server .start ();
@@ -152,7 +155,17 @@ public String getHostName() {
152155
153156 @ Override
154157 public String getAlternateHostName () {
155- throw new UnsupportedOperationException ("getAlternateHostName" );
158+ String alternativeHostnameFromProperty = System .getenv (ALTERNATIVE_HOSTNAME_FOR_TEST_ENV_NAME );
159+ if (alternativeHostnameFromProperty != null ) {
160+ return alternativeHostnameFromProperty ;
161+ }
162+
163+ NetworkUtils networkUtils = new NetworkUtils ();
164+ try {
165+ return networkUtils .getNonLoopbackAddressOfThisMachine ();
166+ } catch (WebDriverException e ) {
167+ return networkUtils .getPrivateLocalAddress ();
168+ }
156169 }
157170
158171 public static void main (String [] args ) {
0 commit comments