Skip to content

Commit 4c365be

Browse files
author
Alexander Andryashin
committed
Dynamic port allocation
1 parent cc90cfa commit 4c365be

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@
257257
<artifactId>selenium-api</artifactId>
258258
<version>2.0b3</version>
259259
</dependency>
260+
261+
<dependency>
262+
<groupId>junit</groupId>
263+
<artifactId>junit</artifactId>
264+
<version>4.9</version>
265+
<scope>test</scope>
266+
</dependency>
267+
260268
</dependencies>
261269

262270
<reporting>

src/main/java/org/browsermob/proxy/ProxyServer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.browsermob.core.har.HarPage;
99
import org.browsermob.proxy.http.BrowserMobHttpClient;
1010
import org.browsermob.proxy.jetty.http.HttpContext;
11+
import org.browsermob.proxy.jetty.http.HttpListener;
1112
import org.browsermob.proxy.jetty.http.SocketListener;
1213
import org.browsermob.proxy.jetty.jetty.Server;
1314
import org.browsermob.proxy.jetty.util.InetAddrPort;
@@ -40,7 +41,8 @@ public void start() throws Exception {
4041
}
4142

4243
server = new Server();
43-
server.addListener(new SocketListener(new InetAddrPort(getPort()))); // todo: arg?
44+
HttpListener listener = new SocketListener(new InetAddrPort(getPort()));
45+
server.addListener(listener);
4446
HttpContext context = new HttpContext();
4547
context.setContextPath("/");
4648
server.addContext(context);
@@ -56,6 +58,8 @@ public void start() throws Exception {
5658
context.addHandler(handler);
5759

5860
server.start();
61+
62+
setPort(listener.getPort());
5963
}
6064

6165
public org.openqa.selenium.Proxy seleniumProxy() {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.browsermob.proxy;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.junit.rules.ExternalResource;
8+
9+
import static org.hamcrest.CoreMatchers.equalTo;
10+
import static org.hamcrest.CoreMatchers.not;
11+
import static org.junit.Assert.assertThat;
12+
13+
public class ProxyServerTest {
14+
15+
private ProxyServer server = new ProxyServer(0);
16+
17+
@Before
18+
public void startServer() throws Exception {
19+
server.start();
20+
}
21+
22+
@After
23+
public void stopServer() throws Exception {
24+
server.stop();
25+
}
26+
27+
@Test
28+
public void portAllocation() throws Exception {
29+
assertThat(server.getPort(), not(equalTo(0)));
30+
}
31+
}

0 commit comments

Comments
 (0)