我正在尝试对烧瓶应用程序进行单元测试,在该应用程序中上传文件(或多个文件),这是到目前为止的内容,但是我尝试过多种发布文件的方法
file_storage = FileStorage(stream=f, filename='replays')
response = self.context.post('/api/upload', data={'files':(file_storage, 'replays')},
无论我做什么请求。在表单填充有我需要的数据时,文件始终为空。
我无法使用request.post,因为我需要对正在测试的呼叫进行同步访问。
答案 0 :(得分:0)
如果还有其他人遇到此问题。这是解决方案
# create your request like you normally would for upload
r = Request('POST', LOCAL_URL + '/api/upload',
files={'replays': ('filename', io.Bytes(file))})
# build it
prepped = r.prepare()
# extract data needed for content
content_data = list(prepped.headers._store.items())
content_length = content_data[0][1][1]
content_type = content_data[1][1][1]
# add the body as an input stream and use the existing values
response = self.context.post('/api/upload',
input_stream=io.BytesIO(prepped.body),
content_length=content_length,
content_type=content_type)