def test_is_match_regex_is_false(): """ Test a non-matching regex """ match_rule = MatchRule('GET /test-match/[0-9]{5}') mock_request = create_request('GET /test-match/12a45') assert_false(match_rule.matches(mock_request)) assert_false(match_rule.rule_matches(mock_request['rule']))
def test_is_match_basic(): """ Test a basic matching method / url""" match_rule = MatchRule('GET /test-match') mock_request = create_request('GET /test-match') assert_true(match_rule.matches(mock_request)) assert_true(match_rule.rule_matches(mock_request['rule']))
def test_is_match_basic_is_false(): """ Test a basic non-matching method \ url """ match_rule = MatchRule('GET /testxx-match') mock_request = create_request('GET /test-match') assert_false(match_rule.matches(mock_request)) assert_false(match_rule.rule_matches(mock_request['rule']))
def test_is_match_with_no_headers(): """ Test a matching rule with no headers """ match_rule = MatchRule('GET /test-match') mock_request = create_request('GET /test-match', {'Etag': 'A123'}) assert_true(match_rule.matches(mock_request)) assert_true(match_rule.rule_matches(mock_request['rule'])) assert_true(match_rule.headers_match(mock_request['headers']))
def test_is_match_with_headers_and_no_request_headers(): """ Test a matching rule with headers not in request """ match_rule = MatchRule('GET /test-match', headers={'Etag': 'A123'}) mock_request = create_request('GET /test-match') assert_false(match_rule.matches(mock_request)) assert_true(match_rule.rule_matches(mock_request['rule'])) assert_false(match_rule.headers_match(mock_request['headers']))
def test_is_match_headers_is_false(): """ Test a matching rule with non-matching headers """ match_rule = MatchRule('GET /test-match', headers={'ETag': 'A123'}) mock_request = create_request('GET /test-match', {'ETag': 'XXXX'}) assert_false(match_rule.matches(mock_request)) assert_true(match_rule.rule_matches(mock_request['rule'])) assert_false(match_rule.headers_match(mock_request['headers']))
def test_is_match_headers_is_false(): """ Test a matching rule with non-matching headers """ match_rule = MatchRule( 'GET /test-match', headers={'ETag': 'A123'} ) mock_request = create_request('GET /test-match', {'ETag': 'XXXX'}) assert_false(match_rule.matches(mock_request)) assert_true(match_rule.rule_matches(mock_request['rule'])) assert_false(match_rule.headers_match(mock_request['headers']))
def test_is_match_headers_with_extra_headers(): """ Test a matching rule with an unmatched extra header """ match_rule = MatchRule( 'GET /test-match', headers={'ETag': 'A123', 'Another-Header': 'XX'} ) mock_request = create_request('GET /test-match', {'ETag': 'A123'}) assert_false(match_rule.matches(mock_request)) assert_true(match_rule.rule_matches(mock_request['rule'])) assert_false(match_rule.headers_match(mock_request['headers']))
def test_is_match_headers_with_extra_headers(): """ Test a matching rule with an unmatched extra header """ match_rule = MatchRule('GET /test-match', headers={ 'ETag': 'A123', 'Another-Header': 'XX' }) mock_request = create_request('GET /test-match', {'ETag': 'A123'}) assert_false(match_rule.matches(mock_request)) assert_true(match_rule.rule_matches(mock_request['rule'])) assert_false(match_rule.headers_match(mock_request['headers']))
def when(self, rule='', headers=None, body=None): """ Set the match rule which is the first part of the Preset. :param rule: String incorporating the method and url to match eg "GET url/to/match" :param headers: An optional dictionary of headers to match. .. note:: ``rule`` is matched as a regular expression and can therefore be set to match more than one request. eg. ``r'^GET /something/([a-zA-Z0-9\-_]*)/?'`` Also note that it is always seen as a regex and therefore to match ``"GET /foo?bar=1"`` you would need to use something like:: 'GET /foo\?bar=1' """ match_rule = MatchRule(rule, headers, body) mock = copy(self) mock.rule = match_rule return mock