Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -127,7 +128,11 @@ public Mono<ActionExecutionResult> triggerApiCall(WebClient client, HttpMethod h
*/
if (contentType.includes(MediaType.APPLICATION_JSON)) {
try {
String jsonBody = new String(body, StandardCharsets.UTF_8);
Charset charset = contentType.getCharset();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a JUnit TC for this in the file RestApiPluginTest.java ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Will do.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@balachandarsv please mention here once you are done.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please check @sumitsum

if(charset==null) {
charset = StandardCharsets.UTF_8;
}
String jsonBody = new String(body, charset);
result.setBody(objectMapper.readTree(jsonBody));
responseDataType = ResponseDataType.JSON;
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1903,5 +1903,38 @@ public void whenAPIReturnsNon200_doNotStringifyResponseBody() throws IOException
})
.verifyComplete();
}

@Test
public void testExtractResponseCharsetAndReadResponse() {
MockWebServer mockWebServer = new MockWebServer();
MockResponse mockResponse = new MockResponse()
.setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=UTF-16")
.setBody("{\"name\":\"John\"}");
mockWebServer.enqueue(mockResponse);
mockWebServer.start();

DatasourceConfiguration dsConfig = new DatasourceConfiguration();
dsConfig.setUrl(mockWebServer.url("/").toString());

ActionConfiguration actionConfig = new ActionConfiguration();
actionConfig.setHttpMethod(HttpMethod.GET);

Mono<ActionExecutionResult> resultMono = pluginExecutor.executeParameterized(null, new ExecuteActionDTO(), dsConfig, actionConfig);

StepVerifier.create(resultMono)
.assertNext(result -> {
assertTrue(result.getIsExecutionSuccess());
assertNotNull(result.getBody());
assertEquals("{\"name\":\"John\"}", result.getBody().toString());
})
.verifyComplete();

try {
mockWebServer.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}
}