How to mock a https server by pact-python, could you give me a example

510 views Asked by At

I can't mock a https serve according to API,please give me some advice

python version: 3.6.5 pact-python: 1.0 pytest:5.3.5 platform:windows

Error message: urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='localhost', port=1234): Max retries exceeded with url: / (Caused by SSLError(SSLError("bad handshake: Error([('SSL rou tines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))

process:

I try to mock a https server by pact-python so I set ssl=True and don't set sslcert and sslkey, it can't work. Then I try to creat a self-signed sslcert and sslkey use openssl tools then set option: sslcert = 'server.crt', sslkey = 'server.key',it still not work. It's a very simple example, I just wanna mock a https server:

import requests
import atexit
import pytest

from pact import Consumer, Provider


def user(user_name):
    """Fetch a user object by user_name from the server."""
    uri = 'https://localhost:1234/users/' + user_name
    return requests.get(uri, verify = False).json()


pact = Consumer('Consumer').has_pact_with(Provider('Provider'), port=1234, ssl = True,
                                          sslcert = 'server.crt', sslkey = 'server.key')
pact.start_service()
atexit.register(pact.stop_service)


def test_get_user():
    expected = {
        'username': 'UserA',
        'id': 123,
        'groups': ['Editors']
    }

    (pact
     .given('UserA exists and is not an administrator')
     .upon_receiving('a request for UserA')
     .with_request('get', '/users/UserA')
     .will_respond_with(200, body = expected))

    with pact:
        result = user('UserA')

    assert(result, expected)
1

There are 1 answers

2
Matthew Fellows On BEST ANSWER

I think it might be the Pact Python trying to see if the server has come up. It might be a bug, if you can reproduce the bug reliably, please share that code and raise an issue.

FWIW using Pact tests with https is usually pointless IMO. Pact tests are designed to tests the contract, and the s part of HTTP makes no difference to this.