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_falsey_request_body(): target = Request("GET", "/path", body=[]) assert target.json("2.0.0") == { "method": "GET", "path": "/path", "body": [] }
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_falsey_request_body(): target = Request('GET', '/path', body=[]) assert target.json('2.0.0') == { 'method': 'GET', 'path': '/path', 'body': [] }
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_falsey_body(self): target = Request('GET', '/path', body=[]) result = target.json('2.0.0') self.assertEqual(result, { 'method': 'GET', 'path': '/path', 'body': [] })
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_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 with_request(self, method, path, body=None, headers=None, query=None): """ Define the request the request that the client is expected to perform. :param method: The HTTP method. :type method: str :param path: The path portion of the URI the client will access. :type path: str, Matcher :param body: The request body, can be a string or an object that will serialize to JSON, like list or dict, defaults to None. :type body: list, dict or None :param headers: The headers the client is expected to include on with this request. Defaults to None. :type headers: dict or None :param query: The query options the client is expected to send. Can be a dict of keys and values, or a URL encoded string. Defaults to None. :type query: dict, basestring, or None :rtype: Pact """ self._interactions[0]['request'] = Request(method, path, body=body, headers=headers, query=query).json( self.version) return self
def test_all_options(self): target = Request('POST', '/path', body='the content', headers={'Accept': 'application/json'}, query='term=test') result = target.json('2.0.0') self.assertEqual( result, { 'method': 'POST', 'path': '/path', 'body': 'the content', 'headers': { 'Accept': 'application/json' }, 'query': 'term=test' })
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_sparse(self): target = Request('GET', '/path') result = target.json('2.0.0') self.assertEqual(result, {'method': 'GET', 'path': '/path'})