0

我的 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

请注意左括号前的空格。

这是什么特殊人物吗?为什么将其添加到有效负载中?有没有合乎逻辑的解释?

干杯,埃德

4

1 回答 1

0

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

请检查文件编码。您可以通过设置 System 属性告诉 Citrus 使用其他编码

citrus.file.encoding=UTF-8

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

于 2017-04-14T12:31:02.110 回答