2016-08-11 2 views
0

Ich versuche einige APIs mit frisby.js zu testen. Ich habe Probleme bekommen, einen Block von xml über die .post() Methode zu senden. Ich kann es in Postman gut machen.frisby.js .post() - Probleme beim Senden von xml im Body

Hier ist mein Code:

$ 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 

Ich habe versucht, mit/ohne den 'Content-Length'

Ich habe versucht ':

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 

Hier ist der Ausgang Content-Type ':' application/xml 'und' Content-Type ':' application/xml; charset = UTF-8 '

Ich denke das Problem darin liegt, mit, dass die .inspectRequest()

body: null,

Als ich bei der erfolgreichen Postman aussehen zeigt ‚-Code generieren‘ für NodeJS es anfordern zeigt einen Körper:

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>' }; 

Ich weiß nicht, warum Frisby zeigt keinen Körper (und deshalb auch keinen?), obwohl ich den xml_body dorthin setze, wo er hingehört.

Antwort

0

Hier ist die offizielle Antwort: Um XML zu senden, müssen Sie Ihre Anfrage Körper als ein Stück der Kopfzeile senden.

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

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

können Sie sehen, ich habe den Körperteil leer mit links {} im .post(url, body, header) Format. Dann in dem dritten .post Variable, die Sie geben ein body: xmlVariable

Ein weiteres Beispiel hier: https://github.com/vlucas/frisby/issues/290

0

Während keine direkte Antwort auf meine Frage, falls andere das gleiche Problem haben ... können Sie Ihren Geist aus frisby bekommen und einen Schritt zurück und verwenden jasmine-node statt - und in Ihrer frisby _spec.js-Datei! Node.js hat ein request Paket, mit dem Sie alles posten können, so dass Sie einfach einen Jasmine beschreiben können.

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(); 
    }); 
    }); 
}); 

Dies wird erfolgreich XML an die API veröffentlichen und eine exzellente Spielbarkeit zurück in Form von body bekommen. Ich wäre eher in der Lage, dies in frisby.js zu tun, damit ich für die Lösungen von Leuten noch offen bin.

Verwandte Themen