Skip to content

Commit

Permalink
Handle OkHttp POST with empty body
Browse files Browse the repository at this point in the history
  • Loading branch information
sashirestela committed Jan 24, 2025
1 parent 2509bb2 commit bd859e6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private FunctionsByCategory getFunctions(ReturnType returnType) {
}

private Request convertToOkHttpRequest(RequestData request) {
var requestBody = createRequestBody(request.getBody(), request.getContentType());
var requestBody = createRequestBody(request.getBody(), request.getContentType(), request.getHttpMethod());
var headersArray = request.getHeaders().toArray(new String[0]);
var requestBuilder = new Request.Builder()
.url(request.getUrl())
Expand All @@ -152,10 +152,13 @@ private Request convertToOkHttpRequest(RequestData request) {
}

@SuppressWarnings("unchecked")
private RequestBody createRequestBody(Object bodyObject, ContentType contentType) {
private RequestBody createRequestBody(Object bodyObject, ContentType contentType, String httpMethod) {
RequestBody requestBody = null;
if (contentType == null) {
logger.debug(REQUEST_BODY_FORMAT, "(Empty)");
if (httpMethod.equalsIgnoreCase("POST")) {
requestBody = RequestBody.create("", null);
}
} else if (contentType == ContentType.MULTIPART_FORMDATA) {
logger.debug(REQUEST_BODY_FORMAT, bodyObject);
var bodyBytes = HttpMultipart.toByteArrays((Map<String, Object>) bodyObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,28 @@ default void shouldShutdownWithoutExceptions() {
testShutdown();
}

@Test
default void shouldReturnAnObjectSyncWhenIsPostMethodAndBodyIsEmpty() throws IOException, InterruptedException {
setMocksForString(SyncType.SYNC, "{\"id\":100,\"description\":\"Description\",\"active\":true}");

var service = getHttpProcessor().createProxy(ITest.SyncService.class);
var actualDemo = service.cancelDemo(100);
var expectedDemo = new ITest.Demo(100, "Description", true);

assertEquals(expectedDemo, actualDemo);
}

@Test
default void shouldReturnAnObjectAsyncWhenIsPostMethodAndBodyIsEmpty() throws IOException, InterruptedException {
setMocksForString(SyncType.ASYNC, "{\"id\":100,\"description\":\"Description\",\"active\":true}");

var service = getHttpProcessor().createProxy(ITest.AsyncService.class);
var actualDemo = service.cancelDemo(100).join();
var expectedDemo = new ITest.Demo(100, "Description", true);

assertEquals(expectedDemo, actualDemo);
}

private String transformUsers(String jsonInput) {
List<String> flatUsers = new ArrayList<>();
String patternStr = "\"id\":\\s*(\\d+).*?" + // id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ interface AsyncService {
@GET("/{demoId}")
CompletableFuture<Demo> getDemo(@Path("demoId") Integer demoId);

@POST
CompletableFuture<Demo> cancelDemo(@Path("demoId") Integer demoId);

@GET("/{genericDemoId}")
CompletableFuture<Generic<Demo>> getGenericDemo(@Path("genericDemoId") Integer genericDemoId);

Expand Down Expand Up @@ -113,6 +116,9 @@ interface SyncService {
@GET("/{demoId}")
Demo getDemo(@Path("demoId") Integer demoId);

@POST
Demo cancelDemo(@Path("demoId") Integer demoId);

@GET("/{genericDemoId}")
Generic<Demo> getGenericDemo(@Path("genericDemoId") Integer genericDemoId);

Expand Down

0 comments on commit bd859e6

Please sign in to comment.