reecetech / pactman

Pact management (mocking, generation and verification)
Other
91 stars 37 forks source link

received but no interaction registered #73

Open DannyCork opened 5 years ago

DannyCork commented 5 years ago

I'm having an issue when attempting to make a request to the mock server. The mock server returns a 500 status code and a message 'received but no interaction registered'

My code looks like this:

import atexit
from pactman import Consumer, Provider
import requests

pact = Consumer('Consumer').has_pact_with(Provider('Provider'), use_mocking_server=True)
pact.start_mocking()
print('server on {}'.format(pact.uri))
atexit.register(pact.stop_mocking)

def testabc():
    path = '/test/123'
    pact.given(
        'blah'
    ).upon_receiving(
        'a GET request'
    ).with_request(
        method='GET',
        path='/test/123'
    ).will_respond_with(200, body={'status': 'FAILED'})

    response = requests.get(pact.uri+path)
    print(response.status_code)
    print(response.text)
    assert (response.status_code == 200)

I then run it using

$pytest test2.py

and get

Loading pacts from None
rootdir: /home/xxxx/xxx/xxxx-xxxx-xxxx/tests/contract_tests
plugins: pactman-2.25.0
collecting ... server on http://localhost:8155
collected 1 item                                                                                                                                                                                                  

test2.py::testabc 500
Request at /test/123 received but no interaction registered
FAILED

==================================================================================================== FAILURES =====================================================================================================
_____________________________________________________________________________________________________ testabc _____________________________________________________________________________________________________

    def testabc():
        path = '/test/123'
        pact.given(
            'blah'
        ).upon_receiving(
            'a GET request'
        ).with_request(
            method='GET',
            path='/test/123'
        ).will_respond_with(200, body={'status': 'FAILED'})
        response = requests.get(pact.uri+path)
        print(response.status_code)
        print(response.text)
>       assert (response.status_code == 200)
E       assert 500 == 200
E         -500
E         +200

test2.py:24: AssertionError

I have no additional code. My understanding is that pact provides the mocking, is this correct?

Any help appreciated. Thanks.

r1chardj0n3s commented 4 years ago

Sorry for the delay, Danny. Is this still an issue?

Mukheem commented 4 years ago

Hello @r1chardj0n3s ,

This is still an issue which is observed for my code. `import atexit import unittest import requests from pactman import Consumer, Provider, Term, Like from pactman.verifier.pytest_plugin import pact_verifier pact = Consumer('ConsumerPOC').has_pact_with(Provider('Provider'), use_mocking_server=True, pact_dir="C:\Users\*****\PycharmProjects\pact-python-master") pact.start_mocking() atexit.register(pact.stop_mocking)

class GetUserInfoContract(unittest.TestCase):

def test_generate_pact_from_consumer_end(self):
    # Setting up Expected Response
    expected = {'skip': 0, 'limit': 150}
    # Simulating an Real HTTP Server
    pact.given(
        'User ML exists and is not an administrator'
    ).upon_receiving(
        'a request for ML'
    ).with_request(
        method='GET',
        path='/api',
    ).will_respond_with(200, body=expected)

    with pact:
        result = requests.get(pact.uri + '/api')

    self.assertEqual(result.json(), expected)`

When the above code is run in debug mode, I found the error 'Received but no interaction registered'. I executed requests.get(pact.uri + '/api') explicitly during debugging and then I got to see this issue

When it is run in a single shot I am seeing below error. requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8155): Max retries exceeded with url: /api (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x04595310>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

Any help is appreciated !!!

Thanks.

r1chardj0n3s commented 4 years ago

"No connection could be made because the target machine actively refused it" looks like some sort of firewall configuration issue?

Mukheem commented 4 years ago

But @r1chardj0n3s , If I use the below code also I am able to generate PACT.

pact = Consumer('ConsumerPOC').has_pact_with(Provider('Provider'),
use_mocking_server=False, pact_dir="C:\Users\*****\PycharmProjects\pact-python-master")

# pact.start_mocking()
# atexit.register(pact.stop_mocking)

Observe, I have commented the lines Start server and Stop server also by giving the flag value use_mocking_server=False

Then what is the actual need of previous code where in we are starting and stopping the mock server ??

r1chardj0n3s commented 4 years ago

Yep! You don't need to run a mocking server to get the benefits of pact mocking - it's included mostly for legacy and edge case needs.

Mukheem commented 4 years ago

Can you help me in understanding what do you mean by edge case needs... !!!

Any Example which can help me understand the concept more clear.

r1chardj0n3s commented 4 years ago

Sorry, I can't actually think of one right now. If you don't need it, then don't use it.

Mukheem commented 4 years ago

Okay @r1chardj0n3s .