负载中的意外字符

Unexpected character in payload

我的 Citrus 测试向某些 REST API 发送(旅行)请求。响应处理如下:

http()
.client("http://localhost:18082/cases")
.send()
.post()
.accept("application/json; charset=UTF-8")
.contentType("application/json")
//.payload(new ClassPathResource("templates/travelrequest.json"));
.payload(
"{ "+
"\"definition\": \"travelrequest.xml\", "+
"\"name\": \"travelrequest\" "+
"} "
);

虽然收到了响应代码 500,但这是我所期望的。在 Wireshark 中,我捕获了以下包:

Host: localhost:18082
Connection: Keep-Alive                
User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_121)
Accept-Encoding: gzip,deflate

{ "definition": "travelrequest.xml", "name": "travelrequest" } HTTP/1.1 500 Internal Server Error
Server: spray-can/1.3.3
Date: Thu, 13 Apr 2017 15:33:37 GMT

当我将负载移动到模板时,我的测试的接收部分现在看起来像这样:

http()
.client("http://localhost:18082/cases")
.send()
.post()
.accept("application/json; charset=UTF-8")
.contentType("application/json")
.payload(new ClassPathResource("templates/travelrequest.json"));
//.payload(
//        "{ "+
//        "\"definition\": \"travelrequest.xml\", "+
//       "\"name\": \"travelrequest\" "+
//        "} "
//);

模板资源包含以下文本:

{
"definition": "travelrequest.xml",
"name": "travelrequest"
}

当我运行这个测试时,我收到一个不同的响应代码:400。在Wireshark中我捕获了以下包:

Host: localhost:18082
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_121)
Accept-Encoding: gzip,deflate

?{
"definition": "travelrequest.xml",
"name": "travelrequest"
}HTTP/1.1 400 Bad Request
Server: spray-can/1.3.3
Date: Thu, 13 Apr 2017 15:36:15 GMT

请注意请求以意外的问号开头。这个问号在 Citrus 输出中不可见:

17:36:13,629 DEBUG    client.HttpClient| Sending HTTP message to: 'http://localhost:18082/cases'
17:36:13,629 DEBUG    client.HttpClient| Message to send:
 {
"definition": "travelrequest.xml",
"name": "travelrequest"
}
17:36:13,630 DEBUG ingClientInterceptor| Sending Http request message

请注意左括号前的 space。

这是什么特殊字符吗?为什么将它添加到有效负载中?有合理的解释吗?

干杯, 埃德

似乎是编码问题。 Citrus 在读取文件内容时默认使用 UTF-8 编码。也许文件使用了一些其他编码,第一个字符是这种差异的结果。

请检查文件编码。您可以通过设置系统 属性

告诉 Citrus 使用其他编码
citrus.file.encoding=UTF-8

您还可以将此 属性 添加到 citrus-application.properties 文件,如下所述:http://www.citrusframework.org/reference/html/configuration.html#citrus-application-properties