2016-08-11 5 views
0

私はfrisby.jsでいくつかのAPIをテストしようとしています。私は.post()メソッドでxmlのブロックを送信するのに問題が発生しました。私は郵便配達でそれをやってもいい。 「frisby.js .post() - 本文にxmlを送信するのに問題があります

$ 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'

のない私が試した:ここ

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 

が出力されます。ここでは

は私のコードです「コンテンツタイプ」:「アプリケーション/ xml」と「コンテンツタイプ」:「アプリケーション/ xml;文字セット= UTF-8」

私は問題だと思いますそれは体を示して要求に私が成功したポストマンを見てみると.inspectRequest()はNodeJSは「コードの生成」

body: null,

を示しているという事実である:

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を置くべき場所に置いているにもかかわらず、ボディを表示していません(したがって、1つも送りません)。ケース他で私の質問に直接答えは、これと同じ問題を抱えているわけではないが

答えて

0

ここに正式な回答があります:XMLを送信するには、リクエスト本体をヘッダーの一部として送信する必要があります。

frisby.create('Post XML to api') 
    .post(someUrl, {}, { 
    headers: headers4, 
    body: xml1 
    } 
) 
    //.inspectRequest() 

    //.inspectBody() 
    .expectHeaderContains('Content-Type', 'application/xml') 
    .expectStatus(200) 
.toss(); 

あなたは、私が.post(url, body, header)形式で{}で空の本体部分を残してきた見ることができます。そして、第三.post変数に、あなたはそれをここにbody: xmlVariable

別の例を与える:https://github.com/vlucas/frisby/issues/290

0

...あなたはfrisbyのうち、あなたの心を取得し、代わりにjasmine-nodeをバックステップして使用することができます - そして、あなたのfrisby _spec.jsファイル内で! Node.jsにはrequestパッケージがあり、何でも投稿できるので、ジャスミンのdescribe/itを実行できます。

describe('Stuff', function() { 

    it('DoIt', function(done) { 
    var options = { 
     method: 'POST', 
     url: envSetup.URL + '/' + resource, 
     qs: { action: 'basic' }, 
     headers: 
     { accept: 'application/xml', 
     authorization: yourAuthorization, 
     'content-type': 'application/xml' }, 
     body: xml_variable 
    }; 

    request(options, function (error, response, body) { 
     if (error) throw new Error(error); 
     //console.log(body); 
     expect(response.statusCode).toBe(200); 
     done(); 
    }); 
    }); 
}); 

これが成功したAPIにXMLを投稿し、バックbodyの形でREPONSEを取得します。私はむしろfrisby.jsでこれを行うことができるので、私は人々の解決策にまだ開いています。

関連する問題