将字符转换为 UTF-8

发布于 2024-10-16 17:18:01 字数 833 浏览 1 评论 0原文

在我的单元测试中,我试图证明发送到客户端应用程序的 HTTP 响应仅包含有效的 UTF-8 字符。这是由于英镑符号“£”未作为 UTF-8 编码字符发送而导致客户端应用程序验证失败的问题。

在我的测试中,我执行以下操作:

// setup
byte[] outputData = "£".getBytes("ISO-8859-1");
String serviceXmlResponse = String.format("<?xml version=\"1.0\" encoding=\"utf-8\" ?><some>%s</some>", outputData.toString());
String expectedXmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><some>£</some>";
when(service.getMessage()).thenReturn(serviceXmlResponse);

// act
ModelAndView result = controller.handlePostRequest(request, response, mav);

// assert
assertEquals(expectedXmlContent, result.getModel().get(ApplicationController.RESPONSE_KEY));

生成的 XML 包含:

<?xml version="1.0" encoding="utf-8" ?><some>[B@1164b9b6</some>

我在这里做错了什么? 谢谢

In my unit test, i'm trying to prove that HTTP Responses sent onto client applications only contain valid UTF-8 characters. This is off the back of an issue whereby the British Pound symbol '£' was not sent as a UTF-8 encoded character, causing a validation failure for a client application.

In my test i'm doing the following:

// setup
byte[] outputData = "£".getBytes("ISO-8859-1");
String serviceXmlResponse = String.format("<?xml version=\"1.0\" encoding=\"utf-8\" ?><some>%s</some>", outputData.toString());
String expectedXmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><some>£</some>";
when(service.getMessage()).thenReturn(serviceXmlResponse);

// act
ModelAndView result = controller.handlePostRequest(request, response, mav);

// assert
assertEquals(expectedXmlContent, result.getModel().get(ApplicationController.RESPONSE_KEY));

The resultant XML contains:

<?xml version="1.0" encoding="utf-8" ?><some>[B@1164b9b6</some>

What am I doing wrong here?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离鸿 2024-10-23 17:18:01

如果 service.getMessage() 将 XML 作为 String 返回,而 result.getModel().get(ApplicationController.RESPONSE_KEY) 返回相同的值XML 以 byte[] 的形式编码,您的测试应该如下所示:

String serviceXmlResponse = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><some>£</some>";
when(service.getMessage()).thenReturn(serviceXmlResponse);

assertArrayEquals(serviceXmlResponse.getBytes("UTF-8"), (byte[]) result.getModel().get(ApplicationController.RESPONSE_KEY));

否则您的测试没有多大意义,特别是如果 result.getModel().get(ApplicationController.RESPONSE_KEY ) 返回一个 String,以便在生成模型属性后在视图中应用实际编码。

另请参阅:

If service.getMessage() returns your XML as a String, whereas result.getModel().get(ApplicationController.RESPONSE_KEY) returns the same XML in encoded form as a byte[], your test should look like this:

String serviceXmlResponse = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><some>£</some>";
when(service.getMessage()).thenReturn(serviceXmlResponse);

assertArrayEquals(serviceXmlResponse.getBytes("UTF-8"), (byte[]) result.getModel().get(ApplicationController.RESPONSE_KEY));

Otherwise your test doesn't make much sense, especially if result.getModel().get(ApplicationController.RESPONSE_KEY) returns a String, so that actual encoding is applied in the view, after generating model attributes.

See also:

旧人 2024-10-23 17:18:01

[B 是在 byte 数组上调用 toString() 时得到的结果。

因此,您的测试不起作用,因为它基本上说:“调用 getMessage() 时返回预期输出”。因此它测试 assertEquals (expected, Expected);

您的问题出在从套接字读取 XML 字节,然后将它们转换为 service.getMessage() 返回的字符串的代码中

所以你的代码必须如下所示:

String expectedXmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><some>£</some>";
byte[] data = expectedXmlContent.getBytes("UTF-8");

... send data to service as byte stream ...

assertEquals(expectedXmlContent, service.getMessage());

也不要混合编码;如果 XML 显示“UTF-8”,则必须在 getBytes() 中使用相同的编码。

[B is what you get when you call toString() on a byte array.

So your test doesn't work since it basically says: "Return the expected output when getMessage() is called". So it tests assertEquals (expected, expected);

Your problem is in the code which reads the XML bytes from the socket and then converts them into a String that is returned by service.getMessage()

So your code must look like:

String expectedXmlContent = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><some>£</some>";
byte[] data = expectedXmlContent.getBytes("UTF-8");

... send data to service as byte stream ...

assertEquals(expectedXmlContent, service.getMessage());

Also don't mix encodings; if the XML says "UTF-8", then you must use the same encoding in getBytes().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文