简体   繁体   中英

Robot Framework: send binary data in POST request body with

I have a problem with getting my test running using Robot Framework and robotframework-requests . I need to send a POST request and a binary data in the body. I looked at this question already, but it's not really answered. Here's how my test case looks like:

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}

The problem is that my binary data is about 250MB. When the data is read with Get Binary File I see that memory consumption goes up to 2.x GB. A few seconds later when the Post Request is triggered my test is killed by OOM. I already looked at files parameter, but it seems it uses multipart encoding upload, which is not what I need.

My other thought was about passing open file handler directly to underlying requests library, but I guess that would require robotframework-request modification. Another idea is to fall back to curl for this test only.

Am I missing something in my test? What is the better way to address this?

I proceeded with the idea of robotframework-request modification and added this method

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

I guess I can improve it a bit and create a pull request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM