def test_provider_state_uses_custom_headers(monkeypatch, mock_pact, mock_result_factory, fake_interaction): fake_interaction["request"]["headers"] = dict(request_header="value") monkeypatch.setattr(requests, "post", Mock(return_value=Mock(status_code=200))) monkeypatch.setattr(requests, "get", Mock()) monkeypatch.setattr(ResponseVerifier, "verify", Mock()) fake_interaction["providerStates"] = "some state" i = Interaction(mock_pact("2.0.0"), fake_interaction, mock_result_factory) i.verify( "http://provider.example/", "http://provider.example/pact-setup/", extra_provider_headers={"some_header": "some_header"}, ) requests.post.assert_called_with( "http://provider.example/pact-setup/", json={ "provider": "SpamProvider", "consumer": "SpamConsumer", "states": "some state" }, headers={"some_header": "some_header"}, ) requests.get.assert_called_with( "http://provider.example/users-service/user/alex", headers={ "request_header": "value", "some_header": "some_header" }, ) i.response.verify.assert_called()
def test_provider_state_uses_custom_headers(monkeypatch, mock_pact, mock_result_factory, fake_interaction): fake_interaction['request']['headers'] = dict(request_header='value') monkeypatch.setattr(requests, 'post', Mock(return_value=Mock(status_code=200))) monkeypatch.setattr(requests, 'get', Mock()) monkeypatch.setattr(ResponseVerifier, 'verify', Mock()) fake_interaction['providerStates'] = 'some state' i = Interaction(mock_pact('2.0.0'), fake_interaction, mock_result_factory) i.verify('http://provider.example/', 'http://provider.example/pact-setup/', extra_provider_headers={'some_header': 'some_header'}) requests.post.assert_called_with('http://provider.example/pact-setup/', json={ 'provider': "SpamProvider", 'consumer': "SpamConsumer", 'states': "some state" }, headers={'some_header': "some_header"}) requests.get.assert_called_with( 'http://provider.example/users-service/user/alex', headers={ 'request_header': 'value', 'some_header': "some_header" }) i.response.verify.assert_called()
def test_interaction_setup_fails(monkeypatch, mock_pact, mock_result, fake_interaction, option): monkeypatch.setattr(requests, 'post', Mock(return_value=Mock(status_code=400, text='fail'))) monkeypatch.setattr(requests, 'get', Mock()) monkeypatch.setattr(ResponseVerifier, 'verify', Mock()) fake_interaction[option] = 'some state' i = Interaction(mock_pact('2.0.0'), fake_interaction, mock_result) i.verify('http://provider.example/', 'http://provider.example/pact-setup/') i.result.fail.assert_called_with("Invalid provider state 'some state'")
def test_interaction_verify_method_not_supported(monkeypatch, mock_pact, mock_result, fake_interaction): monkeypatch.setattr(requests, 'get', Mock()) monkeypatch.setattr(ResponseVerifier, 'verify', Mock()) fake_interaction['request']['method'] = 'FLEBBLE' i = Interaction(mock_pact('2.0.0'), fake_interaction, mock_result) i.verify('http://provider.example/', 'http://provider.example/pact-setup/') i.result.fail.assert_called_with("Request method FLEBBLE not implemented in verifier") i.response.verify.assert_not_called()
def test_interaction_verify_patch_unsupported_content_type(monkeypatch, mock_pact, mock_result, fake_interaction): monkeypatch.setattr(requests, 'patch', Mock()) monkeypatch.setattr(ResponseVerifier, 'verify', Mock()) fake_interaction['request']['method'] = 'PATCH' fake_interaction['request']['headers'] = {'Content-Type': 'spam/ham'} i = Interaction(mock_pact('2.0.0'), fake_interaction, mock_result) i.verify('http://provider.example/', 'http://provider.example/pact-setup/') i.result.fail.assert_called_with("PATCH content type spam/ham not implemented in verifier") requests.patch.assert_not_called()
def test_interaction_verify_get(monkeypatch, mock_pact, mock_result, fake_interaction): monkeypatch.setattr(requests, 'post', Mock()) monkeypatch.setattr(requests, 'get', Mock()) monkeypatch.setattr(ResponseVerifier, 'verify', Mock()) i = Interaction(mock_pact('2.0.0'), fake_interaction, mock_result) i.verify('http://provider.example/', 'http://provider.example/pact-setup/') requests.post.assert_not_called() requests.get.assert_called_with('http://provider.example/users-service/user/alex', headers={}) i.response.verify.assert_called()
def test_interaction_setup_fails(monkeypatch, mock_pact, mock_result_factory, fake_interaction, option): monkeypatch.setattr(requests, "post", Mock(return_value=Mock(status_code=400, text="fail"))) monkeypatch.setattr(requests, "get", Mock()) monkeypatch.setattr(ResponseVerifier, "verify", Mock()) fake_interaction[option] = "some state" i = Interaction(mock_pact("2.0.0"), fake_interaction, mock_result_factory) i.verify("http://provider.example/", "http://provider.example/pact-setup/") i.result.warn.assert_called_with("Invalid provider state 'some state'")
def test_interaction_verify_method_not_supported(monkeypatch, mock_pact, mock_result_factory, fake_interaction): monkeypatch.setattr(requests, "get", Mock()) monkeypatch.setattr(ResponseVerifier, "verify", Mock()) fake_interaction["request"]["method"] = "FLEBBLE" i = Interaction(mock_pact("2.0.0"), fake_interaction, mock_result_factory) i.verify("http://provider.example/", "http://provider.example/pact-setup/") i.result.fail.assert_called_with( "Request method FLEBBLE not implemented in verifier") i.response.verify.assert_not_called()
def test_interaction_verify_patch_unsupported_content_type( monkeypatch, mock_pact, mock_result_factory, fake_interaction): monkeypatch.setattr(requests, "patch", Mock()) monkeypatch.setattr(ResponseVerifier, "verify", Mock()) fake_interaction["request"]["method"] = "PATCH" fake_interaction["request"]["headers"] = {"Content-Type": "spam/ham"} i = Interaction(mock_pact("2.0.0"), fake_interaction, mock_result_factory) i.verify("http://provider.example/", "http://provider.example/pact-setup/") i.result.fail.assert_called_with( "PATCH content type spam/ham not implemented in verifier") requests.patch.assert_not_called()
def test_interaction_verify_get(monkeypatch, mock_pact, mock_result_factory, fake_interaction): monkeypatch.setattr(requests, "post", Mock()) monkeypatch.setattr(requests, "get", Mock()) monkeypatch.setattr(ResponseVerifier, "verify", Mock()) i = Interaction(mock_pact("2.0.0"), fake_interaction, mock_result_factory) i.verify("http://provider.example/", "http://provider.example/pact-setup/") requests.post.assert_not_called() requests.get.assert_called_with( "http://provider.example/users-service/user/alex", headers={}) i.response.verify.assert_called()
def test_interaction_sends_headers(monkeypatch, mock_pact, mock_result, fake_interaction, method): headers = {'key1': 'value1'} requests_method = Mock() monkeypatch.setattr(requests, method.lower(), requests_method) monkeypatch.setattr(ResponseVerifier, 'verify', Mock()) fake_interaction['request']['method'] = method fake_interaction['request']['body'] = 'body-data' fake_interaction['request']['headers'] = headers i = Interaction(mock_pact('2.0.0'), fake_interaction, mock_result) i.verify('http://provider.example/', 'http://provider.example/pact-setup/') request_kwargs = requests_method.call_args[1] assert request_kwargs["headers"] == headers
def test_interaction_sends_headers(monkeypatch, mock_pact, mock_result_factory, fake_interaction, method): headers = {"key1": "value1"} requests_method = Mock() monkeypatch.setattr(requests, method.lower(), requests_method) monkeypatch.setattr(ResponseVerifier, "verify", Mock()) fake_interaction["request"]["method"] = method fake_interaction["request"]["body"] = "body-data" fake_interaction["request"]["headers"] = headers i = Interaction(mock_pact("2.0.0"), fake_interaction, mock_result_factory) i.verify("http://provider.example/", "http://provider.example/pact-setup/") request_kwargs = requests_method.call_args[1] assert request_kwargs["headers"] == headers
def test_interaction_verify_delete(monkeypatch, mock_pact, mock_result): monkeypatch.setattr(requests, 'delete', Mock()) monkeypatch.setattr(ResponseVerifier, 'verify', Mock()) fake_interaction = { 'description': 'dummy', 'request': {'method': 'DELETE', 'path': '/diary-notes/diary-note/1'}, 'response': {'headers': {}, 'status': 200} } i = Interaction(mock_pact('2.0.0'), fake_interaction, mock_result) i.verify('http://provider.example/', 'http://provider.example/pact-setup/') requests.delete.assert_called_with('http://provider.example/diary-notes/diary-note/1', headers={}) i.response.verify.assert_called()
def test_interaction_verify_with_setup(monkeypatch, mock_pact, mock_result, fake_interaction, option, arg): monkeypatch.setattr(requests, 'post', Mock(return_value=Mock(status_code=200))) monkeypatch.setattr(requests, 'get', Mock()) monkeypatch.setattr(ResponseVerifier, 'verify', Mock()) fake_interaction[option] = 'some state' i = Interaction(mock_pact('2.0.0'), fake_interaction, mock_result) i.verify('http://provider.example/', 'http://provider.example/pact-setup/') requests.post.assert_called_with( 'http://provider.example/pact-setup/', json={'provider': "SpamProvider", 'consumer': "SpamConsumer", arg: "some state"} ) i.response.verify.assert_called()
def test_interaction_verify_qs(monkeypatch, mock_pact, mock_result, fake_interaction): monkeypatch.setattr(requests, 'post', Mock()) monkeypatch.setattr(requests, 'get', Mock()) monkeypatch.setattr(ResponseVerifier, 'verify', Mock()) fake_interaction['request']['query'] = 'a=b&c=d' i = Interaction(mock_pact('2.0.0'), fake_interaction, mock_result) i.verify('http://provider.example/', 'http://provider.example/pact-setup/') requests.get.assert_called_with( 'http://provider.example/users-service/user/alex', params=dict(a=['b'], c=['d']), headers={}, ) i.response.verify.assert_called()
def test_interaction_verify_qs(monkeypatch, mock_pact, mock_result_factory, fake_interaction): monkeypatch.setattr(requests, "post", Mock()) monkeypatch.setattr(requests, "get", Mock()) monkeypatch.setattr(ResponseVerifier, "verify", Mock()) fake_interaction["request"]["query"] = "a=b&c=d" i = Interaction(mock_pact("2.0.0"), fake_interaction, mock_result_factory) i.verify("http://provider.example/", "http://provider.example/pact-setup/") requests.get.assert_called_with( "http://provider.example/users-service/user/alex", params=dict(a=["b"], c=["d"]), headers={}) i.response.verify.assert_called()
def test_interaction_verify_patch(monkeypatch, mock_pact, mock_result_factory, fake_interaction): monkeypatch.setattr(requests, "patch", Mock()) monkeypatch.setattr(requests, "get", Mock()) monkeypatch.setattr(ResponseVerifier, "verify", Mock()) fake_interaction["request"]["method"] = "PATCH" fake_interaction["request"]["body"] = "spam" i = Interaction(mock_pact("2.0.0"), fake_interaction, mock_result_factory) i.verify("http://provider.example/", "http://provider.example/pact-setup/") requests.get.assert_not_called() requests.patch.assert_called_with( "http://provider.example/users-service/user/alex", json="spam", headers={}) i.response.verify.assert_called()
def test_interaction_verify_patch(monkeypatch, mock_pact, mock_result, fake_interaction): monkeypatch.setattr(requests, 'patch', Mock()) monkeypatch.setattr(requests, 'get', Mock()) monkeypatch.setattr(ResponseVerifier, 'verify', Mock()) fake_interaction['request']['method'] = 'PATCH' fake_interaction['request']['body'] = 'spam' i = Interaction(mock_pact('2.0.0'), fake_interaction, mock_result) i.verify('http://provider.example/', 'http://provider.example/pact-setup/') requests.get.assert_not_called() requests.patch.assert_called_with( 'http://provider.example/users-service/user/alex', json='spam', headers={}, ) i.response.verify.assert_called()
def test_interaction_verify_with_setup(monkeypatch, mock_pact, mock_result_factory, fake_interaction, option, arg): monkeypatch.setattr(requests, "post", Mock(return_value=Mock(status_code=200))) monkeypatch.setattr(requests, "get", Mock()) monkeypatch.setattr(ResponseVerifier, "verify", Mock()) fake_interaction[option] = "some state" i = Interaction(mock_pact("2.0.0"), fake_interaction, mock_result_factory) i.verify("http://provider.example/", "http://provider.example/pact-setup/") requests.post.assert_called_with( "http://provider.example/pact-setup/", json={ "provider": "SpamProvider", "consumer": "SpamConsumer", arg: "some state" }, ) i.response.verify.assert_called()
def test_interaction_verify_delete(monkeypatch, mock_pact, mock_result_factory): monkeypatch.setattr(requests, "delete", Mock()) monkeypatch.setattr(ResponseVerifier, "verify", Mock()) fake_interaction = { "description": "dummy", "request": { "method": "DELETE", "path": "/diary-notes/diary-note/1" }, "response": { "headers": {}, "status": 200 }, } i = Interaction(mock_pact("2.0.0"), fake_interaction, mock_result_factory) i.verify("http://provider.example/", "http://provider.example/pact-setup/") requests.delete.assert_called_with( "http://provider.example/diary-notes/diary-note/1", headers={}) i.response.verify.assert_called()