英文:
Encoding special characters in wiremock response
问题
我面临这样一种情况,偶尔会在我的 okhttp 请求中返回字符"�",而这个字符会引起一些下游问题。因此,我已经添加了代码来移除这个字符,如果存在的话,我想添加一个测试用例来确保这个操作正常工作。问题是,wiremock 似乎不喜欢这个特殊字符。
通常情况下,我会从响应中提取数据,如下所示:
String stringifiedResponse = response.getResponseString();
if (response.isSuccessful()) {
custResp = response.getData();
通常情况下,这对于我所有的请求都可以正常工作。然而,当我设置 wiremock 返回一个带有特殊字符的响应(即使是一个单独的字符,我想测试多个不同的字段),字符串化的响应确实包含了响应,但数据却是 null。
这是我在测试类中设置模拟的方式:
public static void mockCPInvalidChars(String ssn) {
String customerPrefillPrimaryOwnerRequest = " {\n" +
" \"customers\": [\n" +
" {\n" +
" \"partyId\": \"" + ssn + "\",\n" +
" \"idType\": \"LID\"\n" +
" }\n" +
" ]\n" +
"}";
String partyId =
ssn.substring(0, 3) + "-"
+ ssn.substring(3, 5) + "-"
+ ssn.substring(5, 9);
String customerPrefillPrimaryOwnerResponse = "{\"totalRecords\":1,\"customers\":[{\"partyId\":\"" + partyId + "\",\"idType\":\"LID\",\"sourceCode\":\"ICS\",\"firstName\":\"R�EEVES\",\"lastName\":\"WI�CKLIFF\",\"address1\":\"59 Ma�iling LANE\",\"address2\":\"ma�l2\",\"address3\":\"mail�3\",\"address4\":\"mai�l4\",\"city\":\"Mai�l\",\"state\":\"M�A\",\"zipCode\":\"010�10\",\"primaryPhone\":\"817504�0350\",\"alternatePhone\":\"81750�40351\",\"birthDate\":\"1902-0�2-10\",\"foreignIndicator\":\"N\",\"alternateAddress1\":\"88 LEG�AL LANE\",\"alternateAddress2\":\"leg�al2\",\"alternateAddress3\":\"lega�l3\",\"alternateAddress4\":\"lega�l4\",\"alternateCity\":\"LEG�AL\",\"alternateZipCode\":\"020�20\",\"alternateState\":\"L�A\",\"alternateForeignIndicator\":\"N\",\"mailTo\":\"\",\"alternateMailTo\":\"\",\"institutionId\":\"N\",\"taxId\":\"" + ssn + "\",\"taxIdIssuer\":\"S\"}]}";
stubFor(post(urlEqualTo("/my/url"))
.withRequestBody(equalToJson(customerPrefillPrimaryOwnerRequest))
.withHeader("Authorization", equalTo("Bearer " + OauthService.getOauthToken().orElse(new OauthToken()).getAccess_token()))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/xml")
.withHeader("Content-Length", String.valueOf(customerPrefillPrimaryOwnerResponse.length()))
.withBody(customerPrefillPrimaryOwnerResponse)));
}
英文:
I am facing a scenario where the character "�" occasionally gets returned from my okhttp requests, and the character is causing some downstream issues. So I have added code to remove this character should it exist and I would like to add a test case to ensure this works correctly. The issue is that wiremock does not seem to like this special character.
Normally I would pull out the data from the response as so:
String stringifiedResponse = response.getResponseString();
if (response.isSuccessful()) {
custResp = response.getData();
Normally this works fine for all my requests. However, when I set up wiremock to return a response with the special character (even as a single one, and I would like to test with many different fields), the stringified response does have the response but the data is null.
This is how I have set up the mocks in my test class
public static void mockCPInvalidChars(String ssn) {
String customerPrefillPrimaryOwnerRequest = " {\n" +
" \"customers\": [\n" +
" {\n" +
" \"partyId\": \"" + ssn + "\",\n" +
" \"idType\": \"LID\"\n" +
" }\n" +
" ]\n" +
"}";
String partyId =
ssn.substring(0, 3) + "-"
+ ssn.substring(3, 5) + "-"
+ ssn.substring(5, 9);
String customerPrefillPrimaryOwnerResponse = "{\"totalRecords\":1,\"customers\":[{\"partyId\":\"" + partyId + "\",\"idType\":\"LID\",\"sourceCode\":\"ICS\",\"firstName\":\"R�EEVES\",\"lastName\":\"WI�CKLIFF\",\"address1\":\"59 Ma�iling LANE\",\"address2\":\"ma�l2\",\"address3\":\"mail�3\",\"address4\":\"mai�l4\",\"city\":\"Mai�l\",\"state\":\"M�A\",\"zipCode\":\"010�10\",\"primaryPhone\":\"817504�0350\",\"alternatePhone\":\"81750�40351\",\"birthDate\":\"1902-0�2-10\",\"foreignIndicator\":\"N\",\"alternateAddress1\":\"88 LEG�AL LANE\",\"alternateAddress2\":\"leg�al2\",\"alternateAddress3\":\"lega�l3\",\"alternateAddress4\":\"lega�l4\",\"alternateCity\":\"LEG�AL\",\"alternateZipCode\":\"020�20\",\"alternateState\":\"L�A\",\"alternateForeignIndicator\":\"N\",\"mailTo\":\"\",\"alternateMailTo\":\"\",\"institutionId\":\"N\",\"taxId\":\"" + ssn + "\",\"taxIdIssuer\":\"S\"}]}";
stubFor(post(urlEqualTo("/my/url"))
.withRequestBody(equalToJson(customerPrefillPrimaryOwnerRequest))
.withHeader("Authorization", equalTo("Bearer " + OauthService.getOauthToken().orElse(new OauthToken()).getAccess_token()))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/xml")
.withHeader("Content-Length", String.valueOf(customerPrefillPrimaryOwnerResponse.length()))
.withBody(customerPrefillPrimaryOwnerResponse)));
}
答案1
得分: 0
你的问题出在没有转义替换字符。
� 转换为 \uFFFD
,但你还需要转义转义字符(奇怪的 JSON),所以变成 \\uFFFD
,或者在另一个字符串中间变成 "na\\uFFFDthan"
。
英文:
Your issue is coming from not escaping the replacement character.
� translates to \uFFFD
, but you'll need to escape the escape character as well (silly JSON), so that becomes \\uFFFD
, or in the middle of another string "na\\uFFFDthan"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论