Robot Framework:在 POST 请求正文中发送二进制数据
Robot Framework: send binary data in POST request body with
我在使用 Robot Framework 和 robotframework-requests. I need to send a POST request and a binary data in the body. I looked at this question 进行测试 运行 时遇到问题,但没有得到真正的解答。这是我的测试用例的样子:
Upload ${filename} file
Create Session mysession http://${ADDRESS}
${data} = Get Binary File ${filename}
&{headers} = Create Dictionary Content-Type=application/octet-stream Accept=application/octet-stream
${resp} = Post Request mysession ${CGIPath} data=${data} headers=&{headers}
[Return] ${resp.status_code} ${resp.text}
问题是我的二进制数据大约有 250MB。当使用 Get Binary File
读取数据时,我发现内存消耗高达 2.x GB。几秒钟后,当 Post Request
被触发时,我的测试被 OOM 杀死。我已经看过 files
参数,但它似乎使用了分段编码上传,这不是我需要的。
我的另一个想法是将打开的文件处理程序直接传递给底层请求库,但我想这需要修改 robotframework-request。另一个想法是仅针对此测试回退到 curl。
我是不是在测试中遗漏了什么?解决这个问题的更好方法是什么?
我接着robotframework-request修改的思路,加上了这个方法
def post_request_binary(
self,
alias,
uri,
path=None,
params=None,
headers=None,
allow_redirects=None,
timeout=None):
session = self._cache.switch(alias)
redir = True if allow_redirects is None else allow_redirects
self._capture_output()
method_name = "post"
method = getattr(session, method_name)
with open(path, 'rb') as f:
resp = method(self._get_url(session, uri),
data=f,
params=self._utf8_urlencode(params),
headers=headers,
allow_redirects=allow_redirects,
timeout=self._get_timeout(timeout),
cookies=self.cookies,
verify=self.verify)
self._print_debug()
# Store the last session object
session.last_resp = resp
self.builtin.log(method_name + ' response: ' + resp.text, 'DEBUG')
return resp
我想我可以稍微改进一下并创建一个拉取请求。
我在使用 Robot Framework 和 robotframework-requests. I need to send a POST request and a binary data in the body. I looked at this question 进行测试 运行 时遇到问题,但没有得到真正的解答。这是我的测试用例的样子:
Upload ${filename} file
Create Session mysession http://${ADDRESS}
${data} = Get Binary File ${filename}
&{headers} = Create Dictionary Content-Type=application/octet-stream Accept=application/octet-stream
${resp} = Post Request mysession ${CGIPath} data=${data} headers=&{headers}
[Return] ${resp.status_code} ${resp.text}
问题是我的二进制数据大约有 250MB。当使用 Get Binary File
读取数据时,我发现内存消耗高达 2.x GB。几秒钟后,当 Post Request
被触发时,我的测试被 OOM 杀死。我已经看过 files
参数,但它似乎使用了分段编码上传,这不是我需要的。
我的另一个想法是将打开的文件处理程序直接传递给底层请求库,但我想这需要修改 robotframework-request。另一个想法是仅针对此测试回退到 curl。
我是不是在测试中遗漏了什么?解决这个问题的更好方法是什么?
我接着robotframework-request修改的思路,加上了这个方法
def post_request_binary(
self,
alias,
uri,
path=None,
params=None,
headers=None,
allow_redirects=None,
timeout=None):
session = self._cache.switch(alias)
redir = True if allow_redirects is None else allow_redirects
self._capture_output()
method_name = "post"
method = getattr(session, method_name)
with open(path, 'rb') as f:
resp = method(self._get_url(session, uri),
data=f,
params=self._utf8_urlencode(params),
headers=headers,
allow_redirects=allow_redirects,
timeout=self._get_timeout(timeout),
cookies=self.cookies,
verify=self.verify)
self._print_debug()
# Store the last session object
session.last_resp = resp
self.builtin.log(method_name + ' response: ' + resp.text, 'DEBUG')
return resp
我想我可以稍微改进一下并创建一个拉取请求。