Added request body logging for OkHttp requests
This commit is contained in:
parent
0fe3380c4a
commit
1a36670ba6
|
@ -26,8 +26,10 @@ import ca.uhn.fhir.rest.client.api.IHttpResponse;
|
||||||
import ca.uhn.fhir.util.StopWatch;
|
import ca.uhn.fhir.util.StopWatch;
|
||||||
import okhttp3.Call;
|
import okhttp3.Call;
|
||||||
import okhttp3.Call.Factory;
|
import okhttp3.Call.Factory;
|
||||||
|
import okhttp3.MediaType;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.RequestBody;
|
import okhttp3.RequestBody;
|
||||||
|
import okio.Buffer;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -80,10 +82,17 @@ public class OkHttpRestfulRequest extends BaseHttpRequest implements IHttpReques
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getRequestBodyFromStream() {
|
public String getRequestBodyFromStream() throws IOException {
|
||||||
// returning null to indicate this is not supported, as documented in IHttpRequest's contract
|
if (myRequestBody == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
final Buffer buffer = new Buffer();
|
||||||
|
myRequestBody.writeTo(buffer);
|
||||||
|
MediaType contentType = myRequestBody.contentType();
|
||||||
|
return contentType == null || contentType.charset() == null
|
||||||
|
? buffer.readUtf8()
|
||||||
|
: buffer.readString(contentType.charset());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUri() {
|
public String getUri() {
|
||||||
|
|
|
@ -1,12 +1,19 @@
|
||||||
package ca.uhn.fhir.okhttp.client;
|
package ca.uhn.fhir.okhttp.client;
|
||||||
|
|
||||||
import ca.uhn.fhir.rest.api.RequestTypeEnum;
|
import ca.uhn.fhir.rest.api.RequestTypeEnum;
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
public class OkHttpRestfulRequestTest {
|
public class OkHttpRestfulRequestTest {
|
||||||
|
|
||||||
|
private final String ENTITY_CONTENT = "Some entity with special characters: é";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void toString_afterCreation_GetUsefulDataForLogging() {
|
void toString_afterCreation_GetUsefulDataForLogging() {
|
||||||
String theUrl = "https://example.com/fhir/meta";
|
String theUrl = "https://example.com/fhir/meta";
|
||||||
|
@ -24,4 +31,22 @@ public class OkHttpRestfulRequestTest {
|
||||||
OkHttpRestfulRequest okHttpRestfulRequest = new OkHttpRestfulRequest(clientFactory.getNativeClient(), theUrl, RequestTypeEnum.POST, null);
|
OkHttpRestfulRequest okHttpRestfulRequest = new OkHttpRestfulRequest(clientFactory.getNativeClient(), theUrl, RequestTypeEnum.POST, null);
|
||||||
assertEquals("POST https://another.example.com/fhir/Task", okHttpRestfulRequest.toString());
|
assertEquals("POST https://another.example.com/fhir/Task", okHttpRestfulRequest.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRequestBodyFromStream() throws IOException {
|
||||||
|
RequestBody requestBody = RequestBody.create(ENTITY_CONTENT.getBytes(StandardCharsets.ISO_8859_1), MediaType.parse("text/plain; charset=ISO-8859-1"));
|
||||||
|
|
||||||
|
String result = new OkHttpRestfulRequest(null, "https://test.com", null, requestBody).getRequestBodyFromStream();
|
||||||
|
|
||||||
|
assertEquals(ENTITY_CONTENT, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRequestBodyFromStreamWithUnknownCharset() throws IOException {
|
||||||
|
RequestBody requestBody = RequestBody.create(ENTITY_CONTENT.getBytes(StandardCharsets.UTF_8), MediaType.parse("text/plain; charset=UTF-8"));
|
||||||
|
|
||||||
|
String result = new OkHttpRestfulRequest(null, "https://test.com", null, requestBody).getRequestBodyFromStream();
|
||||||
|
|
||||||
|
assertEquals(ENTITY_CONTENT, result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue