Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/spe-266 #63

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
target
out
17 changes: 10 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
<description>Contains HAR models and writers</description>
<packaging>jar</packaging>

<url>https://github.com/SmartBear/har-java</url>
<url>https://github.com/speakeasy-api/har-java</url>

<developers>
<developer>
<name>Alexa Drake</name>
<id>alexadrake</id>
</developer>
<developer>
<name>Shadid Chowdhury</name>
<id>shadidchowdhury</id>
Expand All @@ -28,16 +32,15 @@
</developers>

<scm>
<url>https://github.com/SmartBear/har-java</url>
<connection>scm:git:ssh://[email protected]/SmartBear/har-java.git</connection>
<developerConnection>scm:git:ssh://[email protected]/SmartBear/har-java.git
</developerConnection>
<tag>master</tag>
<url>https://github.com/speakeasy-api/har-java</url>
<connection>scm:git:https://github.com/speakeasy-api/har-java</connection>
<developerConnection>scm:git:https://github.com/speakeasy-api/har-java.git</developerConnection>
<tag>main</tag>
</scm>

<issueManagement>
<system>github</system>
<url>https://github.com/SmartBear/har-java/issues</url>
<url>https://github.com/speakeasy-api/har-java/issues</url>
</issueManagement>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class DefaultHarStreamWriter implements HarStreamWriter {

private final JsonGenerator jsonGenerator;

private DefaultHarStreamWriter(File harFile, String version, HarCreator creator, HarBrowser browser, List<HarPage> pages, String comment, boolean usePrettyPrint) throws IOException {
jsonGenerator = new JsonFactory().createGenerator(harFile, JsonEncoding.UTF8);
private DefaultHarStreamWriter(OutputStream outputStream, String version, HarCreator creator, HarBrowser browser, List<HarPage> pages, String comment, boolean usePrettyPrint) throws IOException {
jsonGenerator = new JsonFactory().createGenerator(outputStream, JsonEncoding.UTF8);
final ObjectMapper objectMapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
jsonGenerator.setCodec(objectMapper);
if (usePrettyPrint) {
Expand Down Expand Up @@ -70,7 +71,7 @@ public void closeHar() throws IOException {
}

public static class Builder {
private File harFile;
private OutputStream outputStream;
private String version = "1.2";
private HarCreator creator;
private HarBrowser browser;
Expand Down Expand Up @@ -111,8 +112,8 @@ public Builder withComment(String comment) {
return this;
}

public Builder withOutputFile(File harFile) {
this.harFile = harFile;
public Builder withOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
return this;
}

Expand All @@ -124,7 +125,7 @@ public Builder withUsePrettyPrint(boolean usePrettyPrint) {

public DefaultHarStreamWriter build() throws IOException {

return new DefaultHarStreamWriter(harFile, version, creator, browser, pages, comment, usePrettyPrint);
return new DefaultHarStreamWriter(outputStream, version, creator, browser, pages, comment, usePrettyPrint);
}
}
}
17 changes: 11 additions & 6 deletions src/main/java/com/smartbear/har/model/HarContent.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.smartbear.har.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

/**
* This object describes details about response content (embedded in response object).
* This object describes details about response content (embedded in response
* object).
*
* @see <a href="http://www.softwareishard.com/blog/har-12-spec/#content">specification</a>
* @see <a href=
* "http://www.softwareishard.com/blog/har-12-spec/#content">specification</a>
*/
@JsonPropertyOrder({
"size",
Expand All @@ -26,8 +30,9 @@ public class HarContent {

@JsonCreator
public HarContent(@JsonProperty("size") Long size, @JsonProperty("compression") Long compression,
@JsonProperty("mimeType") String mimeType, @JsonProperty("text") String text,
@JsonProperty("comment") String comment) {
@JsonProperty("mimeType") String mimeType,
@JsonProperty("text") @JsonInclude(Include.NON_EMPTY) String text,
@JsonProperty("comment") String comment) {
this.size = size;
this.compression = compression;
this.mimeType = mimeType;
Expand Down Expand Up @@ -57,7 +62,7 @@ public Long getSize() {

@Override
public String toString() {
return "HarContent [text = " + text + ", comment = " + comment + ", compression = " + compression + ", mimeType = " + mimeType + ", size = " + size + "]";
return "HarContent [text = " + text + ", comment = " + comment + ", compression = " + compression
+ ", mimeType = " + mimeType + ", size = " + size + "]";
}
}

18 changes: 12 additions & 6 deletions src/main/java/com/smartbear/har/model/HarCookie.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.smartbear.har.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

/**
* Cookie used in request and response objects.
*
* @see <a href="http://www.softwareishard.com/blog/har-12-spec/#cookies">specification</a>
* @see <a href=
* "http://www.softwareishard.com/blog/har-12-spec/#cookies">specification</a>
*/
@JsonPropertyOrder({
"name",
Expand All @@ -19,6 +22,8 @@
"secure",
"comment"
})

@JsonInclude(Include.NON_DEFAULT)
public class HarCookie {

private String name;
Expand All @@ -32,9 +37,9 @@ public class HarCookie {

@JsonCreator
public HarCookie(@JsonProperty("name") String name, @JsonProperty("value") String value,
@JsonProperty("path") String path, @JsonProperty("domain") String domain,
@JsonProperty("expires") String expires, @JsonProperty("httpOnly") boolean httpOnly,
@JsonProperty("secure") boolean secure, @JsonProperty("comment") String comment) {
@JsonProperty("path") String path, @JsonProperty("domain") String domain,
@JsonProperty("expires") String expires, @JsonProperty("httpOnly") boolean httpOnly,
@JsonProperty("secure") boolean secure, @JsonProperty("comment") String comment) {
this.name = name;
this.value = value;
this.path = path;
Expand Down Expand Up @@ -79,7 +84,8 @@ public String getComment() {

@Override
public String toString() {
return "HarCookie [expires = " + expires + ", name = " + name + ", secure = " + secure + ", domain = " + domain + ", path = " + path + ", value = " + value + ", httpOnly = " + httpOnly + ", comment = " + comment + "]";
return "HarCookie [expires = " + expires + ", name = " + name + ", secure = " + secure + ", domain = " + domain
+ ", path = " + path + ", value = " + value + ", httpOnly = " + httpOnly + ", comment = " + comment
+ "]";
}
}

11 changes: 7 additions & 4 deletions src/main/java/com/smartbear/har/model/HarCreator.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.smartbear.har.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;


/**
* Information about the creator of HAR
*
* @see <a href="http://www.softwareishard.com/blog/har-12-spec/#creator">specification</a>
* @see <a href=
* "http://www.softwareishard.com/blog/har-12-spec/#creator">specification</a>
*/
@JsonPropertyOrder({
"name",
Expand All @@ -22,8 +24,9 @@ public class HarCreator {
private String comment;

@JsonCreator
public HarCreator(@JsonProperty("name") String name, @JsonProperty("comment") String comment,
@JsonProperty("version") String version) {
public HarCreator(@JsonProperty("name") String name,
@JsonProperty("comment") @JsonInclude(Include.NON_EMPTY) String comment,
@JsonProperty("version") String version) {
this.name = name;
this.comment = comment;
this.version = version;
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/com/smartbear/har/model/HarEntry.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.smartbear.har.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

/**
* This object represents an array with all exported HTTP requests.
*
* @see <a href="http://www.softwareishard.com/blog/har-12-spec/#entries">specification</a>
* @see <a href=
* "http://www.softwareishard.com/blog/har-12-spec/#entries">specification</a>
*/
@JsonPropertyOrder({
"pageref",
Expand Down Expand Up @@ -36,10 +39,12 @@ public class HarEntry {

@JsonCreator
public HarEntry(@JsonProperty("pageref") String pageref, @JsonProperty("startedDateTime") String startedDateTime,
@JsonProperty("time") long time, @JsonProperty("request") HarRequest request,
@JsonProperty("response") HarResponse response, @JsonProperty("cache") HarCache cache,
@JsonProperty("timings") HarTimings timings, @JsonProperty("serverIPAddress") String serverIPAddress,
@JsonProperty("connection") String connection, @JsonProperty("comment") String comment) {
@JsonProperty("time") long time, @JsonProperty("request") HarRequest request,
@JsonProperty("response") HarResponse response,
@JsonProperty("cache") @JsonInclude(Include.ALWAYS) HarCache cache,
@JsonProperty("timings") @JsonInclude(Include.ALWAYS) HarTimings timings,
@JsonProperty("serverIPAddress") String serverIPAddress,
@JsonProperty("connection") String connection, @JsonProperty("comment") String comment) {
this.pageref = pageref;
this.startedDateTime = startedDateTime;
this.time = time;
Expand Down Expand Up @@ -94,6 +99,8 @@ public String getComment() {

@Override
public String toString() {
return "HarEntry [response = " + response + ", connection = " + connection + ", time = " + time + ", pageref = " + pageref + ", cache = " + cache + ", timings = " + timings + ", request = " + request + ", comment = " + comment + ", serverIPAddress = " + serverIPAddress + ", startedDateTime = " + startedDateTime + "]";
return "HarEntry [response = " + response + ", connection = " + connection + ", time = " + time + ", pageref = "
+ pageref + ", cache = " + cache + ", timings = " + timings + ", request = " + request + ", comment = "
+ comment + ", serverIPAddress = " + serverIPAddress + ", startedDateTime = " + startedDateTime + "]";
}
}
13 changes: 9 additions & 4 deletions src/main/java/com/smartbear/har/model/HarQueryString.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.smartbear.har.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

/**
* This object contains list of all parameters and values parsed from a query string, if any (embedded in request object).
* @see <a href="http://www.softwareishard.com/blog/har-12-spec/#queryString">specification</a>
*/
* This object contains list of all parameters and values parsed from a query
* string, if any (embedded in request object).
*
* @see <a href=
* "http://www.softwareishard.com/blog/har-12-spec/#queryString">specification</a>
*/
@JsonPropertyOrder({
"name",
"value",
Expand All @@ -20,7 +25,7 @@ public class HarQueryString {

@JsonCreator
public HarQueryString(@JsonProperty("name") String name, @JsonProperty("value") String value,
@JsonProperty("comment") String comment) {
@JsonProperty("comment") @JsonInclude(Include.NON_DEFAULT) String comment) {
this.name = name;
this.value = value;
this.comment = comment;
Expand Down
27 changes: 16 additions & 11 deletions src/main/java/com/smartbear/har/model/HarResponse.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.smartbear.har.model;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.util.List;

/**
* This object contains detailed info about the response.
*
* @see <a href="http://www.softwareishard.com/blog/har-12-spec/#response">specification</a>
* @see <a href=
* "http://www.softwareishard.com/blog/har-12-spec/#response">specification</a>
*/
@JsonPropertyOrder({
"status",
Expand Down Expand Up @@ -38,17 +41,18 @@ public class HarResponse {

@JsonCreator
public HarResponse(@JsonProperty("status") int status, @JsonProperty("statusText") String statusText,
@JsonProperty("httpVersion") String httpVersion, @JsonProperty("cookies") List<HarCookie> cookies,
@JsonProperty("headers") List<HarHeader> headers, @JsonProperty("content") HarContent content,
@JsonProperty("redirectURL") String redirectURL, @JsonProperty("headersSize") Long headersSize,
@JsonProperty("bodySize") Long bodySize, @JsonProperty("comment") String comment) {
@JsonProperty("httpVersion") String httpVersion, @JsonProperty("cookies") List<HarCookie> cookies,
@JsonProperty("headers") List<HarHeader> headers, @JsonProperty("content") HarContent content,
@JsonProperty("redirectURL") @JsonInclude(Include.ALWAYS) String redirectURL,
@JsonProperty("headersSize") Long headersSize,
@JsonProperty("bodySize") Long bodySize, @JsonProperty("comment") String comment) {
this.status = status;
this.statusText = statusText;
this.httpVersion = httpVersion;
this.cookies = cookies;
this.headers = headers;
this.content = content;
this.redirectURL = redirectURL;
this.redirectURL = redirectURL == null ? "" : redirectURL;
this.headersSize = headersSize;
this.bodySize = bodySize;
this.comment = comment;
Expand Down Expand Up @@ -96,8 +100,9 @@ public Long getHeadersSize() {

@Override
public String toString() {
return "HarResponse [content = " + content + ", headers = " + headers + ", bodySize = " + bodySize + ", httpVersion = " + httpVersion + ", status = " + status + ", redirectURL = " + redirectURL + ", statusText = " + statusText + ", comment = " + comment + ", cookies = " + cookies + ", headersSize = " + headersSize + "]";
return "HarResponse [content = " + content + ", headers = " + headers + ", bodySize = " + bodySize
+ ", httpVersion = " + httpVersion + ", status = " + status + ", redirectURL = " + redirectURL
+ ", statusText = " + statusText + ", comment = " + comment + ", cookies = " + cookies
+ ", headersSize = " + headersSize + "]";
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
Expand All @@ -17,16 +19,19 @@ public class DefaultHarStreamWriterTest {

private File tempHarFile;
private HarStreamWriter harBuilder;
private OutputStream outputStream;

@Before
public void setUp() throws Exception {
tempHarFile = File.createTempFile("virt", ".har");
harBuilder = new DefaultHarStreamWriter.Builder().withOutputFile(tempHarFile).withComment("Test Har").build();
outputStream = new FileOutputStream(tempHarFile);
harBuilder = new DefaultHarStreamWriter.Builder().withOutputStream(outputStream).withComment("Test Har").build();
}

@After
public void tearDown() throws Exception {
tempHarFile.deleteOnExit();
outputStream.close();
}

@Test
Expand Down