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

Commit a8dc2ce

Browse files
committed
Added _url field to har entry
1 parent 8891404 commit a8dc2ce

File tree

6 files changed

+79
-13
lines changed

6 files changed

+79
-13
lines changed

browserup-proxy-core/src/main/java/com/browserup/harreader/model/HarEntry.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package com.browserup.harreader.model;
22

3-
import com.fasterxml.jackson.annotation.JsonAnyGetter;
4-
import com.fasterxml.jackson.annotation.JsonAnySetter;
5-
import com.fasterxml.jackson.annotation.JsonFormat;
6-
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
7-
import com.fasterxml.jackson.annotation.JsonInclude;
3+
import com.fasterxml.jackson.annotation.*;
84

95
import java.util.Date;
106
import java.util.HashMap;
@@ -29,6 +25,7 @@ public class HarEntry {
2925
private String serverIPAddress;
3026
private String connection;
3127
private String comment;
28+
private String _url;
3229
private Map<String, Object> additional = new HashMap<>();
3330

3431
/**
@@ -121,6 +118,19 @@ public void setTimings(HarTiming timings) {
121118
this.timings = timings;
122119
}
123120

121+
@JsonProperty("_url")
122+
public String getUrl() {
123+
if (_url == null) {
124+
_url = "";
125+
}
126+
return _url;
127+
}
128+
129+
@JsonProperty("_url")
130+
public void setUrl(String url) {
131+
this._url = url;
132+
}
133+
124134
/**
125135
* @return Server IP address (result of DNS resolution), null if not present.
126136
*/

browserup-proxy-core/src/main/resources/mitmproxy/har_dump.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def generate_har_entry_response_for_failure(self):
336336
result['_errorMessage'] = "No response received"
337337
return result
338338

339-
def generate_har_entry(self):
339+
def generate_har_entry(self, request_url):
340340
return {
341341
"pageref": "",
342342
"startedDateTime": "",
@@ -347,7 +347,8 @@ def generate_har_entry(self):
347347
"timings": self.generate_har_timings(),
348348
"serverIPAddress": "",
349349
"connection": "",
350-
"comment": ""
350+
"comment": "",
351+
"_url": request_url
351352
}
352353

353354
def get_or_create_har(self, page_ref, page_title, create_page=False):

browserup-proxy-core/src/main/resources/mitmproxy/http_connect_capture.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def proxy_to_server_connection_succeeded(self, f):
127127

128128
def init_har_entry(self, flow):
129129
ctx.log.debug("Initializing har entry for flow request: {}".format(str(flow.request)))
130-
setattr(flow.request, 'har_entry', self.har_dump_addon.generate_har_entry())
130+
setattr(flow.request, 'har_entry', self.har_dump_addon.generate_har_entry(flow.request.url))
131131
self.har_dump_addon.append_har_entry(flow.request.har_entry)
132132

133133
def error(self, flow):

browserup-proxy-core/src/main/resources/mitmproxy/init_flow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def request(self, flow):
5454

5555
def init_har_entry(self, flow):
5656
ctx.log.debug("Initializing har entry for flow request: {}".format(str(flow.request)))
57-
setattr(flow.request, 'har_entry', self.har_dump_addon.generate_har_entry())
57+
setattr(flow.request, 'har_entry', self.har_dump_addon.generate_har_entry(flow.request.url))
5858
self.har_dump_addon.append_har_entry(flow.request.har_entry)
5959

6060
addons = [

browserup-proxy-core/src/test/groovy/com/browserup/bup/mitmproxy/HarValidationTest.groovy

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import com.browserup.bup.MitmProxyServer
88
import com.browserup.bup.proxy.test.util.MockServerTest
99
import com.browserup.bup.proxy.test.util.NewProxyServerTestUtil
1010
import com.browserup.harreader.model.*
11+
import org.apache.commons.lang3.StringUtils
1112
import org.apache.http.client.methods.HttpGet
1213
import org.junit.After
1314
import org.junit.Test
1415

1516
import static com.github.tomakehurst.wiremock.client.WireMock.*
1617
import static org.junit.Assert.assertNotNull
18+
import static org.junit.Assert.assertTrue
1719

1820
class HarValidationTest extends MockServerTest {
1921
private MitmProxyServer proxy
@@ -25,6 +27,34 @@ class HarValidationTest extends MockServerTest {
2527
}
2628
}
2729

30+
@Test
31+
void testHarEntryContainsUrlField() {
32+
def stubUrl = "/testUrl.*"
33+
stubFor(get(urlMatching(stubUrl)).willReturn(ok()))
34+
35+
proxy = new MitmProxyServer()
36+
proxy.start()
37+
38+
proxy.newHar()
39+
40+
def requestUrl = "http://localhost:${mockServerPort}/testUrl"
41+
42+
NewProxyServerTestUtil.getNewHttpClient(proxy.port).withCloseable {
43+
NewProxyServerTestUtil.toStringAndClose(it.execute(new HttpGet(requestUrl)).getEntity().getContent())
44+
}
45+
46+
Thread.sleep(500)
47+
def har = proxy.getHar()
48+
49+
assertNotNull("Expected not null log creator name", har.log.creator.name)
50+
assertNotNull("Expected not null log creator version", har.log.creator.version)
51+
52+
har.log.entries.each {
53+
assertTrue(StringUtils.isNotEmpty(it.url))
54+
}
55+
verify(1, getRequestedFor(urlMatching(stubUrl)))
56+
}
57+
2858
@Test
2959
void testDefaultValuesOfMockedHarResponse() {
3060
def stubUrl = "/testUrl.*"

browserup-proxy-core/src/test/groovy/com/browserup/bup/mitmproxy/NewHarTest.groovy

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.browserup.bup.proxy.test.util.NewProxyServerTestUtil
1313
import com.browserup.harreader.model.*
1414
import com.github.tomakehurst.wiremock.client.WireMock
1515
import com.google.common.collect.Iterables
16+
import org.apache.commons.lang3.StringUtils
1617
import org.apache.http.client.config.RequestConfig
1718
import org.apache.http.client.methods.CloseableHttpResponse
1819
import org.apache.http.client.methods.HttpGet
@@ -390,6 +391,9 @@ class NewHarTest extends MockServerTest {
390391

391392
assertEquals("Expected to not capture body content in HAR", "", content.text)
392393

394+
assertTrue("Expected HAR entries to have _url field",
395+
har.log.entries.every { StringUtils.isNotEmpty(it.url) })
396+
393397
verify(1, getRequestedFor(urlEqualTo(stubUrl)))
394398
}
395399

@@ -467,6 +471,9 @@ class NewHarTest extends MockServerTest {
467471
assertNotNull("Expected to find HAR content", newContent)
468472

469473
assertEquals("Expected to capture body content in HAR", "success", newContent.text)
474+
475+
assertTrue("Expected HAR entries to have _url field",
476+
populatedHar.log.entries.every { StringUtils.isNotEmpty(it.url) })
470477
}
471478
}
472479

@@ -517,6 +524,8 @@ class NewHarTest extends MockServerTest {
517524

518525
assertEquals("Expected HAR returned from newPage() not to contain second page", 1, harWithFirstPageOnly.log.pages.size())
519526
assertEquals("Expected id of HAR page to be 'first-page'", "first-page", harWithFirstPageOnly.log.pages.first().id)
527+
assertTrue("Expected HAR entries to have _url field",
528+
har.log.entries.every { StringUtils.isNotEmpty(it.url) })
520529
}
521530

522531
@Test
@@ -546,7 +555,8 @@ class NewHarTest extends MockServerTest {
546555

547556
String capturedUrl = har.log.entries[0].request.url
548557
assertEquals("URL captured in HAR did not match request URL", requestUrl, capturedUrl)
549-
558+
assertTrue("Expected HAR entries to have _url field",
559+
har.log.entries.every { StringUtils.isNotEmpty(it.url) })
550560
verify(1, getRequestedFor(urlEqualTo(stubUrl)))
551561
}
552562

@@ -582,7 +592,8 @@ class NewHarTest extends MockServerTest {
582592

583593
assertEquals("Expected first query parameter name to be param1", "param1", har.log.entries[0].request.queryString[0].name)
584594
assertEquals("Expected first query parameter value to be value1", "value1", har.log.entries[0].request.queryString[0].value)
585-
595+
assertTrue("Expected HAR entries to have _url field",
596+
har.log.entries.every { StringUtils.isNotEmpty(it.url) })
586597
verify(1, getRequestedFor(urlMatching(stubUrl)))
587598
}
588599

@@ -620,7 +631,8 @@ class NewHarTest extends MockServerTest {
620631

621632
assertEquals("Expected first query parameter name to be param1", "param1", har.log.entries[0].request.queryString[0].name)
622633
assertEquals("Expected first query parameter value to be value1", "value1", har.log.entries[0].request.queryString[0].value)
623-
634+
assertTrue("Expected HAR entries to have _url field",
635+
har.log.entries.every { StringUtils.isNotEmpty(it.url) })
624636
verify(1, getRequestedFor(urlMatching(stubUrl)))
625637
}
626638

@@ -662,7 +674,8 @@ class NewHarTest extends MockServerTest {
662674

663675
assertEquals("Expected first query parameter name to be param1", "param1", har.log.entries[0].request.queryString[0].name)
664676
assertEquals("Expected first query parameter value to be value1", "value1", har.log.entries[0].request.queryString[0].value)
665-
677+
assertTrue("Expected HAR entries to have _url field",
678+
har.log.entries.every { StringUtils.isNotEmpty(it.url) })
666679
verify(1, getRequestedFor(urlMatching(stubUrl)))
667680
}
668681

@@ -711,6 +724,8 @@ class NewHarTest extends MockServerTest {
711724
assertEquals("Expected HAR timings to contain default values after DNS failure", 0L, harTimings.getSend(TimeUnit.NANOSECONDS))
712725
assertEquals("Expected HAR timings to contain default values after DNS failure", 0L, harTimings.getWait(TimeUnit.NANOSECONDS))
713726
assertEquals("Expected HAR timings to contain default values after DNS failure", 0L, harTimings.getReceive(TimeUnit.NANOSECONDS))
727+
assertTrue("Expected HAR entries to have _url field",
728+
har.log.entries.every { StringUtils.isNotEmpty(it.url) })
714729
assertNotNull(har.log.entries[0].time)
715730
}
716731

@@ -760,6 +775,8 @@ class NewHarTest extends MockServerTest {
760775
assertEquals("Expected HAR timings to contain default values after DNS failure", 0L, harTimings.getSend(TimeUnit.NANOSECONDS))
761776
assertEquals("Expected HAR timings to contain default values after DNS failure", 0L, harTimings.getWait(TimeUnit.NANOSECONDS))
762777
assertEquals("Expected HAR timings to contain default values after DNS failure", 0L, harTimings.getReceive(TimeUnit.NANOSECONDS))
778+
assertTrue("Expected HAR entries to have _url field",
779+
har.log.entries.every { StringUtils.isNotEmpty(it.url) })
763780
assertNotNull(har.log.entries[0].time)
764781
}
765782

@@ -808,6 +825,8 @@ class NewHarTest extends MockServerTest {
808825
assertEquals("Expected HAR timings to contain default values after connection failure", 0L, harTimings.getSend(TimeUnit.NANOSECONDS))
809826
assertEquals("Expected HAR timings to contain default values after connection failure", 0L, harTimings.getWait(TimeUnit.NANOSECONDS))
810827
assertEquals("Expected HAR timings to contain default values after connection failure", 0L, harTimings.getReceive(TimeUnit.NANOSECONDS))
828+
assertTrue("Expected HAR entries to have _url field",
829+
har.log.entries.every { StringUtils.isNotEmpty(it.url) })
811830
assertNotNull(har.log.entries[0].time)
812831
}
813832

@@ -856,6 +875,8 @@ class NewHarTest extends MockServerTest {
856875
assertEquals("Expected HAR timings to contain default values after connection failure", 0L, harTimings.getSend(TimeUnit.NANOSECONDS))
857876
assertEquals("Expected HAR timings to contain default values after connection failure", 0L, harTimings.getWait(TimeUnit.NANOSECONDS))
858877
assertEquals("Expected HAR timings to contain default values after connection failure", 0L, harTimings.getReceive(TimeUnit.NANOSECONDS))
878+
assertTrue("Expected HAR entries to have _url field",
879+
har.log.entries.every { StringUtils.isNotEmpty(it.url) })
859880
assertTrue(har.log.entries[0].time > 0)
860881
}
861882

@@ -914,6 +935,8 @@ class NewHarTest extends MockServerTest {
914935
assertThat("Expected wait time to be populated", harTimings.getWait(TimeUnit.NANOSECONDS), greaterThan(0L))
915936

916937
assertEquals("Expected receive time to not be populated", 0L, harTimings.getReceive(TimeUnit.NANOSECONDS))
938+
assertTrue("Expected HAR entries to have _url field",
939+
har.log.entries.every { StringUtils.isNotEmpty(it.url) })
917940
assertTrue(har.log.entries[0].time > 0)
918941
}
919942

@@ -973,6 +996,8 @@ class NewHarTest extends MockServerTest {
973996
assertThat("Expected wait time to be populated", harTimings.getWait(TimeUnit.NANOSECONDS), greaterThan(0L))
974997

975998
assertEquals("Expected receive time to not be populated", 0L, harTimings.getReceive(TimeUnit.NANOSECONDS))
999+
assertTrue("Expected HAR entries to have _url field",
1000+
har.log.entries.every { StringUtils.isNotEmpty(it.url) })
9761001
assertTrue(har.log.entries[0].time > 0)
9771002
}
9781003

0 commit comments

Comments
 (0)