我无法根据 API 模拟 https 服务,请给我一些建议
python版本:3.6.5 pact-python:1.0 pytest:5.3.5平台: windows
错误消息: 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', '证书验证失败')],)",),))
过程:
我尝试通过 pact-python 模拟 https 服务器,所以我设置 ssl=True 并且不设置 sslcert 和 sslkey,它无法工作。然后我尝试使用openssl工具创建一个自签名的sslcert和sslkey,然后设置选项:sslcert ='server.crt',sslkey ='server.key',它仍然不起作用。这是一个非常简单的例子,我只想模拟一个 https 服务器:
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)