【问题标题】:How to unit test file upload with Supertest -and- send a token?如何使用 Supertest 对文件上传进行单元测试并发送令牌?
【发布时间】:2016-11-19 00:37:32
【问题描述】:

如何在发送令牌的情况下测试文件上传?我返回“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();
           });
    });
});

这是一个有效的测试:

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

欢迎提出任何建议!

【问题讨论】:

    标签: unit-testing mocha.js endpoint chai supertest


    【解决方案1】:

    使用 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');
    

    更好的是,根据docs,您可以制作一个 Buffer 对象来附加:

    const buffer = Buffer.from('some data');
    
    return request(server)
      .post('/route')
      .set('Authorization', 'bearer ' + token)
      .attach('name', buffer, 'custom_file_name.txt');
    

    【讨论】:

      【解决方案2】:

      似乎在附加文件时令牌字段被覆盖。我的解决方法是将令牌添加到 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();
          });
      });
      

      您的身份验证中间件必须设置为从 URL 查询参数中提取 JWT。 Passport-JWT 在我的服务器上执行此提取。

      【讨论】:

        【解决方案3】:

        官方文件说明

        当你使用 .field() 或 .attach() 时,你不能使用 .send() 并且你不能设置 Content-Type(会为你设置正确的类型)。

        所以替换 .send({"token": config.token}) .field("token", config.token)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-12-23
          • 2012-06-25
          • 2017-04-13
          • 2021-09-18
          • 2011-07-12
          相关资源
          最近更新 更多