lundi 24 février 2020

how to get payload from flask test_client?

How do i send a payload using post on flask test_client?

The fixture with test_client

@pytest.fixture
def test_client():
    app = create_app(configuration_name="testing")
    return app.test_client()

The payload that i'm trying to send

@pytest.fixture
def selection_payload():
    return {
        'name': 'random-selection-name',
        'description': 'mock-selection-description',
        'products_skus': ['123', '456']
    }

The test:

def test_can_create_selection(self, test_client, selection_payload):
    response = test_client.post(
        self.ENDPOINT,
        data=selection_payload
    )

    assert response.status_code == HTTPStatus.CREATED

When i try to access the payload using api.payloadthe value is None, i also try importing the request.get_json() from flask

def post(self):
    try:
        selection = Selection(**api.payload) # api.payload is None
        result = selection.save()
    except NotUniqueError:
        api.abort(HTTPStatus.CONFLICT, 'code already exist.')

    return result

Aucun commentaire:

Enregistrer un commentaire