Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit 79b8b80

Browse files
authored
optional Har reset on getHar() (#234)
1 parent e557b21 commit 79b8b80

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

browserup-proxy-core/src/main/java/com/browserup/bup/BrowserUpProxy.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,17 @@ public interface BrowserUpProxy {
117117
*
118118
* @return current HAR, or null if HAR capture is not enabled
119119
*/
120-
Har getHar();
120+
default Har getHar() {
121+
return getHar(false);
122+
}
123+
124+
/**
125+
* If cleanHar is false - returns current HAR.
126+
* If cleanHar is true - cleans current HAR and returns HAR with data it has before cleaning.
127+
*
128+
* @return current HAR, or null if HAR capture is not enabled
129+
*/
130+
Har getHar(boolean cleanHar);
121131

122132
/**
123133
* Starts a new HAR file with the default page name (see {@link #newPage()}. Enables HAR capture if it was not previously enabled.

browserup-proxy-core/src/main/java/com/browserup/bup/BrowserUpProxyServer.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
public class BrowserUpProxyServer implements BrowserUpProxy {
125125
private static final Logger log = LoggerFactory.getLogger(BrowserUpProxyServer.class);
126126
private static final Object LOCK = new Object();
127+
private static final Object GET_HAR_LOCK = new Object();
127128

128129
public static final String DEFAULT_PAGE_REF = "Default";
129130
public static final String DEFAULT_PAGE_TITLE = "Default";
@@ -542,7 +543,20 @@ public InetAddress getServerBindAddress() {
542543

543544
@Override
544545
public Har getHar() {
545-
return this.har;
546+
synchronized (GET_HAR_LOCK) {
547+
return this.har;
548+
}
549+
}
550+
551+
@Override
552+
public Har getHar(boolean cleanHar) {
553+
if (!cleanHar) {
554+
return this.har;
555+
}
556+
557+
synchronized (GET_HAR_LOCK) {
558+
return this.newHar();
559+
}
546560
}
547561

548562
@Override
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Modifications Copyright (c) 2019 BrowserUp, Inc.
3+
*/
4+
5+
package com.browserup.bup.proxy
6+
7+
import com.browserup.bup.BrowserUpProxy
8+
import com.browserup.bup.BrowserUpProxyServer
9+
import com.browserup.bup.proxy.test.util.MockServerTest
10+
import com.browserup.bup.proxy.test.util.NewProxyServerTestUtil
11+
import com.browserup.harreader.model.Har
12+
import org.apache.http.client.methods.HttpGet
13+
import org.junit.After
14+
import org.junit.Test
15+
16+
import static com.github.tomakehurst.wiremock.client.WireMock.*
17+
import static org.hamcrest.Matchers.empty
18+
import static org.hamcrest.Matchers.not
19+
import static org.junit.Assert.assertEquals
20+
import static org.junit.Assert.assertThat
21+
22+
class GetHarTest extends MockServerTest {
23+
private BrowserUpProxy proxy
24+
25+
@After
26+
void tearDown() {
27+
if (proxy?.started) {
28+
proxy.abort()
29+
}
30+
}
31+
32+
@Test
33+
void testGetHarClean() {
34+
def stubUrl = "/testCaptureResponseCookiesInHar"
35+
stubFor(get(urlEqualTo(stubUrl))
36+
.willReturn(ok()
37+
.withBody("success"))
38+
)
39+
40+
proxy = new BrowserUpProxyServer()
41+
proxy.setHarCaptureTypes([CaptureType.RESPONSE_COOKIES] as Set)
42+
proxy.setTrustAllServers(true)
43+
proxy.start()
44+
45+
proxy.newHar()
46+
47+
NewProxyServerTestUtil.getNewHttpClient(proxy.port).withCloseable {
48+
String responseBody = NewProxyServerTestUtil.toStringAndClose(it.execute(new HttpGet("https://localhost:${mockServerHttpsPort}/testCaptureResponseCookiesInHar")).getEntity().getContent())
49+
assertEquals("Did not receive expected response from mock server", "success", responseBody)
50+
}
51+
52+
Thread.sleep(500)
53+
Har har = proxy.getHar()
54+
55+
assertThat("Expected to find entries in the HAR", har.getLog().getEntries(), not(empty()))
56+
har = proxy.getHar(true)
57+
assertThat("Expected to find entries in the HAR", har.getLog().getEntries(), not(empty()))
58+
59+
har = proxy.getHar()
60+
assertThat("Expected to find no entries in the HAR", har.getLog().getEntries(), empty())
61+
62+
verify(1, getRequestedFor(urlEqualTo(stubUrl)))
63+
}
64+
65+
66+
}

0 commit comments

Comments
 (0)