From 552b206521b9690b611136e0000b9a41507675eb Mon Sep 17 00:00:00 2001 From: Vyacheslav Yurkov Date: Thu, 5 Mar 2020 11:23:56 +0100 Subject: [PATCH] Added Get File Descriptor keyword 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 --- src/RequestsLibrary/RequestsKeywords.py | 7 ++++++- src/RequestsLibrary/log.py | 7 ++++++- tests/base64Decode.py | 7 +++++++ tests/testcase.robot | 13 +++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/base64Decode.py diff --git a/src/RequestsLibrary/RequestsKeywords.py b/src/RequestsLibrary/RequestsKeywords.py index d74d72c7..16a11bcd 100644 --- a/src/RequestsLibrary/RequestsKeywords.py +++ b/src/RequestsLibrary/RequestsKeywords.py @@ -1,6 +1,7 @@ import json import copy import sys +import io import requests from requests.models import Response @@ -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 @@ -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): """ diff --git a/src/RequestsLibrary/log.py b/src/RequestsLibrary/log.py index 12755ee5..f931d099 100644 --- a/src/RequestsLibrary/log.py +++ b/src/RequestsLibrary/log.py @@ -1,3 +1,4 @@ +import io from robot.api import logger from RequestsLibrary import utils @@ -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 diff --git a/tests/base64Decode.py b/tests/base64Decode.py new file mode 100644 index 00000000..fcb5d049 --- /dev/null +++ b/tests/base64Decode.py @@ -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," + # Therefore we strip the headers and decode the actual data + return base64.b64decode(s.split(',')[1]) diff --git a/tests/testcase.robot b/tests/testcase.robot index 3b97a641..4a79cb8d 100644 --- a/tests/testcase.robot +++ b/tests/testcase.robot @@ -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 @@ -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