'In a connexion/flask app, how can I fake post requests with parameters in body for endpoint?
I'm trying to test my connexion app, I've followed the link here,
And the get requests seem to be working fine. Now I'm trying to test post requests, but the endpoint method takes in parameters as arguments. My set up is very similar to the one linked above, and I followed the advice listed here, but it doesn't seem to work. In my tests I have tried something like:
response = client.post('/path-to-request', data=dict(var1='data1', var2='data2'))
Where client is the same as the one defined by ksindi here. I'll have this path mapped to a python method in my swagger file and want to retrieve the data packaged in the post as the argument to that method. Example snippet in a swaggerfile:
paths:
...
/models:
post:
operationId: bar.foo
...
And then in bar.py I'll have a method foo:
foo(data_here):
<code>
The attempt I mentioned above seems to not work, I will get through to the method and the code will execute but "data_here" will be None, I want it to be the dict(var1='data1', var2='data2') that is packaged with the post request. Any help is appreciated thanks!
Solution 1:[1]
Okay so I think I figured it out. Reading the answer here by Martjin, I was able to get it to work by doing the following:
response = client.post('/models', data=json.dumps(job), headers=headers)
Solution 2:[2]
@pytest.fixture
def app():
app = App(__name__, specification_dir=SWAGGER_PATH)
app.app.json_encoder = JSONEncoder
app.add_api("swagger.yaml")
app_client = app.app.test_client()
return app_client
def test_post_foo(app) -> None:
"""
:expect:201
"""
response = app.post("/api/all",
content_type='application/json',
data=json.dumps(foo_data))
assert response.status_code == 201
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dagda88 |
| Solution 2 |
