Skip to content

Commit 52df3eb

Browse files
committed
fix: call callout endpoint with proper body when it contains accents
gravitee-io/issues#8109
1 parent 3ee2157 commit 52df3eb

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed

src/main/java/io/gravitee/policy/callout/CalloutHttpPolicy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ private void doCallout(ExecutionContext context, Consumer<Void> onSuccess, Consu
244244
// Check the resolved body before trying to send it.
245245
if (body != null && !body.isEmpty()) {
246246
httpClientRequest.headers().remove(HttpHeaders.TRANSFER_ENCODING);
247-
httpClientRequest.putHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(body.length()));
247+
// Removing Content-Length header to let VertX automatically set it correctly
248+
httpClientRequest.headers().remove(HttpHeaders.CONTENT_LENGTH);
248249
futureResponse = httpClientRequest.send(Buffer.buffer(body));
249250
} else {
250251
futureResponse = httpClientRequest.send();

src/test/java/io/gravitee/policy/CalloutHttpPolicyIntegrationTest.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@
1515
*/
1616
package io.gravitee.policy;
1717

18-
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
19-
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
20-
import static com.github.tomakehurst.wiremock.client.WireMock.get;
21-
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
22-
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
23-
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
18+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
2419
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
2520
import static org.assertj.core.api.Assertions.assertThat;
2621

@@ -35,6 +30,8 @@
3530
import io.gravitee.policy.callout.CalloutHttpPolicy;
3631
import io.gravitee.policy.callout.configuration.CalloutHttpPolicyConfiguration;
3732
import io.reactivex.observers.TestObserver;
33+
import io.vertx.core.http.HttpHeaders;
34+
import io.vertx.core.json.JsonObject;
3835
import io.vertx.reactivex.core.buffer.Buffer;
3936
import io.vertx.reactivex.ext.web.client.HttpResponse;
4037
import io.vertx.reactivex.ext.web.client.WebClient;
@@ -103,6 +100,37 @@ void shouldDoCalloutAndSetResponseAsAttribute(WebClient client) throws Exception
103100
calloutServer.verify(getRequestedFor(urlPathEqualTo("/callout")).withHeader("X-Callout", equalTo("calloutHeader")));
104101
}
105102

103+
@Test
104+
@DisplayName("Should call callout endpoint with proper body when it contains accents")
105+
@DeployApi("/apis/callout-http-post-with-accents.json")
106+
void shouldDoCalloutAndSetResponseWithAccentAsAttribute(WebClient client) {
107+
wiremock.stubFor(post("/endpoint").willReturn(ok("réponse from backend")));
108+
calloutServer.stubFor(post("/callout").willReturn(ok("response from callout")));
109+
110+
final TestObserver<HttpResponse<Buffer>> obs = client
111+
.post("/test")
112+
.rxSendJsonObject(new JsonObject().put("clé", "value").put("key", "välæur"))
113+
.test();
114+
115+
awaitTerminalEvent(obs);
116+
obs
117+
.assertComplete()
118+
.assertValue(response -> {
119+
assertThat(response.statusCode()).isEqualTo(200);
120+
assertThat(response.bodyAsString()).isEqualTo("réponse from backend");
121+
return true;
122+
})
123+
.assertNoErrors();
124+
125+
wiremock.verify(postRequestedFor(urlPathEqualTo("/endpoint")));
126+
calloutServer.verify(
127+
postRequestedFor(urlPathEqualTo("/callout"))
128+
.withoutHeader(HttpHeaders.TRANSFER_ENCODING.toString())
129+
.withHeader(HttpHeaders.CONTENT_LENGTH.toString(), equalTo("33"))
130+
.withRequestBody(equalToJson("{\"clé\":\"value\", \"key\":\"välæur\"}"))
131+
);
132+
}
133+
106134
@Test
107135
@DisplayName("Should do callout Fire and Forget")
108136
@DeployApi("/apis/callout-http-fire-and-forget.json")
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"id": "my-api",
3+
"name": "my-api",
4+
"gravitee": "2.0.0",
5+
"proxy": {
6+
"context_path": "/test",
7+
"endpoints": [
8+
{
9+
"name": "default",
10+
"target": "http://localhost:8080/endpoint",
11+
"http": {
12+
"connectTimeout": 3000,
13+
"readTimeout": 60000
14+
}
15+
}
16+
]
17+
},
18+
"flows": [
19+
{
20+
"name": "flow-1",
21+
"methods": [
22+
"POST"
23+
],
24+
"enabled": true,
25+
"path-operator": {
26+
"path": "/",
27+
"operator": "STARTS_WITH"
28+
},
29+
"pre": [
30+
{
31+
"name": "Callout HTTP",
32+
"description": "",
33+
"enabled": true,
34+
"policy": "policy-http-callout",
35+
"configuration": {
36+
"method": "POST",
37+
"url": "http://localhost:8089/callout",
38+
"scope": "REQUEST_CONTENT",
39+
"body": "{#request.content}"
40+
}
41+
}
42+
]
43+
}
44+
]
45+
}

0 commit comments

Comments
 (0)