Merge pull request #674 from hapifhir/otasek-dstu3-request-logging

Add dstu3 request logging
This commit is contained in:
dotasek 2021-11-24 15:35:51 -05:00 committed by GitHub
commit 3de30b04bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 133 additions and 4 deletions

View File

@ -0,0 +1,2 @@
Other changes:
* Fix logging of requests for dstu3

View File

@ -206,14 +206,19 @@ public class FhirRequestBuilder {
public <T extends Resource> ResourceRequest<T> execute() throws IOException {
formatHeaders(httpRequest, resourceFormat, headers);
Response response = getHttpClient().newCall(httpRequest.build()).execute();
final Request request = httpRequest.build();
log(request.method(), request.url().toString(), request.headers(), request.body() != null ? request.body().toString().getBytes() : null);
Response response = getHttpClient().newCall(request).execute();
T resource = unmarshalReference(response, resourceFormat);
return new ResourceRequest<T>(resource, response.code(), getLocationHeader(response.headers()));
}
public Bundle executeAsBatch() throws IOException {
formatHeaders(httpRequest, resourceFormat, null);
Response response = getHttpClient().newCall(httpRequest.build()).execute();
final Request request = httpRequest.build();
log(request.method(), request.url().toString(), request.headers(), request.body() != null ? request.body().toString().getBytes() : null);
Response response = getHttpClient().newCall(request).execute();
return unmarshalFeed(response, resourceFormat);
}
@ -302,6 +307,26 @@ public class FhirRequestBuilder {
}
}
/**
* Logs the given {@link Request}, using the current {@link ToolingClientLogger}. If the current
* {@link FhirRequestBuilder#logger} is null, no action is taken.
*
* @param method HTTP request method
* @param url request URL
* @param requestHeaders {@link Headers} for request
* @param requestBody Byte array request
*/
protected void log(String method, String url, Headers requestHeaders, byte[] requestBody) {
if (logger != null) {
List<String> headerList = new ArrayList<>(Collections.emptyList());
Map<String, List<String>> headerMap = requestHeaders.toMultimap();
headerMap.keySet().forEach(key -> headerMap.get(key).forEach(value -> headerList.add(key + ":" + value)));
logger.logRequest(method, url, headerList, requestBody);
}
}
/**
* Logs the given {@link Response}, using the current {@link ToolingClientLogger}. If the current
* {@link FhirRequestBuilder#logger} is null, no action is taken.

View File

@ -0,0 +1,101 @@
package org.hl7.fhir.dstu3.utils.client.network;
import okhttp3.*;
import org.hl7.fhir.dstu3.formats.IParser;
import org.hl7.fhir.utilities.ToolingClientLogger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.AdditionalMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.ArgumentMatchers;
import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
@ExtendWith(MockitoExtension.class)
public class FhirRequestBuilderTests {
private static final String DUMMY_URL = "https://some-url.com/";
Request mockRequest = new Request.Builder()
.url(DUMMY_URL)
.build();
final String RESPONSE_BODY_STRING = "{}";
Response response = new Response.Builder()
.request(mockRequest)
.protocol(Protocol.HTTP_2)
.code(200) // status code
.message("")
.body(ResponseBody.create(RESPONSE_BODY_STRING,
MediaType.get("application/json; charset=utf-8")
))
.addHeader("Content-Type", "")
.build();
final Request.Builder requestBuilder = new Request.Builder()
.url(DUMMY_URL);
final FhirRequestBuilder fhirRequestBuilder = Mockito.spy(new FhirRequestBuilder(requestBuilder));
@Mock
OkHttpClient client;
@Mock
Call mockCall;
@Mock
ToolingClientLogger logger;
@BeforeEach
public void beforeEach() {
Mockito.doReturn(client).when(fhirRequestBuilder).getHttpClient();
fhirRequestBuilder.withLogger(logger);
}
@Nested
class RequestLoggingTests {
@BeforeEach
public void beforeEach() throws IOException {
Mockito.doReturn(response).when(mockCall).execute();
Mockito.doReturn(mockCall).when(client).newCall(ArgumentMatchers.any());
Mockito.doReturn(null).when(fhirRequestBuilder).unmarshalReference(ArgumentMatchers.any(), ArgumentMatchers.isNull());
}
@Test
public void testExecuteLogging() throws IOException {
fhirRequestBuilder.execute();
Mockito.verify(logger).logRequest(ArgumentMatchers.eq("GET"), ArgumentMatchers.eq(DUMMY_URL), ArgumentMatchers.anyList(), ArgumentMatchers.isNull());
}
@Test
public void testExecuteBatchLogging() throws IOException {
fhirRequestBuilder.executeAsBatch();
Mockito.verify(logger).logRequest(ArgumentMatchers.eq("GET"), ArgumentMatchers.eq(DUMMY_URL), ArgumentMatchers.anyList(), ArgumentMatchers.isNull());
}
}
@Test
public void testUnmarshallReferenceLogging() {
IParser parser = Mockito.mock(IParser.class);
Mockito.doReturn(parser).when(fhirRequestBuilder).getParser(ArgumentMatchers.eq("json"));
fhirRequestBuilder.unmarshalReference(response, "json");
Mockito.verify(logger).logResponse(ArgumentMatchers.eq("200"), ArgumentMatchers.anyList(), AdditionalMatchers.aryEq(RESPONSE_BODY_STRING.getBytes()));
}
@Test
public void testUnmarshallFeedLogging() {
fhirRequestBuilder.unmarshalFeed(response, "application/json");
Mockito.verify(logger).logResponse(ArgumentMatchers.eq("200"), ArgumentMatchers.anyList(), AdditionalMatchers.aryEq(RESPONSE_BODY_STRING.getBytes()));
}
}

View File

@ -46,7 +46,7 @@ public class HTMLClientLogger extends BaseLogger implements ToolingClientLogger
private boolean req = false;
private PrintStream file;
public HTMLClientLogger(String log) {
if (log != null) {
try {
@ -89,12 +89,13 @@ public class HTMLClientLogger extends BaseLogger implements ToolingClientLogger
if (DEBUG) {
System.out.println(" txlog resp: " +outcome+" "+present(body));
}
req = false;
if (file == null)
return;
if (!req) {
System.out.println("Record Response without request");
}
req = false;
file.println("<pre>");
file.println(outcome);
for (String s : headers)