Skip to content

Commit

Permalink
Added Get File Descriptor keyword
Browse files Browse the repository at this point in the history
A new keyword allow to pass file descriptor to python requests,
which allows to read a binary file directly without loading it into memory

Signed-off-by: Vyacheslav Yurkov <Vyacheslav.Yurkov@bruker.com>
  • Loading branch information
UVV-gh committed Mar 5, 2020
1 parent ad89c73 commit 552b206
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/RequestsLibrary/RequestsKeywords.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import copy
import sys
import io

import requests
from requests.models import Response
Expand Down Expand Up @@ -668,7 +669,7 @@ def post_request(
``timeout`` connection timeout
"""
session = self._cache.switch(alias)
if not files:
if not files and not isinstance(data, io.IOBase):
data = utils.format_data_according_to_header(session, data, headers)
redir = True if allow_redirects is None else allow_redirects

Expand Down Expand Up @@ -945,6 +946,10 @@ def _common_request(

return resp

@staticmethod
def get_file_descriptor(path):
return open(path, 'rb')

@staticmethod
def _check_status(expected_status, resp, msg=None):
"""
Expand Down
7 changes: 6 additions & 1 deletion src/RequestsLibrary/log.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
from robot.api import logger

from RequestsLibrary import utils
Expand Down Expand Up @@ -28,9 +29,13 @@ def log_request(
args.pop('session', None)
# This will log specific headers merged with session defined headers
merged_headers = utils.merge_headers(session, args.pop('headers', None))
formatted_data = utils.format_data_to_log_string_according_to_headers(session,
if not isinstance(args['data'], io.IOBase):
formatted_data = utils.format_data_to_log_string_according_to_headers(session,
args.pop('data', None),
merged_headers)
else:
formatted_data = ""

formatted_json = args.pop('json', None)
method_log = '%s Request using : ' % method.upper()
uri_log = 'uri=%s' % uri
Expand Down
7 changes: 7 additions & 0 deletions tests/base64Decode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import base64

def base64_decode_data(s):
# We expect base64 string retrieved from json.data(), which also includes
# MIME headers in a form "data:application/octet-stream;base64,<ACTUAL BASE64 DATA>"
# Therefore we strip the headers and decode the actual data
return base64.b64decode(s.split(',')[1])
13 changes: 13 additions & 0 deletions tests/testcase.robot
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Library String
Library ../src/RequestsLibrary/RequestsKeywords.py
Library OperatingSystem
Library customAuthenticator.py
Library base64Decode.py
Resource res_setup.robot

Suite Setup Setup Flask Http Server
Expand Down Expand Up @@ -194,6 +195,18 @@ Post Request With Arbitrary Binary Data
${resp}= Post Request httpbin /post data=${data} headers=&{headers}
# TODO Compare binaries. Content is json with base64 encoded data
Log "Success"
${receivedData}= Base64 Decode Data ${resp.json()['data']}
Should Be Equal ${receivedData} ${data}

Post Request With File Descriptor
[Tags] post
Create Session httpbin http://httpbin.org debug=3
${handle}= Get File Descriptor ${CURDIR}${/}randombytes.bin
&{headers}= Create Dictionary Content-Type=application/octet-stream Accept=application/octet-stream
${resp}= Post Request httpbin /post data=${handle} headers=&{headers}
${receivedData}= Base64 Decode Data ${resp.json()['data']}
${data}= Get Binary File ${CURDIR}${/}randombytes.bin
Should Be Equal ${receivedData} ${data}

Post Request With File
[Tags] post
Expand Down

0 comments on commit 552b206

Please sign in to comment.