My 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
请注意,请求以意外的问号开头。此问号在柑橘输出中不可见:
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
请注意开口支架前面的空间。
这是一些特殊的角色吗?为什么要添加到有效载荷中?有合理的解释吗?
干杯, 编
答案 0 :(得分:0)
似乎是一个编码问题。在读取文件内容时,Citrus默认使用UTF-8
编码。也许该文件使用其他一些编码,第一个字符是这种差异的结果。
请检查文件编码。您可以通过设置系统属性
告诉Citrus使用其他一些编码citrus.file.encoding=UTF-8
您还可以将此属性添加到citrus-application.properties
文件中,如下所述:http://www.citrusframework.org/reference/html/configuration.html#citrus-application-properties