在 wiremock 响应中编码特殊字符
Encoding special characters in wiremock response
我遇到这样一种情况,字符“�”偶尔会从我的 okhttp 请求中 returned,并且该字符会导致一些下游问题。所以我添加了代码来删除这个字符,如果它存在的话,我想添加一个测试用例以确保它正常工作。问题是wiremock似乎不喜欢这个特殊字符。
通常我会这样从响应中提取数据:
String stringifiedResponse = response.getResponseString();
if (response.isSuccessful()) {
custResp = response.getData();
通常这可以很好地满足我的所有请求。但是,当我将 wiremock 设置为 return 具有特殊字符的响应时(即使是单个响应,我想用许多不同的字段进行测试),字符串化响应确实有响应但数据为空.
这就是我在测试中设置模拟的方式 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)));
}
您的问题来自于没有转义替换字符。
� 转换为 \uFFFD
,但您还需要转义转义字符(愚蠢的 JSON),这样就变成了 \uFFFD
,或者在另一个中间字符串 "na\uFFFDthan"
我遇到这样一种情况,字符“�”偶尔会从我的 okhttp 请求中 returned,并且该字符会导致一些下游问题。所以我添加了代码来删除这个字符,如果它存在的话,我想添加一个测试用例以确保它正常工作。问题是wiremock似乎不喜欢这个特殊字符。
通常我会这样从响应中提取数据:
String stringifiedResponse = response.getResponseString();
if (response.isSuccessful()) {
custResp = response.getData();
通常这可以很好地满足我的所有请求。但是,当我将 wiremock 设置为 return 具有特殊字符的响应时(即使是单个响应,我想用许多不同的字段进行测试),字符串化响应确实有响应但数据为空.
这就是我在测试中设置模拟的方式 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)));
}
您的问题来自于没有转义替换字符。
� 转换为 \uFFFD
,但您还需要转义转义字符(愚蠢的 JSON),这样就变成了 \uFFFD
,或者在另一个中间字符串 "na\uFFFDthan"