def test_matcher_in_query(): target = Request('GET', '/test-path', query={ 'q': [Like('spam')], 'l': [Term(r'\d+', '10')] }) assert target.json('3.0.0') == { 'method': 'GET', 'path': '/test-path', 'query': { 'q': ['spam'], 'l': ['10'] }, 'matchingRules': { 'query': { 'q': { 'matchers': [ { 'match': 'type' }, ] }, 'l': { 'matchers': [{ 'match': 'regex', 'regex': r'\d+', }] }, } } }
def test_matcher_in_query(): target = Request("GET", "/test-path", query={ "q": [Like("spam")], "l": [Term(r"\d+", "10")] }) assert target.json("3.0.0") == { "method": "GET", "path": "/test-path", "query": { "q": ["spam"], "l": ["10"] }, "matchingRules": { "query": { "q": { "matchers": [{ "match": "type" }] }, "l": { "matchers": [{ "match": "regex", "regex": r"\d+" }] }, } }, }
def test_regex_passes(): pact = (Pact("Consumer", "Provider", file_write_mode="never").given("spam").with_request( "GET", "/", headers={ "Spam": Term(r"\w+", "spam") }).will_respond_with(200)) with pact: requests.get(pact.uri, headers={"Spam": "ham"})
def test_matcher_in_path_gets_converted(): target = Request("GET", "/", headers={"Spam": Term(r"\w+", "spam")}) assert target.json("3.0.0") == { "method": "GET", "path": "/", "headers": {"Spam": "spam"}, "matchingRules": {"header": {"Spam": {"matchers": [{"match": "regex", "regex": "\\w+"}]}}}, }
def test_regex_fails(): pact = (Pact("Consumer", "Provider", file_write_mode="never").given("spam").with_request( "GET", "/", headers={ "Spam": Term(r"\w+", "spam") }).will_respond_with(200)) with pact: with pytest.raises(AssertionError): requests.get(pact.uri, headers={"Spam": "!@#$"})
def test_regex(): assert Term('[a-zA-Z]', 'abcXYZ').ruby_protocol() == { 'json_class': 'Pact::Term', 'data': { 'matcher': { 'json_class': 'Regexp', 's': '[a-zA-Z]', 'o': 0}, 'generate': 'abcXYZ'} }
def test_matcher_in_path_gets_converted(): target = Request("GET", Term(r"\/.+", "/test-path")) assert target.json("2.0.0") == { "method": "GET", "path": "/test-path", "matchingRules": { "$.path": { "regex": r"\/.+" } }, }
def test_matcher_in_path_gets_converted(): target = Request('GET', Term(r'\/.+', '/test-path')) assert target.json('2.0.0') == { 'method': 'GET', 'path': '/test-path', 'matchingRules': { '$.path': { 'regex': r'\/.+' } } }
def test_regex(): assert Term("[a-zA-Z]", "abcXYZ").ruby_protocol() == { "json_class": "Pact::Term", "data": { "matcher": { "json_class": "Regexp", "s": "[a-zA-Z]", "o": 0 }, "generate": "abcXYZ", }, }
def test_matcher_in_path_gets_converted(self): target = Request('GET', Term(r'\/.+', '/test-path')) result = target.json('2.0.0') self.assertEqual( result, { 'method': 'GET', 'path': '/test-path', 'matchingRules': { '$.path': { 'regex': r'\/.+' } } })
def test_nested_matchers(): matcher = EachLike({"username": Term("[a-z]+", "user"), "id": Like(123)}) assert matcher.ruby_protocol() == { "json_class": "Pact::ArrayLike", "contents": { "username": { "json_class": "Pact::Term", "data": { "matcher": {"json_class": "Regexp", "s": "[a-z]+", "o": 0}, "generate": "user", }, }, "id": {"json_class": "Pact::SomethingLike", "contents": 123}, }, "min": 1, }
def test_nested_matchers(): matcher = EachLike({'username': Term('[a-z]+', 'user'), 'id': Like(123)}) assert matcher.ruby_protocol() == { 'json_class': 'Pact::ArrayLike', 'contents': { 'username': { 'json_class': 'Pact::Term', 'data': { 'matcher': { 'json_class': 'Regexp', 's': '[a-z]+', 'o': 0}, 'generate': 'user'}}, 'id': { 'json_class': 'Pact::SomethingLike', 'contents': 123}}, 'min': 1 }
def test_matcher_in_path_gets_converted(): target = Request('GET', '/', headers={'Spam': Term(r'\w+', 'spam')}) assert target.json('3.0.0') == { 'method': 'GET', 'path': '/', 'headers': { 'Spam': 'spam' }, 'matchingRules': { 'header': { "Spam": { "matchers": [{ "match": "regex", "regex": "\\w+" }] } } } }
def test_regex_passes(): pact = Pact('Consumer', 'Provider', file_write_mode='never').given('spam'). \ with_request('GET', '/', headers={'Spam': Term(r'\w+', 'spam')}).will_respond_with(200) with pact: requests.get(pact.uri, headers={'Spam': 'ham'})
def test_regex_fails_v3(): pact = Pact('Consumer', 'Provider', file_write_mode='never', version='3.0.0').given('spam'). \ with_request('GET', '/', headers={'Spam': Term(r'\w+', 'spam')}).will_respond_with(200) with pact: with pytest.raises(AssertionError): requests.get(pact.uri, headers={'Spam': '!@#$'})