Example #1
0
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+',
                    }]
                },
            }
        }
    }
Example #2
0
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+"
                    }]
                },
            }
        },
    }
Example #3
0
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"})
Example #4
0
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+"}]}}},
    }
Example #5
0
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": "!@#$"})
Example #6
0
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'}
    }
Example #7
0
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"\/.+"
            }
        },
    }
Example #8
0
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'\/.+'
            }
        }
    }
Example #9
0
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",
        },
    }
Example #10
0
 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'\/.+'
                 }
             }
         })
Example #11
0
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,
    }
Example #12
0
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
    }
Example #13
0
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+"
                    }]
                }
            }
        }
    }
Example #14
0
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'})
Example #15
0
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': '!@#$'})