我有一个API,它接受一个图像文件作为输入,上传它,并返回一个指向上传的图像的永久链接。
返回的JSON示例:
{ "url": "localhost:3000/public/uploads/1471759901731.jpg" }我的测试使用mocha和chai-http:
it('should upload single valid picture on /pictures POST', function(done) {
chai.request(app)
.post('/pictures')
.attach('picture', fs.readFileSync('test/test_data/banana.png'), 'banana.png')
.end(function(err, res) {
res.should.have.status(200);
res.should.be.json;
res.body.should.be.a('object');
res.body.should.have.property('url');
// should follow the url, ensure status 200
// follow res.body.url
done();
});
});如何遵循url以确保其有效并返回状态200?
发布于 2016-08-21 15:23:40
通过一些试验和错误,我让它工作了。
it('should upload single valid picture on /pictures POST', function(done) {
chai.request(app)
.post('/pictures')
.attach('picture', fs.readFileSync('test/test_data/banana.png'), 'banana.png')
.end(function(err, res) {
res.should.have.status(200);
res.should.be.json;
res.body.should.be.a('object');
res.body.should.have.property('url');
// remove hostname from url
var pic_url = res.body.url;
pic_url = pic_url.split('/');
pic_url = '/' + pic_url.splice(1).join('/');
// follow the link
chai.request(app)
.get(pic_url)
.end(function(err, res) {
res.should.have.status(200);
content_type = res.header['content-type'].split('/')[0];
(content_type).should.equal('image');
done();
});
});
});但这似乎不是最优雅的解决方案。我找不到其他人在chai.request(app)中嵌套chai.request(app)。
https://stackoverflow.com/questions/39061459
复制相似问题