在 chai 请求中翻译 API
Translate an API in a chai request
我给 API:
打电话
curl -X PATCH --header 'Content-Type: application/json' --header 'Authorization: Bearer 863e2ddf246300f6c62ea9023d068805' -d '1' 'http://asdasd.com/api/loyalty/v1/Accounts/6064361727001553966/Cards'
我想写一个 chai 请求来测试我的 API。
我写了这个:
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();
});
});
});
但通过这种方式,我传递了“1”值,例如 cardStatus 参数的值。在 API 调用中我只有这个
-d '1'
如何在 chai 请求中复制它?
有没有办法在request body中不带参数key传递这个参数?
我找到了解决办法。
我将其添加到我的 .js 文件中:
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();
}
});
并使用 req.text
从请求中获取字符串。
chai请求现在是这样写的:
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();
});
});
});
我可以在命令行中这样调用我的 api:
curl -v -X POST --header 'Content-Type: text/plain' -d '1'
'https://asdasd.com/api/loyalty/v1/cards/6064361727001553966'
我给 API:
打电话curl -X PATCH --header 'Content-Type: application/json' --header 'Authorization: Bearer 863e2ddf246300f6c62ea9023d068805' -d '1' 'http://asdasd.com/api/loyalty/v1/Accounts/6064361727001553966/Cards'
我想写一个 chai 请求来测试我的 API。 我写了这个:
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();
});
});
});
但通过这种方式,我传递了“1”值,例如 cardStatus 参数的值。在 API 调用中我只有这个
-d '1'
如何在 chai 请求中复制它? 有没有办法在request body中不带参数key传递这个参数?
我找到了解决办法。 我将其添加到我的 .js 文件中:
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();
}
});
并使用 req.text
从请求中获取字符串。
chai请求现在是这样写的:
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();
});
});
});
我可以在命令行中这样调用我的 api:
curl -v -X POST --header 'Content-Type: text/plain' -d '1' 'https://asdasd.com/api/loyalty/v1/cards/6064361727001553966'