INQUIRY: upload multipart form file using external functions
roaasallam opened this issue · 1 comments
roaasallam commented
I have an endpoint which accepts multipart form data,
I have wrote python function to write random content in a csv file,
Is there a way to pass this file in the request data using the python function that returns the file?
thanks,
`
request:
url: "{tavern.env_vars.TARGET_HOST}/file-upload"
method: POST
timeout: 10.0
headers:
Authorization: "Bearer {token}"
Content-Type: "multipart/form-data; boundary=654249085280325934841628"
files:
file: "tests/acceptance/testdata/auto-written-file-upload.csv"
data:
# file: [ auto-written-file-upload.csv ] # what we do normally
file: [ testing_utils:read_from_file(filename=auto-written-file-upload.csv) ] # trying to get the file via python function
response:
status_code: [204]
`
michaelboulton commented
If I understand the question correctly, I think this should work if you make your function return a dictionary with the file
key like
def read_from_file(filename):
return {
"file": ...
}
And use it in the test like
files:
file: "tests/acceptance/testdata/auto-written-file-upload.csv"
data:
$ext:
function: testing_utils:read_from_file
kwargs:
filename: auto-written-file-upload.csv
Basically you can't have a $ext
function anywhere except the top level of a 'data' json' etc. block