Skip to content

Commit

Permalink
missing method for parsing bytes to json
Browse files Browse the repository at this point in the history
  • Loading branch information
aludwiko committed Jan 18, 2024
1 parent b7190ff commit e910a36
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import com.fasterxml.jackson.core.JsonProcessingException;

import java.io.IOException;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
Expand Down Expand Up @@ -123,4 +125,15 @@ public static HttpResponse ok(byte[] body) {
public static HttpResponse of(StatusCode.Success statusCode, String contentType, byte[] body) {
return new HttpResponse(statusCode, contentType, body);
}

/**
* Parses an HTTP body to a specified JSON type using Jackson deserializer.
*/
public <T> T bodyAsJson(Class<T> clazz) {
try {
return JsonSupport.parseBytes(body, clazz);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public void shouldReturnJsonStringWithComponentClient() {
assertThat(response.getStatusCode()).isEqualTo(OK);
assertThat(response.getContentType()).contains("application/json");
assertThat(response.getBody()).contains("{\"text\": \"123\"}".getBytes());
assertThat(response.bodyAsJson(Message.class)).isEqualTo(new Message("123"));
}

@Test
Expand Down

0 comments on commit e910a36

Please sign in to comment.