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

Commit d2db3c6

Browse files
committed
Issue-131 har default values
1 parent b840862 commit d2db3c6

File tree

8 files changed

+143
-8
lines changed

8 files changed

+143
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
@JsonIgnoreProperties(ignoreUnknown = true)
1414
public class HarContent {
1515

16-
private Long size;
16+
private Long size = 0L;
1717
private Long compression;
18-
private String mimeType;
18+
private String mimeType = "";
1919
private String text;
2020
private String encoding;
2121
private String comment;

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ public void setCreator(HarCreatorBrowser creator) {
6363
* @return Information about the browser used.
6464
*/
6565
public HarCreatorBrowser getBrowser() {
66-
if (browser == null) {
67-
browser = new HarCreatorBrowser();
68-
}
6966
return browser;
7067
}
7168

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@JsonIgnoreProperties(ignoreUnknown = true)
1616
public class HarPostData {
1717

18-
private String mimeType;
18+
private String mimeType = "";
1919
private List<HarPostDataParam> params = new ArrayList<>();
2020
private String text;
2121
private String comment;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class HarResponse {
2323
private List<HarCookie> cookies;
2424
private List<HarHeader> headers;
2525
private HarContent content;
26-
private String redirectURL;
26+
private String redirectURL = "";
2727
private Long headersSize;
2828
private Long bodySize;
2929
private String comment;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Modifications Copyright (c) 2019 BrowserUp, Inc.
3+
*/
4+
5+
package com.browserup.bup.proxy
6+
7+
import com.browserup.bup.BrowserUpProxy
8+
import com.browserup.bup.BrowserUpProxyServer
9+
import com.browserup.bup.filters.util.HarCaptureUtil
10+
import com.browserup.bup.proxy.dns.AdvancedHostResolver
11+
import com.browserup.bup.proxy.test.util.MockServerTest
12+
import com.browserup.bup.proxy.test.util.NewProxyServerTestUtil
13+
import com.browserup.harreader.model.*
14+
import com.github.tomakehurst.wiremock.client.WireMock
15+
import com.google.common.collect.Iterables
16+
import org.apache.http.client.config.RequestConfig
17+
import org.apache.http.client.methods.CloseableHttpResponse
18+
import org.apache.http.client.methods.HttpGet
19+
import org.junit.After
20+
import org.junit.Test
21+
import org.mockito.invocation.InvocationOnMock
22+
import org.mockito.stubbing.Answer
23+
24+
import java.text.SimpleDateFormat
25+
import java.util.concurrent.TimeUnit
26+
27+
import static com.github.tomakehurst.wiremock.client.WireMock.*
28+
import static org.hamcrest.Matchers.*
29+
import static org.junit.Assert.*
30+
import static org.mockito.Mockito.mock
31+
import static org.mockito.Mockito.when
32+
33+
class HarValidationTest extends MockServerTest {
34+
private BrowserUpProxy proxy
35+
36+
@After
37+
void tearDown() {
38+
if (proxy?.started) {
39+
proxy.abort()
40+
}
41+
}
42+
43+
@Test
44+
void testDefaultValuesOfHarResponse() {
45+
def stubUrl = "/testUrl.*"
46+
stubFor(get(urlMatching(stubUrl)).willReturn(ok()))
47+
48+
proxy = new BrowserUpProxyServer()
49+
proxy.start()
50+
51+
proxy.newHar()
52+
53+
def requestUrl = "http://localhost:${mockServerPort}/testUrl"
54+
55+
NewProxyServerTestUtil.getNewHttpClient(proxy.port).withCloseable {
56+
NewProxyServerTestUtil.toStringAndClose(it.execute(new HttpGet(requestUrl)).getEntity().getContent())
57+
}
58+
59+
Thread.sleep(500)
60+
def har = proxy.getHar()
61+
def entry = proxy.har.log.entries.get(0)
62+
def harResponse = entry.response
63+
64+
assertEquals("Expected redirectURL to have default empty string value", "", harResponse.redirectURL)
65+
assertEquals("Expected content size to have value 0 by default", 0, harResponse.content.size)
66+
assertEquals("Expected empty mime type", "", har.log.entries[0].response.content.mimeType)
67+
assertEquals("Expected empty request post data mime type", "", har.log.entries[0].request.postData.mimeType)
68+
69+
verify(1, getRequestedFor(urlMatching(stubUrl)))
70+
}
71+
72+
73+
}

browserup-proxy-core/src/test/java/com/browserup/harreader/model/HarLogTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void testCreatorNull() {
6363
public void testBrowserNull() {
6464
HarLog log = new HarLog();
6565
log.setBrowser(null);
66-
Assert.assertNotNull(log.getBrowser());
66+
Assert.assertNull(log.getBrowser());
6767
}
6868

6969
@Test
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Modifications Copyright (c) 2019 BrowserUp, Inc.
3+
*/
4+
5+
package com.browserup.bup.proxy.rest
6+
7+
import com.browserup.harreader.model.Har
8+
import com.browserup.harreader.model.HarEntry
9+
import com.fasterxml.jackson.databind.ObjectMapper
10+
import com.github.tomakehurst.wiremock.client.WireMock
11+
import groovyx.net.http.HttpResponseDecorator
12+
import groovyx.net.http.Method
13+
import org.apache.http.entity.ContentType
14+
import org.hamcrest.Matchers
15+
import org.junit.Test
16+
17+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse
18+
import static com.github.tomakehurst.wiremock.client.WireMock.get
19+
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor
20+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor
21+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
22+
import static org.junit.Assert.assertEquals
23+
import static org.junit.Assert.assertNull
24+
import static org.junit.Assert.assertThat
25+
26+
class ValidateHarRestTest extends BaseRestTest {
27+
28+
@Override
29+
String getUrlPath() {
30+
return 'har'
31+
}
32+
33+
@Test
34+
void validateHarForRequestWithEmptyContentAndMimeType() {
35+
def urlToCatch = 'test'
36+
def responseBody = ''
37+
38+
mockTargetServerResponse(urlToCatch, responseBody)
39+
40+
proxyManager.get()[0].newHar()
41+
42+
requestToTargetServer(urlToCatch, responseBody)
43+
44+
proxyRestServerClient.request(Method.GET, ContentType.WILDCARD) { req ->
45+
uri.path = "/proxy/${proxy.port}/${urlPath}"
46+
response.success = { HttpResponseDecorator resp ->
47+
Har har = new ObjectMapper().readValue(resp.entity.content, Har) as Har
48+
assertNull("Expected null browser", har.log.browser)
49+
assertEquals("Expected zero content size for the entry", 0, har.log.entries[0].response.content.size)
50+
assertEquals("Expected empty redirect url", "", har.log.entries[0].response.redirectURL)
51+
assertEquals("Expected empty mime type", "", har.log.entries[0].response.content.mimeType)
52+
assertEquals("Expected empty request post data mime type", "", har.log.entries[0].request.postData.mimeType)
53+
}
54+
}
55+
56+
WireMock.verify(1, getRequestedFor(urlEqualTo("/${urlToCatch}")))
57+
}
58+
59+
protected void mockTargetServerResponse(String url, String responseBody) {
60+
def response = aResponse().withStatus(200)
61+
.withBody(responseBody)
62+
.withHeader('Content-Type', '')
63+
stubFor(get(urlEqualTo("/${url}")).willReturn(response))
64+
}
65+
}

0 commit comments

Comments
 (0)