gpt4 book ai didi

node.js - aws-sdk 上传到 S3 在 KOA 中可以工作,但通过 Mocha 调用时会停止工作

转载 作者:太空宇宙 更新时间:2023-11-04 00:40:03 25 4
gpt4 key购买 nike

我需要从 koa 上传一个文件到 s3,我对 koa 还很陌生,可能遗漏了一些明显的东西。它实际上完成了 200,但该文件从未显示在 s3 上。

这是我的 app.js 的一个片段:

'use strict';

var jwt = require('koa-jwt');


var bodyParser = require('koa-bodyparser');
var koaBody = require('koa-body');

const app = module.exports = require('koa')()
.use(koaBody({multipart:true})) // this is to pase only multipart forms
.use(require('./routes/common'))
.use(require('./routes/auth'))
.use(require('./routes/users_public'))
.use(jwt({ secret: SECRET }))
//protected routes below this line
.use(require('./routes/subcontractors_private'))

.listen(process.env.PORT || 3000);

subcontractors_private.js 看起来像这样:

'use strict';

var AWS = require('aws-sdk');
var fs = require('fs');
var zlib = require('zlib');


var S3_BUCKET = require('../consts').S3_BUCKET;
var S3_OPTIONS = require('../consts').S3_OPTIONS;


module.exports = require('koa-router')()

.post('/subcontractors/:subcontractor_id/coi', function *(next) {
var body = JSON.stringify(this.request.body, null, 2)

let subcontractor_id = this.params.subcontractor_id;
var file = this.request.body.files.coi.path;
var body = fs.createReadStream(file).pipe(zlib.createGzip());


var s3obj = new AWS.S3(
{params:
{
Bucket: 'coi-test',
Key: 'i/' + subcontractor_id + '.png.zgip'
}
});

s3obj.upload({Body: body})
.on('httpUploadProgress', function(evt) {
console.log(evt);
})
.send(function(err, data) {
console.log(err, data);
});


this.response.status = 200;
this.body = { "result": "subcontractor CIO successfully uploaded"};
})
.routes();

最后,测试文件夹中的 subcontractor_private.js 片段:

'use strict';

const supertest = require('co-supertest'); // SuperAgent-driven library for testing HTTP servers
const expect = require('chai').expect; // BDD/TDD assertion library
require('co-mocha'); // enable support for generators in mocha tests using co
var uuid = require('uuid');

var db = require('../../consts').DB;
var moment = require('moment');

const app = require('../../app.js');

const request = supertest.agent(app.listen());

var assert = require('assert');

describe('/subcontractors private routes testing', function() {

it.only('should be able to upload COI for subcontractor', function*() {

const coi_expires_at = moment().add(1, 'd').format();
const response =
yield request.post('/subcontractors')
.set('Content-Type', 'application/json')
.set('Authorization', 'Bearer ' + token)
.send({name: "Joe Doh"})
.end();

//now try to upload the coi file
const response1 =
yield request.post('/subcontractors/' + response.body.subcontractor.id + "/coi")
.set('Authorization', 'Bearer ' + token)
.field('Content-Type', 'multipart/form-data')
.field('coi_expires_at', coi_expires_at)
.attach('coi', './assets/logo-big.png')
.end();

expect(response1.status).to.equal(200, response1.text);
expect(response1.body).to.be.an('object');
expect(response1.body).to.be.json;
expect(response1.body).to.contain.keys('result');
expect(response1.body.result).to.equal('subcontractor CIO successfully uploaded');

});
});

我尝试将上传代码作为独立的 js 文件(通过 Node 运行),并且工作正常。但是,当我将其作为 mocha 测试的 Node 应用程序运行时,该方法以响应 200 完成,并且永远不会完成上传。我做错了什么?

最佳答案

问题在于这些东西的异步性质。测试无需等待请求完成(甚至正确启动)即可完成。我不完全清楚如何正确执行此操作,但添加以下内容:

  it.only('should be able to upload COI for subcontractor', function*(done) {

(完成回调参数)使测试等待调用“完成”回调,从而允许 aws sdk 完成请求。但是,我不确定如何修复您的测试,因为这是两次异步的。 koa 服务器中的 Controller 方法异步返回,无需等待 S3 请求完成,因此 mocha 测试无法等待 S3 处理完成。

(另请参阅我的相关问题: aws-sdk s3 upload not working from mocha test )

关于node.js - aws-sdk 上传到 S3 在 KOA 中可以工作,但通过 Mocha 调用时会停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37241443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com