Help unit testing v3 multipart/form-data with "Missing boundary in multipart" #1964
-
I'm moving from connexion 2 to connexion 3 ( In v2 unit-testing (which uses The OpenAPI endpoint body definitionThis is the endpoint body definition for a POST at asset_post_body:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
name:
type: string
maxLength: 80
pattern: ^[a-z0-9-]{1,63}$
content_file:
type: string
format: binary
content_string:
type: string
scope:
type: string
enum:
- USER
- PRODUCT
- UNIT
- ORGANISATION
- GLOBAL
scope_id:
type: string
secret:
type: boolean
required:
- name
- scope
- secret The unit testHere an excerpt of a test using the above endpoint. The key elements here are the
HEADERS_FORM = {'Accept': 'application/json',
'Content-Type': 'multipart/form-data'}
def test_create_global_secret_asset(client):
# Arrange
data = {'name': 'test-asset',
'content_string': 'secret',
'scope': 'GLOBAL',
'secret': True}
# Act
response = client.post(Config.API_BASE_PATH + '/asset', headers=HEADERS_FORM, data=data)
# Assert
assert response.status_code == 201
r_data = response.json()
assert len(r_data) == 1
assert 'id' in r_data What happens?Although this is modified for the v3 response (where
But, when it's run I get a KeyError from the underlying
So there's a missing boundary. Fine - but why is that my problem? In the v2 implementation's Swagger the generated cURL looks like the following. It does not define a boundary either (here the actual URL replaced by curl -X 'POST' \
'https://[...]/asset' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'content_string=secret' \
-F 'name=test-asset' \
-F 'scope=GLOBAL' \
-F 'secret=true' |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Have not been able to get past this - and I am certainly not going to introduce the boundary strings in my test code. As a work around (which works for me) I have switched the API content from As I said, that is not fixing the problem - I would still like to know how to use the connexion test_client() to drive a |
Beta Was this translation helpful? Give feedback.
-
Hi @alanbchristie, The boundary parameter is required in the Eg. headers={"Content-Type": b"multipart/form-data; boundary=-"} Usually If really needed, you can force the test client to send data as res = app_client.post(
"/v1.0/multipart_form_json",
files={"file": b""}, # Force multipart/form-data content-type
data={"x": json.dumps({"name": "joe", "age": 20})},
) |
Beta Was this translation helpful? Give feedback.
Hi @alanbchristie,
The boundary parameter is required in the
Content-Type
header formultipart/form-data
Eg.
Usually
multipart/form-data
is only used when your form includes files, otherwise it is indeed recommended to useapplication/x-www-form-urlencoded
.If really needed, you can force the test client to send data as
multipart/form-data
as follows: