From 0fadd9f2d7f0d9799536e76f014e1fd736f3308c Mon Sep 17 00:00:00 2001 From: Thomas Vanhaniemi Date: Wed, 9 Oct 2024 12:34:47 +0300 Subject: [PATCH] feat: Add support for uploading files as a list Uploading a list of files with the same key is supported by the Requests library. This allows the receiving server to accept a list of files for the same key, i.e. accepting any number of files. Issue: #401 --- atests/http_server/helpers.py | 19 +++-- atests/test_post_multipart.robot | 30 ++++++- doc/RequestsLibrary.html | 104 +++++++++++++++--------- src/RequestsLibrary/RequestsKeywords.py | 28 +++++-- src/RequestsLibrary/utils.py | 2 + 5 files changed, 129 insertions(+), 54 deletions(-) diff --git a/atests/http_server/helpers.py b/atests/http_server/helpers.py index 0c1b5d6..ce5e82a 100644 --- a/atests/http_server/helpers.py +++ b/atests/http_server/helpers.py @@ -110,15 +110,16 @@ def get_files(): files = dict() - for k, v in request.files.items(): - content_type = request.files[k].content_type or 'application/octet-stream' - val = json_safe(v.read(), content_type) - if files.get(k): - if not isinstance(files[k], list): - files[k] = [files[k]] - files[k].append(val) - else: - files[k] = val + for k in request.files.keys(): + for v in request.files.getlist(k): + content_type = v.content_type or 'application/octet-stream' + val = json_safe(v.read(), content_type) + if files.get(k): + if not isinstance(files[k], list): + files[k] = [files[k]] + files[k].append(val) + else: + files[k] = val return files diff --git a/atests/test_post_multipart.robot b/atests/test_post_multipart.robot index da3abef..7b3b094 100644 --- a/atests/test_post_multipart.robot +++ b/atests/test_post_multipart.robot @@ -3,14 +3,42 @@ Library RequestsLibrary *** Test Cases *** -Test Post On Session Multipart +Test Post Dictionary On Session Multipart ${file_1}= Get File For Streaming Upload atests/randombytes.bin ${file_2}= Get File For Streaming Upload atests/randombytes.bin ${files}= Create Dictionary randombytes1 ${file_1} randombytes2 ${file_2} + Should Not Be True ${file_1.closed} + Should Not Be True ${file_2.closed} + ${resp}= POST On Session ${GLOBAL_SESSION} /anything files=${files} + Should Be True ${file_1.closed} + Should Be True ${file_2.closed} + Should Contain ${resp.json()}[headers][Content-Type] multipart/form-data; boundary= Should Contain ${resp.json()}[headers][Content-Length] 480 Should Contain ${resp.json()}[files] randombytes1 Should Contain ${resp.json()}[files] randombytes2 + +Test Post List On Session Multipart + ${file_1}= Get File For Streaming Upload atests/randombytes.bin + ${file_2}= Get File For Streaming Upload atests/randombytes.bin + ${file_1_tuple}= Create List file1.bin ${file_1} + ${file_2_tuple}= Create List file2.bin ${file_2} + ${file_1_upload}= Create List randombytes ${file_1_tuple} + ${file_2_upload}= Create List randombytes ${file_2_tuple} + ${files}= Create List ${file_1_upload} ${file_2_upload} + + Should Not Be True ${file_1.closed} + Should Not Be True ${file_2.closed} + + ${resp}= POST On Session ${GLOBAL_SESSION} /anything files=${files} + + Should Be True ${file_1.closed} + Should Be True ${file_2.closed} + + Should Contain ${resp.json()}[headers][Content-Type] multipart/form-data; boundary= + Should Contain ${resp.json()}[headers][Content-Length] 466 + Should Contain ${resp.json()}[files] randombytes + Length Should Be ${resp.json()}[files][randombytes] 2 diff --git a/doc/RequestsLibrary.html b/doc/RequestsLibrary.html index 0e0239a..ec9d136 100644 --- a/doc/RequestsLibrary.html +++ b/doc/RequestsLibrary.html @@ -6,9 +6,9 @@ - + - - - - -