可以在axios中模拟上传大文件的HTTP请求而不选择文件吗?

huangapple go评论64阅读模式
英文:

Is it possible to simulate a big file upload HTTP request without selecting a file in axios?

问题

我有这个需求,需要测试文件大小限制,但目前是手动操作,因为文件太大,无法包含在CI/CD中。

我想知道是否有可能使用Axios模拟大文件上传(例如使用愚蠢的随机字节)?

有人曾经这样做过吗?

英文:

I have this requirement where I need to test file size limit and right now it's manual, because files are too big to be included in the CI/CD.

I wonder if it's possible for me to simulate a big file upload (using stupid random bytes for example) with Axios?

Has anyone ever done it?

答案1

得分: 2

你可以尝试使用File()构造函数来创建一个伪造的“文件”。

您可以将多种不同的数据类型传递给它,也许最好从数组中的字符串值开始 - 保持字符串长度不超过允许的最大长度,并根据需要添加足够多的字符串以达到所需的最终文件大小。

类似于

new File(["0123456789".repeat(10000000)], "name")

通过向数组中添加更多这样的字符串来“填满”(大约)所需的大小。

英文:

You could try and create a fake "file" using the File() constructor.

You can feed that with multiple different data types, it would perhaps make sense to start with string values in an array here - keeping the strings under the maximum allowed length, and adding as many as you need to reach your final required file size.

Something like

new File(["0123456789".repeat(10000000)], "name")

"filled up" to (about) the desired size by adding more such strings to the array.

huangapple
  • 本文由 发表于 2023年6月5日 19:28:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定