From bd859e6094c3c3550164d0fd4ea75e4ac3f7181d Mon Sep 17 00:00:00 2001 From: Sashir Estela Date: Fri, 24 Jan 2025 02:33:35 +0000 Subject: [PATCH] Handle OkHttp POST with empty body --- .../client/OkHttpClientAdapter.java | 7 ++++-- .../cleverclient/http/HttpProcessorTest.java | 22 +++++++++++++++++++ .../sashirestela/cleverclient/http/ITest.java | 6 +++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/sashirestela/cleverclient/client/OkHttpClientAdapter.java b/src/main/java/io/github/sashirestela/cleverclient/client/OkHttpClientAdapter.java index 1f6971f..1642085 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/client/OkHttpClientAdapter.java +++ b/src/main/java/io/github/sashirestela/cleverclient/client/OkHttpClientAdapter.java @@ -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()) @@ -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) bodyObject); diff --git a/src/test/java/io/github/sashirestela/cleverclient/http/HttpProcessorTest.java b/src/test/java/io/github/sashirestela/cleverclient/http/HttpProcessorTest.java index 59a3ce1..c7c2552 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/http/HttpProcessorTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/http/HttpProcessorTest.java @@ -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 flatUsers = new ArrayList<>(); String patternStr = "\"id\":\\s*(\\d+).*?" + // id diff --git a/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java b/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java index 0cdc18f..c86826c 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java @@ -75,6 +75,9 @@ interface AsyncService { @GET("/{demoId}") CompletableFuture getDemo(@Path("demoId") Integer demoId); + @POST + CompletableFuture cancelDemo(@Path("demoId") Integer demoId); + @GET("/{genericDemoId}") CompletableFuture> getGenericDemo(@Path("genericDemoId") Integer genericDemoId); @@ -113,6 +116,9 @@ interface SyncService { @GET("/{demoId}") Demo getDemo(@Path("demoId") Integer demoId); + @POST + Demo cancelDemo(@Path("demoId") Integer demoId); + @GET("/{genericDemoId}") Generic getGenericDemo(@Path("genericDemoId") Integer genericDemoId);