简体   繁体   English

在线模拟响应中编码特殊字符

[英]Encoding special characters in wiremock response

I am facing a scenario where the character " " occasionally gets returned from my okhttp requests, and the character is causing some downstream issues.我面临这样一种情况:字符“ ”偶尔会从我的 okhttp 请求中返回,并且该字符会导致一些下游问题。 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.问题是wiremock 似乎不喜欢这个特殊字符。

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.但是,当我设置 wiremock 以返回带有特殊字符的响应(即使是单个字符,并且我想使用许多不同的字段进行测试)时,字符串化的响应确实具有响应,但数据为空。

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)));
}

I am facing a scenario where the character " " occasionally gets returned from my okhttp requests, and the character is causing some downstream issues.我面临这样一种情况:字符“ ”偶尔会从我的 okhttp 请求中返回,并且该字符会导致一些下游问题。 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.问题是wiremock 似乎不喜欢这个特殊字符。

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.但是,当我设置 wiremock 以返回带有特殊字符的响应(即使是单个字符,并且我想使用许多不同的字段进行测试)时,字符串化的响应确实具有响应,但数据为空。

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)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM