From eb340b9788304d4f664837b4e46a4764d233cb0b Mon Sep 17 00:00:00 2001 From: "kirill.turutin" Date: Mon, 2 Mar 2020 16:18:45 +0300 Subject: [PATCH] Get har with clean flag for rest api --- .../bup/proxy/bricks/ProxyResource.java | 5 +- .../bup/proxy/rest/ValidateHarRestTest.groovy | 85 +++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/browserup-proxy-rest/src/main/java/com/browserup/bup/proxy/bricks/ProxyResource.java b/browserup-proxy-rest/src/main/java/com/browserup/bup/proxy/bricks/ProxyResource.java index 61aefb5d1..fb3ca9f74 100644 --- a/browserup-proxy-rest/src/main/java/com/browserup/bup/proxy/bricks/ProxyResource.java +++ b/browserup-proxy-rest/src/main/java/com/browserup/bup/proxy/bricks/ProxyResource.java @@ -137,14 +137,15 @@ public Reply newProxy(Request request) { @Get @At("/:port/har") - public Reply getHar(@Named("port") int port) { + public Reply getHar(@Named("port") int port, Request request) { LOG.info("GET /" + port + "/har"); BrowserUpProxyServer proxy = proxyManager.get(port); if (proxy == null) { return Reply.saying().notFound(); } - Har har = proxy.getHar(); + Boolean cleanHar = "true".equals(request.param("cleanHar")); + Har har = proxy.getHar(cleanHar); return Reply.with(har).as(Json.class); } diff --git a/browserup-proxy-rest/src/test/groovy/com/browserup/bup/proxy/rest/ValidateHarRestTest.groovy b/browserup-proxy-rest/src/test/groovy/com/browserup/bup/proxy/rest/ValidateHarRestTest.groovy index 1a3301459..965ec93ac 100644 --- a/browserup-proxy-rest/src/test/groovy/com/browserup/bup/proxy/rest/ValidateHarRestTest.groovy +++ b/browserup-proxy-rest/src/test/groovy/com/browserup/bup/proxy/rest/ValidateHarRestTest.groovy @@ -15,6 +15,7 @@ import org.junit.Test import static com.github.tomakehurst.wiremock.client.WireMock.* import static org.junit.Assert.assertNotNull import static org.junit.Assert.assertNull +import static org.junit.Assert.assertTrue class ValidateHarRestTest extends BaseRestTest { @@ -23,6 +24,90 @@ class ValidateHarRestTest extends BaseRestTest { return 'har' } + @Test + void cleanHarFalseTest() { + def urlToCatch = 'test' + def responseBody = '' + + mockTargetServerResponse(urlToCatch, responseBody) + + proxyManager.get()[0].newHar() + + requestToTargetServer(urlToCatch, responseBody) + + proxyRestServerClient.request(Method.GET, ContentType.WILDCARD) { req -> + uri.path = "/proxy/${proxy.port}/${urlPath}" + response.success = { HttpResponseDecorator resp -> + Har har = new ObjectMapper().readValue(resp.entity.content, Har) as Har + + assertTrue("Expected captured queries in har", har.getLog().getEntries().size() > 0) + } + } + + proxyRestServerClient.request(Method.GET, ContentType.WILDCARD) { req -> + uri.path = "/proxy/${proxy.port}/${urlPath}" + uri.query = ['cleanHar': false] + response.success = { HttpResponseDecorator resp -> + Har har = new ObjectMapper().readValue(resp.entity.content, Har) as Har + + assertTrue("Expected captured queries in har", har.getLog().getEntries().size() > 0) + } + } + + proxyRestServerClient.request(Method.GET, ContentType.WILDCARD) { req -> + uri.path = "/proxy/${proxy.port}/${urlPath}" + response.success = { HttpResponseDecorator resp -> + Har har = new ObjectMapper().readValue(resp.entity.content, Har) as Har + + assertTrue("Expected captured queries in har", har.getLog().getEntries().size() > 0) + } + } + + verify(1, getRequestedFor(urlEqualTo("/${urlToCatch}"))) + } + + @Test + void cleanHarTest() { + def urlToCatch = 'test' + def responseBody = '' + + mockTargetServerResponse(urlToCatch, responseBody) + + proxyManager.get()[0].newHar() + + requestToTargetServer(urlToCatch, responseBody) + + proxyRestServerClient.request(Method.GET, ContentType.WILDCARD) { req -> + uri.path = "/proxy/${proxy.port}/${urlPath}" + response.success = { HttpResponseDecorator resp -> + Har har = new ObjectMapper().readValue(resp.entity.content, Har) as Har + + assertTrue("Expected captured queries in har", har.getLog().getEntries().size() > 0) + } + } + + proxyRestServerClient.request(Method.GET, ContentType.WILDCARD) { req -> + uri.path = "/proxy/${proxy.port}/${urlPath}" + uri.query = ['cleanHar': true] + response.success = { HttpResponseDecorator resp -> + Har har = new ObjectMapper().readValue(resp.entity.content, Har) as Har + + assertTrue("Expected captured queries in old har", har.getLog().getEntries().size() > 0) + } + } + + proxyRestServerClient.request(Method.GET, ContentType.WILDCARD) { req -> + uri.path = "/proxy/${proxy.port}/${urlPath}" + response.success = { HttpResponseDecorator resp -> + Har har = new ObjectMapper().readValue(resp.entity.content, Har) as Har + + assertTrue("Expected to get Har without entries", har.getLog().getEntries().size() == 0) + } + } + + verify(1, getRequestedFor(urlEqualTo("/${urlToCatch}"))) + } + @Test void validateHarForRequestWithEmptyContentAndMimeType() { def urlToCatch = 'test'