如何通过 pact-python 模拟 https 服务器,你能举个例子吗
How to mock a https server by pact-python, could you give me a example
我无法根据API模拟https服务,请给我一些建议
python版本:3.6.5
契约-python: 1.0
pytest:5.3.5
platform:windows
错误信息:
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='localhost', port=1234): Max retries exceeded with url: / (由 SSLError(SSLError("bad handshake: Error([('SSL rou
尖齿', 'tls_process_server_certificate', 'certificate verify failed')],)",),))
进程:
我尝试通过 pact-python 模拟一个 https 服务器,所以我设置了 ssl=True 而没有设置 sslcert 和 sslkey,它不能工作。然后我尝试创建一个自签名的 sslcert 和 sslkey 使用 openssl 工具然后设置选项: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)
我认为可能是 Pact Python 试图查看服务器是否启动。这可能是一个错误,如果您可以可靠地重现错误,请分享该代码并提出问题。
FWIW 通过 https 使用 Pact 测试通常是毫无意义的 IMO。 Pact 测试旨在测试契约,HTTP 的 s
部分对此没有影响。
我无法根据API模拟https服务,请给我一些建议
python版本:3.6.5 契约-python: 1.0 pytest:5.3.5 platform:windows
错误信息: urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='localhost', port=1234): Max retries exceeded with url: / (由 SSLError(SSLError("bad handshake: Error([('SSL rou 尖齿', 'tls_process_server_certificate', 'certificate verify failed')],)",),))
进程:
我尝试通过 pact-python 模拟一个 https 服务器,所以我设置了 ssl=True 而没有设置 sslcert 和 sslkey,它不能工作。然后我尝试创建一个自签名的 sslcert 和 sslkey 使用 openssl 工具然后设置选项: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)
我认为可能是 Pact Python 试图查看服务器是否启动。这可能是一个错误,如果您可以可靠地重现错误,请分享该代码并提出问题。
FWIW 通过 https 使用 Pact 测试通常是毫无意义的 IMO。 Pact 测试旨在测试契约,HTTP 的 s
部分对此没有影响。