Chr*_*ell 9 unit-testing mocha.js endpoint chai supertest
如何使用正在发送的令牌测试文件上传?我返回“0”而不是确认上传。
这是一个失败的测试:
var chai = require('chai');
var expect = chai.expect;
var config = require("../config"); // contains call to supertest and token info
describe('Upload Endpoint', function (){
it('Attach photos - should return 200 response & accepted text', function (done){
this.timeout(15000);
setTimeout(done, 15000);
config.api.post('/customer/upload')
.set('Accept', 'application.json')
.send({"token": config.token})
.field('vehicle_vin', "randomVIN")
.attach('file', '/Users/moi/Desktop/unit_test_extravaganza/hardwork.jpg')
.end(function(err, res) {
expect(res.body.ok).to.equal(true);
expect(res.body.result[0].web_link).to.exist;
done();
});
});
});
Run Code Online (Sandbox Code Playgroud)
这是一个工作测试:
describe('Upload Endpoint - FL token ', function (){
this.timeout(15000);
it('Press Send w/out attaching photos returns error message', function (done){
config.api.post('/customer/upload')
.set('Accept', 'application.json')
.send({"token": config.token })
.expect(200)
.end(function(err, res) {
expect(res.body.ok).to.equal(false);
done();
});
});
Run Code Online (Sandbox Code Playgroud)
任何建议表示赞赏!
使用 supertest 4.0.2 我能够set获得令牌和attach文件:
import * as request from 'supertest';
return request(server)
.post('/route')
.set('Authorization', 'bearer ' + token)
.attach('name', 'file/path/name.txt');
Run Code Online (Sandbox Code Playgroud)
更好的是,根据docs,您可以制作一个 Buffer 对象来附加:
const buffer = Buffer.from('some data');
return request(server)
.post('/route')
.set('Authorization', 'bearer ' + token)
.attach('name', buffer, 'custom_file_name.txt');
Run Code Online (Sandbox Code Playgroud)
附加文件时似乎令牌字段被覆盖。我的解决方法是将令牌添加到 URL 查询参数:
describe('Upload Endpoint - FL token ', function (){
this.timeout(15000);
it('Press Send w/out attaching photos returns error message', function (done){
config.api.post('/customer/upload/?token='+config.token)
.attach('file', '/Users/moi/Desktop/unit_test_extravaganza/hardwork.jpg')
.expect(200)
.end(function(err, res) {
expect(res.body.ok).to.equal(false);
done();
});
});
Run Code Online (Sandbox Code Playgroud)
您的身份验证中间件必须设置为从 URL 查询参数中提取 JWT。Passport-JWT在我的服务器上执行此提取。
| 归档时间: |
|
| 查看次数: |
12135 次 |
| 最近记录: |