Post 带有表单数据的请求在 mocha 测试中引发 400 错误

Post request with form data raising 400 error in mocha test

我正在尝试 运行 使用 docker 容器在服务器内测试用例。当我尝试使用图像字段测试 post 请求时,其他字段一直在引发错误请求 400。该服务似乎可以很好地满足其他请求。我正在 post 此处查看代码片段。

 it('company  registartion', function(done){
request(app).post('/companies/')
    .set({apikey: 'TestHashKey',headers: headers})
    .type('form')
     .send({"name": "Test_company", "phoneNumber": "+123456741538", "logo":'/app/test/images/company_logo.jpg'})
     .expect(200)
     .end(function(err,res) {
                    if (err) {
                            throw err;
                    }
      done();
      });
    });

回复附在此处。

 GMT uncaughtException: expected 200 "OK", got 400 "Bad Request"

如果 POST 请求正文包含图像,则可以通过 .attach("field":"absolute path of the image") 附加图像。可以像下面的代码。

it("user test ", function(done){
request(app).post('/user')
    .field('Content-Type', 'multipart/form-data')
    .field('name', 'xyz')
    .field('phoneNumber','+258114311354')
    .attach("logo","test/images/user.jpg") 
    .expect(200)
    .end(function(err,res) {
       if (err) {
          throw err;
        }
      done();
      });
});