Pact-python : Consumer pacts for POST calls are failing

690 views Asked by At

I am trying to create consumer side pacts for a POST end point using the pact-python library. But it's failing with the error saying "Missing requests".

Here't the client code which makes the POST API call

def create_user(request):
     return requests.post("http://localhost:1234/user", data=request).json()

Here's my test class which create the consumer pacts.

class TestUserConsumer(unittest.TestCase):

def test_user_creation(self):
    request = {
        "name": "Micky",
        "age": 0
    }
    response = {
        "id": 1232,
        "name": "Micky",
        "age": 0
    }
    pact = Consumer("user_client").has_pact_with(Provider("user_server"))
    pact.start_service()
    pact.with_request(
        method='post',
        path='/user',
        body=request
    ).will_respond_with(status=200, body=response)
    with pact:
        create_user(request)
        pact.verify()

    pact.stop_service()

The test failed with the following error.

line 268, in verify
assert resp.status_code == 200, resp.text
AssertionError: Actual interactions do not match expected interactions for mock MockService.

Missing requests:
   POST /user

The create_user(request) is getting executed, but still the interactions are not recorded on the pact mock server.

Note : The GET API pact creations are working. Only the POSTs are failing.

Appreciate the help.

1

There are 1 answers

0
Sree On

I figured out the problem. I was not converting my dictionary to json before making the request. Hence the request body format was incorrectly send. This caused a failure on the mock server while verifying the pact.

I also noticed that, the logs didn't get generated initially. This was due to my assertions added before stopping the server. Since the assertions got failed, the pact mock server didn't get stopped. Hence the logs are not generated at all. Once I stopped the server, the logs got added and that helped me to identify the problem.