|
36 | 36 | import com.google.longrunning.ListOperationsRequest;
|
37 | 37 | import com.google.protobuf.Empty;
|
38 | 38 | import com.google.protobuf.Field;
|
| 39 | +import com.google.protobuf.util.JsonFormat; |
| 40 | +import java.io.ByteArrayOutputStream; |
39 | 41 | import java.io.IOException;
|
40 | 42 | import java.util.Arrays;
|
41 | 43 | import java.util.HashMap;
|
@@ -177,4 +179,67 @@ public void testRequestUrlUnnormalizedPatch() throws IOException {
|
177 | 179 | Truth.assertThat(httpRequest.getRequestMethod()).isEqualTo("POST");
|
178 | 180 | Truth.assertThat(httpRequest.getHeaders().get("X-HTTP-Method-Override")).isEqualTo("PATCH");
|
179 | 181 | }
|
| 182 | + |
| 183 | + /* |
| 184 | + We use a separate RequestFormatter because formatting the body requests is what sets the charset to be UTF-8. |
| 185 | + The other tests above do not have a set a body request and instead send an EmptyContent (null Type/ CharSet) |
| 186 | + */ |
| 187 | + @Test |
| 188 | + public void testUnicodeValuesInBody() throws IOException { |
| 189 | + HttpRequestFormatter<Field> bodyRequestFormatter = |
| 190 | + ProtoMessageRequestFormatter.<Field>newBuilder() |
| 191 | + .setPath( |
| 192 | + "/name/{name=*}", |
| 193 | + request -> { |
| 194 | + Map<String, String> fields = new HashMap<>(); |
| 195 | + ProtoRestSerializer<Field> serializer = ProtoRestSerializer.create(); |
| 196 | + serializer.putPathParam(fields, "name", request.getName()); |
| 197 | + return fields; |
| 198 | + }) |
| 199 | + .setQueryParamsExtractor(request -> new HashMap<>()) |
| 200 | + .setRequestBodyExtractor( |
| 201 | + request -> |
| 202 | + ProtoRestSerializer.create().toBody("*", request.toBuilder().build(), true)) |
| 203 | + .build(); |
| 204 | + |
| 205 | + Field bodyRequestMessage = |
| 206 | + Field.newBuilder() |
| 207 | + .setName("feline ☺ → ←") |
| 208 | + .setNumber(2) |
| 209 | + .setDefaultValue("bird ☺ → ←") |
| 210 | + .setJsonName("mouse ☺ → ←") |
| 211 | + .setTypeUrl("small ☺ → ←") |
| 212 | + .build(); |
| 213 | + |
| 214 | + ApiMethodDescriptor<Field, Empty> methodDescriptor = |
| 215 | + ApiMethodDescriptor.<Field, Empty>newBuilder() |
| 216 | + .setFullMethodName("house.cat.get") |
| 217 | + .setHttpMethod("PUT") |
| 218 | + .setRequestFormatter(bodyRequestFormatter) |
| 219 | + .setResponseParser(responseParser) |
| 220 | + .build(); |
| 221 | + |
| 222 | + HttpRequestRunnable<Field, Empty> httpRequestRunnable = |
| 223 | + new HttpRequestRunnable<>( |
| 224 | + bodyRequestMessage, |
| 225 | + methodDescriptor, |
| 226 | + "www.googleapis.com/animals/v1/projects", |
| 227 | + HttpJsonCallOptions.newBuilder().build(), |
| 228 | + new MockHttpTransport(), |
| 229 | + HttpJsonMetadata.newBuilder().build(), |
| 230 | + (result) -> {}); |
| 231 | + |
| 232 | + HttpRequest httpRequest = httpRequestRunnable.createHttpRequest(); |
| 233 | + Truth.assertThat(httpRequest.getContent().getType()) |
| 234 | + .isEqualTo("application/json; charset=utf-8"); |
| 235 | + try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { |
| 236 | + // writeTo() uses the Charset when writing to the stream |
| 237 | + httpRequest.getContent().writeTo(byteArrayOutputStream); |
| 238 | + String output = byteArrayOutputStream.toString(); |
| 239 | + Field.Builder expectedBuilder = Field.newBuilder(); |
| 240 | + JsonFormat.parser().merge(output, expectedBuilder); |
| 241 | + Field result = expectedBuilder.build(); |
| 242 | + Truth.assertThat(result).isEqualTo(bodyRequestMessage); |
| 243 | + } |
| 244 | + } |
180 | 245 | }
|
0 commit comments