2017-09-29 4 views
0

Ich habe diesen Aufruf einer API:Übersetzen eine API in einer chai Anfrage

curl -X PATCH --header 'Content-Type: application/json' --header 'Authorization: Bearer 863e2ddf246300f6c62ea9023d068805' - d '1' 'http://asdasd.com/api/loyalty/v1/Accounts/6064361727001553966/Cards'

und ich möchte eine Chai-Anfrage schreiben, um meine API zu testen. Ich schrieb dies:

describe('/PATCH Patch a card with a Status variable inactive test',() => { 


it('it should GET a sample error json response ', (done) => { 
    chai.request(app) 
    .patch('/loyalty/v1/cards/6064361727001553966') 
    .send({"cardStatus": "1" }) 
    .end((err, res) => { 
    res.should.have.status(200); 
    done(); 
}); 
}); 
}); 

aber auf diese Weise ich den Wert „1“ wie Wert des cardStatus Parameter übergeben. In dem API-Aufruf habe ich diese nur

-d '1'

Wie kann ich dies in der chai Anfrage reproduzieren? Es gibt eine Möglichkeit, diesen Parameter im Anfragetext ohne Parameterschlüssel zu übergeben.

Antwort

0

Ich fand eine Lösung. Ich habe diese auf meine JS-Datei:

app.use(function(req, res, next){ 
    if (req.is('text/*')) { 
    req.text = ''; 
    req.setEncoding('utf8'); 
    req.on('data', function(chunk){ req.text += chunk }); 
    req.on('end', next); 
    } else { 
    next(); 
    } 
}); 

und verwendet req.text die Zeichenfolge der Anfrage zu bekommen.

Die chai Anfrage wird nun wie folgt geschrieben:

describe('/PATCH Patch a card with a Status variable active test',() => { 
    it('it should GET a sample error json response ', (done) => { 
    chai.request(app) 
    .post('/loyalty/v1/cards/6064361727001553966') 
    .set('content-type', 'text/plain') 
    .send('1') 
    .end((err, res) => { 
    res.should.have.status(200); 
    done(); 
}); 
}); 
}); 

und ich kann meine api in der Befehlszeile wie folgt aufrufen:

curl -v -X POST --header ‚Content -Typ: text/plain '-d' 1 ' ' https://asdasd.com/api/loyalty/v1/cards/6064361727001553966 '

Verwandte Themen