Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)))
}


}