2016-07-15 21 views
0

如何使用令牌發送來測試文件上傳?我回來了「0」,而不是確認上傳。如何使用Supertest對文件上傳進行單元測試 - 並且發送令牌?

這是一個失敗的測試:

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

這是一個工作測試:

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

任何建議都感激!

回答

0

附加文件時似乎令牌字段被忽略。我的解決方法是添加令牌網址查詢參數:

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

您的身份驗證的中間件必須設置爲從URL查詢參數提取JWT。 Passport-JWT在我的服務器上執行此提取。

相關問題