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 1 commit
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 @@ -13,9 +13,9 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class HarContent {

private Long size;
private Long size = 0L;
private Long compression;
private String mimeType;
private String mimeType = "";
private String text;
private String encoding;
private String comment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ public void setCreator(HarCreatorBrowser creator) {
* @return Information about the browser used.
*/
public HarCreatorBrowser getBrowser() {
if (browser == null) {
browser = new HarCreatorBrowser();
}
return browser;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class HarPostData {

private String mimeType;
private String mimeType = "";
private List<HarPostDataParam> params = new ArrayList<>();
private String text;
private String comment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class HarResponse {
private List<HarCookie> cookies;
private List<HarHeader> headers;
private HarContent content;
private String redirectURL;
private String redirectURL = "";
private Long headersSize;
private Long bodySize;
private String comment;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.filters.util.HarCaptureUtil
import com.browserup.bup.proxy.dns.AdvancedHostResolver
import com.browserup.bup.proxy.test.util.MockServerTest
import com.browserup.bup.proxy.test.util.NewProxyServerTestUtil
import com.browserup.harreader.model.*
import com.github.tomakehurst.wiremock.client.WireMock
import com.google.common.collect.Iterables
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpGet
import org.junit.After
import org.junit.Test
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Answer

import java.text.SimpleDateFormat
import java.util.concurrent.TimeUnit

import static com.github.tomakehurst.wiremock.client.WireMock.*
import static org.hamcrest.Matchers.*
import static org.junit.Assert.*
import static org.mockito.Mockito.mock
import static org.mockito.Mockito.when

class HarValidationTest extends MockServerTest {
private BrowserUpProxy proxy

@After
void tearDown() {
if (proxy?.started) {
proxy.abort()
}
}

@Test
void testDefaultValuesOfHarResponse() {
def stubUrl = "/testUrl.*"
stubFor(get(urlMatching(stubUrl)).willReturn(ok()))

proxy = new BrowserUpProxyServer()
proxy.start()

proxy.newHar()

def requestUrl = "http://localhost:${mockServerPort}/testUrl"

NewProxyServerTestUtil.getNewHttpClient(proxy.port).withCloseable {
NewProxyServerTestUtil.toStringAndClose(it.execute(new HttpGet(requestUrl)).getEntity().getContent())
}

Thread.sleep(500)
def har = proxy.getHar()
def entry = proxy.har.log.entries.get(0)
def harResponse = entry.response

assertEquals("Expected redirectURL to have default empty string value", "", harResponse.redirectURL)
assertEquals("Expected content size to have value 0 by default", 0, harResponse.content.size)
assertEquals("Expected empty mime type", "", har.log.entries[0].response.content.mimeType)
assertEquals("Expected empty request post data mime type", "", har.log.entries[0].request.postData.mimeType)

verify(1, getRequestedFor(urlMatching(stubUrl)))
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void testCreatorNull() {
public void testBrowserNull() {
HarLog log = new HarLog();
log.setBrowser(null);
Assert.assertNotNull(log.getBrowser());
Assert.assertNull(log.getBrowser());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Modifications Copyright (c) 2019 BrowserUp, Inc.
*/

package com.browserup.bup.proxy.rest

import com.browserup.harreader.model.Har
import com.browserup.harreader.model.HarEntry
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.tomakehurst.wiremock.client.WireMock
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.Method
import org.apache.http.entity.ContentType
import org.hamcrest.Matchers
import org.junit.Test

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse
import static com.github.tomakehurst.wiremock.client.WireMock.get
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertNull
import static org.junit.Assert.assertThat

class ValidateHarRestTest extends BaseRestTest {

@Override
String getUrlPath() {
return 'har'
}

@Test
void validateHarForRequestWithEmptyContentAndMimeType() {
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
assertNull("Expected null browser", har.log.browser)
assertEquals("Expected zero content size for the entry", 0, har.log.entries[0].response.content.size)
assertEquals("Expected empty redirect url", "", har.log.entries[0].response.redirectURL)
assertEquals("Expected empty mime type", "", har.log.entries[0].response.content.mimeType)
assertEquals("Expected empty request post data mime type", "", har.log.entries[0].request.postData.mimeType)
}
}

WireMock.verify(1, getRequestedFor(urlEqualTo("/${urlToCatch}")))
}

protected void mockTargetServerResponse(String url, String responseBody) {
def response = aResponse().withStatus(200)
.withBody(responseBody)
.withHeader('Content-Type', '')
stubFor(get(urlEqualTo("/${url}")).willReturn(response))
}
}