我正在尝试使用 frisby.js 测试一些 API。我在通过该.post()
方法发送一个 xml 块时遇到了麻烦。我可以在 Postman 中做到这一点。
这是我的代码:
var xml_body = envSetup.ENV_DATA.inrule.xml_post_kia1;
frisby.create('InRule 02 Verify XML Post')
.addHeaders({
'Accept': 'application/xml',
'Authorization': 'Basic <some internal token>',
'Content-Length': xml_body.length,
'Content-Type': 'application/xml'
})
.post(envSetup.URL + '/' + resource + '?action=basic', xml_body)
.inspectRequest()
.inspectBody()
.expectStatus(200)
.expectHeaderContains('Content-Type', 'application/xml')
.toss(); // InRule 02
这是输出:
$ jasmine-node --color --verbose --config ENV inrule spec/inrule_spec.js
{ json: false,
uri: 'http://dev.<company_api>/api/rules?action=basic',
body: null,
method: 'POST',
headers:
{ accept: 'application/xml',
authorization: 'Basic <some internal token>',
'content-length': '787',
'content-type': 'application/xml' },
inspectOnFailure: false,
baseUri: '',
timeout: 5000 }
<RuleResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://<company_api>/resources/rules"><EntityData i:nil="true" /><Error>Unhandled Exception: Rules controller caught Exception: Object reference not set to an instance of an object.</Error><LogDetailLocation i:nil="true" /><ResponseText>Error Occurred</ResponseText><RuleApplication i:nil="true" /><Status>5</Status><StatusDescription i:nil="true" /></RuleResponse>
Frisby Test: InRule 01 Verify Get - 253 ms
[ GET http://dev.<company_api>/api/rules?action=basic ] - 253 ms
Frisby Test: InRule 02 Verify Schema (Post) - 24 ms
[ POST http://dev.<company_api>/api/rules?action=basic ] - 23 ms
Failures:
1) Frisby Test: InRule 02 Verify Schema (Post)
[ POST http://dev.<company_api>/api/rules?action=basic ]
Message:
Expected 500 to equal 200.
Stacktrace:
Error: Expected 500 to equal 200.
at null.<anonymous> (H:\frisby\esb\node_modules\frisby\lib\frisby.js:493:42)
at null.<anonymous> (H:\frisby\esb\node_modules\frisby\lib\frisby.js:1074:43)
at Timer.listOnTimeout (timers.js:92:15)
Finished in 0.278 seconds
2 tests, 5 assertions, 1 failure, 0 skipped
我试过有/没有'Content-Length'
我试过 'Content-Type': 'application/xml' 和 'Content-Type': 'application/xml;charset=UTF-8'
我认为问题在于显示的.inspectRequest()
事实
body: null,
当我查看 NodeJS 请求的成功邮递员“生成代码”时,它显示了一个正文:
var options = { method: 'POST',
url: 'http://dev.rules.itagroupservices.int/api/rules',
qs: { action: 'basic' },
headers:
{ 'postman-token': '69bf39bc-3a6e-f1ea-7d8c-319ebbd41eef',
'cache-control': 'no-cache',
accept: 'application/xml',
authorization: 'Basic <some internal token>',
'content-type': 'application/xml' },
body: '<RuleRequest xmlns:i="http://www.w3.org/2001/XMLSchema-instance"\r\n\t\txmlns="http://<company_api>/resources/rules">\r\n \r\n <EntityData>\r\n<![CDATA[bunch of xml data]]>\r\n \r\n </EntityData>\r\n</RuleRequest>' };
我不知道为什么 frisby 没有显示尸体(因此没有发送一个?)即使我把xml_body
它放在应该去的地方。