From 3377275aebcc5ad31197c988b756a4ebbae5e07f Mon Sep 17 00:00:00 2001 From: "kirill.turutin" Date: Fri, 28 Feb 2020 15:09:22 +0300 Subject: [PATCH] optional Har reset on getHar() --- .../com/browserup/bup/BrowserUpProxy.java | 12 +++- .../browserup/bup/BrowserUpProxyServer.java | 16 ++++- .../com/browserup/bup/proxy/GetHarTest.groovy | 66 +++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 browserup-proxy-core/src/test/groovy/com/browserup/bup/proxy/GetHarTest.groovy diff --git a/browserup-proxy-core/src/main/java/com/browserup/bup/BrowserUpProxy.java b/browserup-proxy-core/src/main/java/com/browserup/bup/BrowserUpProxy.java index 464ee7354..6d3c11e52 100644 --- a/browserup-proxy-core/src/main/java/com/browserup/bup/BrowserUpProxy.java +++ b/browserup-proxy-core/src/main/java/com/browserup/bup/BrowserUpProxy.java @@ -117,7 +117,17 @@ public interface BrowserUpProxy { * * @return current HAR, or null if HAR capture is not enabled */ - Har getHar(); + default Har getHar() { + return getHar(false); + } + + /** + * If cleanHar is false - returns current HAR. + * If cleanHar is true - cleans current HAR and returns HAR with data it has before cleaning. + * + * @return current HAR, or null if HAR capture is not enabled + */ + Har getHar(boolean cleanHar); /** * Starts a new HAR file with the default page name (see {@link #newPage()}. Enables HAR capture if it was not previously enabled. diff --git a/browserup-proxy-core/src/main/java/com/browserup/bup/BrowserUpProxyServer.java b/browserup-proxy-core/src/main/java/com/browserup/bup/BrowserUpProxyServer.java index c08e116a0..a467fccba 100644 --- a/browserup-proxy-core/src/main/java/com/browserup/bup/BrowserUpProxyServer.java +++ b/browserup-proxy-core/src/main/java/com/browserup/bup/BrowserUpProxyServer.java @@ -124,6 +124,7 @@ public class BrowserUpProxyServer implements BrowserUpProxy { private static final Logger log = LoggerFactory.getLogger(BrowserUpProxyServer.class); private static final Object LOCK = new Object(); + private static final Object GET_HAR_LOCK = new Object(); public static final String DEFAULT_PAGE_REF = "Default"; public static final String DEFAULT_PAGE_TITLE = "Default"; @@ -542,7 +543,20 @@ public InetAddress getServerBindAddress() { @Override public Har getHar() { - return this.har; + synchronized (GET_HAR_LOCK) { + return this.har; + } + } + + @Override + public Har getHar(boolean cleanHar) { + if (!cleanHar) { + return this.har; + } + + synchronized (GET_HAR_LOCK) { + return this.newHar(); + } } @Override diff --git a/browserup-proxy-core/src/test/groovy/com/browserup/bup/proxy/GetHarTest.groovy b/browserup-proxy-core/src/test/groovy/com/browserup/bup/proxy/GetHarTest.groovy new file mode 100644 index 000000000..fa6b1283a --- /dev/null +++ b/browserup-proxy-core/src/test/groovy/com/browserup/bup/proxy/GetHarTest.groovy @@ -0,0 +1,66 @@ +/* + * Modifications Copyright (c) 2019 BrowserUp, Inc. + */ + +package com.browserup.bup.proxy + +import com.browserup.bup.BrowserUpProxy +import com.browserup.bup.BrowserUpProxyServer +import com.browserup.bup.proxy.test.util.MockServerTest +import com.browserup.bup.proxy.test.util.NewProxyServerTestUtil +import com.browserup.harreader.model.Har +import org.apache.http.client.methods.HttpGet +import org.junit.After +import org.junit.Test + +import static com.github.tomakehurst.wiremock.client.WireMock.* +import static org.hamcrest.Matchers.empty +import static org.hamcrest.Matchers.not +import static org.junit.Assert.assertEquals +import static org.junit.Assert.assertThat + +class GetHarTest extends MockServerTest { + private BrowserUpProxy proxy + + @After + void tearDown() { + if (proxy?.started) { + proxy.abort() + } + } + + @Test + void testGetHarClean() { + def stubUrl = "/testCaptureResponseCookiesInHar" + stubFor(get(urlEqualTo(stubUrl)) + .willReturn(ok() + .withBody("success")) + ) + + proxy = new BrowserUpProxyServer() + proxy.setHarCaptureTypes([CaptureType.RESPONSE_COOKIES] as Set) + proxy.setTrustAllServers(true) + proxy.start() + + proxy.newHar() + + NewProxyServerTestUtil.getNewHttpClient(proxy.port).withCloseable { + String responseBody = NewProxyServerTestUtil.toStringAndClose(it.execute(new HttpGet("https://localhost:${mockServerHttpsPort}/testCaptureResponseCookiesInHar")).getEntity().getContent()) + assertEquals("Did not receive expected response from mock server", "success", responseBody) + } + + Thread.sleep(500) + Har har = proxy.getHar() + + assertThat("Expected to find entries in the HAR", har.getLog().getEntries(), not(empty())) + har = proxy.getHar(true) + assertThat("Expected to find entries in the HAR", har.getLog().getEntries(), not(empty())) + + har = proxy.getHar() + assertThat("Expected to find no entries in the HAR", har.getLog().getEntries(), empty()) + + verify(1, getRequestedFor(urlEqualTo(stubUrl))) + } + + +}