我正在尝试使用 FrisbyJS(位于用于 Node 的 Jasmine 之上)来测试我的 API。我想知道是否有人知道如何使用 Jasmine 提交/发送图像?
我当前的代码是...
var frisby = require('frisby');
var URL = 'http://localhost/api';
frisby.globalSetup({
request: {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'api': 'key'
}
},
timeout: (30 * 1000)
});
frisby.create('submit an image')
.post(URL + 'image', {images: '@test.jpg'}, {method: 'POST'})
.expectStatus(200)
.afterJSON(function(cart){
console.log('made it!');
})
}).toss();
我收到以下错误:
1) Frisby Test: submit an image
[ POST http://localhost/api ]
Message:
timeout: timed out after 30000 msec waiting for HTTP Request timed out before completing
Stacktrace:
undefined
Finished in 31.458 seconds
是的,图像 test.jpg 确实存在于与规范文件相同的文件夹中 :) 以及我执行规范本身的位置 (jasmine-node .)。
最佳答案
我根据 https://github.com/vlucas/frisby/blob/master/examples/httpbin_multipart_spec.js 给出的示例找到了解决方案
var frisby = require('frisby');
var fs = require('fs');
var path = require('path');
var FormData = require('form-data');
var contentPath = path.resolve(__dirname, './path/to/image.jpg');
var form = new FormData();
form.append('file', fs.createReadStream(contentPath), {
knownLength: fs.statSync(contentPath).size
});
frisby.create('Create content asset')
.post(apiPath + '/assets', form, {
json: false,
headers: {
'content-type': 'multipart/form-data; boundary=' + form.getBoundary(),
'content-length': form.getLengthSync(),
}
})
.expectStatus(201)
.toss()
关于node.js - 有没有办法用 Jasmine NodeJS 发送 JPG?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14150543/