0
我有一個express.js
post函數,可以很好地接收和處理文件。Request.js模擬一個文件上傳(multipart/form-data)
我想寫一個jasmine
單元測試,它使用模擬相同的東西fs
& request
。
const request = require("request")
const fs = require("fs")
const app = require("../app.js")
const base_url = "http://localhost:3002/"
var wordFile = fs.readFileSync(__dirname + '/test.docx', 'utf8')
describe("POST a word file to /word", function() {
it("parses the word file, and returns an array of components", function(done) {
request({
url: base_url + "word",
method: "POST",
json: true,
headers: {
"content-type": "multipart/form-data",
},
body: wordFile
}, function (error, response, body) {
expect(response.statusCode).toBe(200)
done()
}
)
})
})
我得到一個500錯誤,但是當我用下面的上傳查看我的應用程序,它工作正常:
<form action="/mediaDev/word" method="post" enctype="multipart/form-data">
<input type="file" name='doc' placeholder="Select file"/>
<br/>
<button>Upload</button>
</form>
是這個問題涉及到的事實,我讀的文件utf8還是別的?
感謝但這恰恰與nginx的路由做,終點似乎是正確的。與您發佈的內容相比,我獲得了404而不是500. –
好的 - 然後更改URL並嘗試更改我提出的代碼 - 會發生什麼? – duncanhall
對不起,沒有注意到有其他建議會嘗試。 –