Skip to content

Commit

Permalink
Bug fix #124: Unicode support for execute-request (#128)
Browse files Browse the repository at this point in the history
* #124: Unicode support for execute-request.
When a body is sent with multipart-form-dara content type, we were using the default content type when adding the multipart string to the actual MultipartEntity object. This default content type is plain/text with ISO_8859_1 encoding charset. The fix was to enforce using StandarCharsets.UTF_8.
  • Loading branch information
symphony-soufiane authored Jul 19, 2022
1 parent ac396e5 commit 1cb59ec
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,26 @@ private HttpResponse executeWithBody(Request request, Object body, Map<String, S
throws IOException {

// set body
String contentType = headers.get(HttpHeaders.CONTENT_TYPE);
if (body != null && ContentType.MULTIPART_FORM_DATA.getMimeType().equals(contentType)) {
String headerContentType = headers.get(HttpHeaders.CONTENT_TYPE);
if (body != null && ContentType.MULTIPART_FORM_DATA.getMimeType().equals(headerContentType)) {

final MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder
.create();

Map<String, Object> bodyAsMap = (LinkedHashMap<String, Object>) body;
bodyAsMap.forEach((key, value) -> multipartEntityBuilder.addTextBody(key, value.toString()));
ContentType textBodyContentType = ContentType.create("text/plain", StandardCharsets.UTF_8);
bodyAsMap.forEach((key, value) -> multipartEntityBuilder.addTextBody(key, value.toString(), textBodyContentType));

request.body(multipartEntityBuilder.build());

// The content type with boundary is provided in the entity, otherwise it is overridden
headers.remove(HttpHeaders.CONTENT_TYPE);

} else if (body != null && StringUtils.isNotEmpty(contentType)) {
if (contentType.equals(ContentType.APPLICATION_JSON.getMimeType()) && !(body instanceof String)) {
} else if (body != null && StringUtils.isNotEmpty(headerContentType)) {
if (headerContentType.equals(ContentType.APPLICATION_JSON.getMimeType()) && !(body instanceof String)) {
request.bodyString(OBJECT_MAPPER.writeValueAsString(body), ContentType.APPLICATION_JSON);
} else {
request.bodyString(body.toString(), ContentType.parse(contentType));
request.bodyString(body.toString(), ContentType.parse(headerContentType));
}

} else if (body != null) { // if no content type is provided, we set application/json by default
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.symphony.bdk.workflow;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
Expand Down Expand Up @@ -71,6 +72,26 @@ void executeRequestSuccessful(MappingBuilder method, String swadlFile, List<Stri
assertThat(workflow).isExecuted().executed(activitiesToBeExecuted.toArray(new String[0]));
}

@Test
void executeRequestEncodeBody(WireMockRuntimeInfo wmRuntimeInfo) throws Exception {
final Workflow workflow =
SwadlParser.fromYaml(getClass().getResourceAsStream("/request/execute-request-encode-body.swadl.yaml"));

this.putFirstActivityUrl(workflow, wmRuntimeInfo.getHttpBaseUrl() + "/api");

stubFor(post(UrlPattern.ANY)
.withHeader("Content-Type", containing("multipart/form-data; charset=ISO-8859-1;"))
.withRequestBody(containing("Content-Type: text/plain; charset=UTF-8"))
.withRequestBody(containing("Content-Disposition: form-data; name=\"text\""))
.willReturn(ok()));

engine.deploy(workflow);

engine.onEvent(messageReceived("/executeEncodeBody"));

assertThat(workflow).isExecuted().executed("executeRequestEncodeBody", "assertionScript");
}

@Test
void executeRequestSuccessfulToDelete(WireMockRuntimeInfo wmRuntimeInfo) throws Exception {
final Workflow workflow =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
id: execute-request-encode-body
activities:
- execute-request:
id: executeRequestEncodeBody
on:
message-received:
content: "/executeEncodeBody"
method: POST
url: https://wiremock.com/api
body:
text: こんにちは世界
headers:
Content-Type: multipart/form-data

- execute-script:
id: assertionScript
script: |
assert executeRequestEncodeBody.outputs.status == 200

0 comments on commit 1cb59ec

Please sign in to comment.