Skip to content

Commit

Permalink
Improved documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sdstoehr committed Dec 22, 2024
1 parent 8191c21 commit 43eb8bc
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 8 deletions.
70 changes: 62 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ Read [HTTP Archives](http://www.softwareishard.com/blog/har-12-spec/) with Java.

## Usage

Reading HAR from File:
### Reading HAR

#### Reading HAR from File:

```java
HarReader harReader = new HarReader();
Har har = harReader.readFromFile(new File("myhar.har"));
System.out.println(har.getLog().getCreator().getName());
System.out.println(har.log().creator().name());
```

Reading HAR from String:
#### Reading HAR from String:

```java
HarReader harReader = new HarReader();
Expand All @@ -43,15 +45,17 @@ Har har = harReader.readFromString("{ ... HAR-JSON-Data ... }", HarReaderMode.LA

You can also follow the next section and configure your own mapping configuration to deal with these fields.

Writing HAR to File:
### Writing HAR

#### Writing HAR to File:

```java
Har har = new Har();
HarWriter harWriter = new HarWriter();
harWriter.writeTo(new File("myhar.har"), har);
```

Writing HAR to OutputStream:
#### Writing HAR to OutputStream:

```java
Har har = new Har();
Expand All @@ -60,7 +64,7 @@ ByteArrayOutputStream baos = new ByteArrayOutputStream();
harWriter.writeTo(baos, har);
```

Writing HAR to Writer:
#### Writing HAR to Writer:

```java
Har har = new Har();
Expand All @@ -69,14 +73,62 @@ StringWriter sw = new StringWriter();
harWriter.writeTo(sw, har);
```

Writing HAR as bytes:
#### Writing HAR as bytes:

```java
Har har = new Har();
HarWriter harWriter = new HarWriter();
byte[] harBytes = harWriter.writeAsBytes(har);
```

#### Manually creating HAR data structures:

The model objects can be created by using the provided builder API:

```java
Har har = Har.builder()
.log(HarLog.builder()
.creator(HarCreatorBrowser.builder()
.name("HAR reader")
.version("1.0")
.build())
.entry(HarEntry.builder()
.pageref("page_0")
.startedDateTime(ZonedDateTime.parse("2021-01-01T00:00:00Z"))
.time(42)
.request(HarRequest.builder()
.method(HttpMethod.GET.name())
.url("https://www.example.com")
.httpVersion("HTTP/1.1")
.build())
.response(HarResponse.builder()
.status(200)
.statusText("OK")
.httpVersion("HTTP/1.1")
.build())
.build())
.build()
).build();
```

The builders allow to add single entries to lists or multiple entries at once, e.g.:

```java
harLogBuilder.page(HarPage page); // add a single page
harLogBuilder.pages(List<HarPage> pages); // add multiple pages (it is NOT replacing previously added pages!)
harLogBuilder.clearPages(); // clear previously added pages
```

To update an existing object, you can use `.toBuilder()` to obtain a prefilled builder:

```java
Har updatedHar = har.toBuilder()
.log(har.log().toBuilder()
.comment("Updated comment")
.build())
.build();
```

### Customizing HAR reader

As of version 2.0.0 you can create your own `MapperFactory` [(DefaultMapperFactory)](src/main/java/de/sstoehr/harreader/jackson/DefaultMapperFactory.java)
Expand Down Expand Up @@ -142,7 +194,8 @@ HarReader harReader = new HarReader(new MyMapperFactory());

[Details](https://github.com/sdstoehr/har-reader/releases/tag/har-reader-2.3.0)


<details>
<summary>Older releases</summary>
### 2.2.1 - 2022-05-26

* Updated dependencies
Expand Down Expand Up @@ -249,3 +302,4 @@ response.getAdditional().get("_transferSize");
* HAR reader threw exceptions, when required fields were empty. This behaviour was changed, so that you can now read non-standard-compliant HAR files

[Details](https://github.com/sdstoehr/har-reader/releases/tag/har-reader-2.0.0)
</details>
38 changes: 38 additions & 0 deletions src/test/java/de/sstoehr/harreader/HarWriterTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import de.sstoehr.harreader.jackson.DefaultMapperFactory;
import de.sstoehr.harreader.model.HarCreatorBrowser;
import de.sstoehr.harreader.model.HarEntry;
import de.sstoehr.harreader.model.HarLog;
import de.sstoehr.harreader.model.HarRequest;
import de.sstoehr.harreader.model.HarResponse;
import de.sstoehr.harreader.model.HttpMethod;
import org.junit.jupiter.api.Test;

import de.sstoehr.harreader.model.Har;
Expand Down Expand Up @@ -72,4 +79,35 @@ void testWriteToWriter() throws IOException {
assertEquals(MAPPER.writeValueAsString(expected), sw.toString());
}

@Test
void testWriteFromBuilder() throws IOException {
Har har = Har.builder()
.log(HarLog.builder()
.creator(HarCreatorBrowser.builder()
.name("HAR reader")
.version("1.0")
.build())
.entry(HarEntry.builder()
.pageref("page_0")
.startedDateTime(ZonedDateTime.parse("2021-01-01T00:00:00Z"))
.time(42)
.request(HarRequest.builder()
.method(HttpMethod.GET.name())
.url("https://www.example.com")
.httpVersion("HTTP/1.1")
.build())
.response(HarResponse.builder()
.status(200)
.statusText("OK")
.httpVersion("HTTP/1.1")
.build())
.build())
.build()
).build();

HarWriter writer = new HarWriter();
assertEquals("{\"log\":{\"version\":\"1.1\",\"creator\":{\"name\":\"HAR reader\",\"version\":\"1.0\"},\"pages\":[],\"entries\":[{\"pageref\":\"page_0\",\"startedDateTime\":\"2021-01-01T00:00:00Z\",\"time\":42,\"request\":{\"method\":\"GET\",\"url\":\"https://www.example.com\",\"httpVersion\":\"HTTP/1.1\",\"cookies\":[],\"headers\":[],\"queryString\":[],\"postData\":{\"params\":[]},\"headersSize\":-1,\"bodySize\":-1},\"response\":{\"status\":200,\"statusText\":\"OK\",\"httpVersion\":\"HTTP/1.1\",\"cookies\":[],\"headers\":[],\"content\":{},\"headersSize\":-1,\"bodySize\":-1},\"cache\":{},\"timings\":{\"blocked\":-1,\"dns\":-1,\"connect\":-1,\"ssl\":-1}}]}}",
writer.writeAsString(har));
}

}

0 comments on commit 43eb8bc

Please sign in to comment.