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

Commit 964103a

Browse files
authored
Get har with clean flag for rest api (#238)
1 parent 060c674 commit 964103a

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

browserup-proxy-rest/src/main/java/com/browserup/bup/proxy/bricks/ProxyResource.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,15 @@ public Reply<?> newProxy(Request request) {
137137

138138
@Get
139139
@At("/:port/har")
140-
public Reply<?> getHar(@Named("port") int port) {
140+
public Reply<?> getHar(@Named("port") int port, Request request) {
141141
LOG.info("GET /" + port + "/har");
142142
BrowserUpProxyServer proxy = proxyManager.get(port);
143143
if (proxy == null) {
144144
return Reply.saying().notFound();
145145
}
146146

147-
Har har = proxy.getHar();
147+
Boolean cleanHar = "true".equals(request.param("cleanHar"));
148+
Har har = proxy.getHar(cleanHar);
148149

149150
return Reply.with(har).as(Json.class);
150151
}

browserup-proxy-rest/src/test/groovy/com/browserup/bup/proxy/rest/ValidateHarRestTest.groovy

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import org.junit.Test
1515
import static com.github.tomakehurst.wiremock.client.WireMock.*
1616
import static org.junit.Assert.assertNotNull
1717
import static org.junit.Assert.assertNull
18+
import static org.junit.Assert.assertTrue
1819

1920
class ValidateHarRestTest extends BaseRestTest {
2021

@@ -23,6 +24,90 @@ class ValidateHarRestTest extends BaseRestTest {
2324
return 'har'
2425
}
2526

27+
@Test
28+
void cleanHarFalseTest() {
29+
def urlToCatch = 'test'
30+
def responseBody = ''
31+
32+
mockTargetServerResponse(urlToCatch, responseBody)
33+
34+
proxyManager.get()[0].newHar()
35+
36+
requestToTargetServer(urlToCatch, responseBody)
37+
38+
proxyRestServerClient.request(Method.GET, ContentType.WILDCARD) { req ->
39+
uri.path = "/proxy/${proxy.port}/${urlPath}"
40+
response.success = { HttpResponseDecorator resp ->
41+
Har har = new ObjectMapper().readValue(resp.entity.content, Har) as Har
42+
43+
assertTrue("Expected captured queries in har", har.getLog().getEntries().size() > 0)
44+
}
45+
}
46+
47+
proxyRestServerClient.request(Method.GET, ContentType.WILDCARD) { req ->
48+
uri.path = "/proxy/${proxy.port}/${urlPath}"
49+
uri.query = ['cleanHar': false]
50+
response.success = { HttpResponseDecorator resp ->
51+
Har har = new ObjectMapper().readValue(resp.entity.content, Har) as Har
52+
53+
assertTrue("Expected captured queries in har", har.getLog().getEntries().size() > 0)
54+
}
55+
}
56+
57+
proxyRestServerClient.request(Method.GET, ContentType.WILDCARD) { req ->
58+
uri.path = "/proxy/${proxy.port}/${urlPath}"
59+
response.success = { HttpResponseDecorator resp ->
60+
Har har = new ObjectMapper().readValue(resp.entity.content, Har) as Har
61+
62+
assertTrue("Expected captured queries in har", har.getLog().getEntries().size() > 0)
63+
}
64+
}
65+
66+
verify(1, getRequestedFor(urlEqualTo("/${urlToCatch}")))
67+
}
68+
69+
@Test
70+
void cleanHarTest() {
71+
def urlToCatch = 'test'
72+
def responseBody = ''
73+
74+
mockTargetServerResponse(urlToCatch, responseBody)
75+
76+
proxyManager.get()[0].newHar()
77+
78+
requestToTargetServer(urlToCatch, responseBody)
79+
80+
proxyRestServerClient.request(Method.GET, ContentType.WILDCARD) { req ->
81+
uri.path = "/proxy/${proxy.port}/${urlPath}"
82+
response.success = { HttpResponseDecorator resp ->
83+
Har har = new ObjectMapper().readValue(resp.entity.content, Har) as Har
84+
85+
assertTrue("Expected captured queries in har", har.getLog().getEntries().size() > 0)
86+
}
87+
}
88+
89+
proxyRestServerClient.request(Method.GET, ContentType.WILDCARD) { req ->
90+
uri.path = "/proxy/${proxy.port}/${urlPath}"
91+
uri.query = ['cleanHar': true]
92+
response.success = { HttpResponseDecorator resp ->
93+
Har har = new ObjectMapper().readValue(resp.entity.content, Har) as Har
94+
95+
assertTrue("Expected captured queries in old har", har.getLog().getEntries().size() > 0)
96+
}
97+
}
98+
99+
proxyRestServerClient.request(Method.GET, ContentType.WILDCARD) { req ->
100+
uri.path = "/proxy/${proxy.port}/${urlPath}"
101+
response.success = { HttpResponseDecorator resp ->
102+
Har har = new ObjectMapper().readValue(resp.entity.content, Har) as Har
103+
104+
assertTrue("Expected to get Har without entries", har.getLog().getEntries().size() == 0)
105+
}
106+
}
107+
108+
verify(1, getRequestedFor(urlEqualTo("/${urlToCatch}")))
109+
}
110+
26111
@Test
27112
void validateHarForRequestWithEmptyContentAndMimeType() {
28113
def urlToCatch = 'test'

0 commit comments

Comments
 (0)