forked from svanoort/pyresttest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_extension.py
executable file
·83 lines (66 loc) · 2.76 KB
/
sample_extension.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Sample python extension
import pyresttest.validators as validators
from pyresttest.binding import Context
import sys
# Python 3 compatibility
if sys.version_info[0] > 2:
from past.builtins import basestring
from pyresttest.six import text_type
from pyresttest.six import binary_type
class ContainsValidator(validators.AbstractValidator):
# Sample validator that verifies a string is contained in the request body
contains_string = None
def validate(self, body=None, headers=None, context=None):
if isinstance(body, binary_type) and isinstance(self.contains_string, text_type):
result = self.contains_string.encode('utf-8') in body
else:
result = self.contains_string in body
if result:
return True
else: # Return failure object with additional information
message = "Request body did not contain string: {0}".format(
self.contains_string)
return validators.Failure(message=message, details=None, validator=self)
@staticmethod
def parse(config):
""" Parse a contains validator, which takes as the config a simple string to find """
if not isinstance(config, basestring):
raise TypeError("Contains input must be a simple string")
validator = ContainsValidator()
validator.contains_string = config
return validator
class WeirdzoExtractor(validators.AbstractExtractor):
""" Always returns 'zorba' """
extractor_type = 'weirdzo'
is_body_extractor = True
@classmethod
def parse(cls, config, extractor_base=None):
base = WeirdzoExtractor()
return cls.configure_base(config, base)
def extract_internal(self, query=None, args=None, body=None, headers=None):
return 'zorba'
def parse_generator_doubling(config):
""" Returns generators that double with each value returned
Config includes optional start value """
start = 1
if 'start' in config:
start = int(config['start'])
# We cannot simply use start as the variable, because of scoping
# limitations
def generator():
val = start
while(True):
yield val
val = val * 2
return generator()
def test_is_dict(input):
""" Simple test that returns true if item is a dictionary """
return isinstance(input, dict)
# This is where the magic happens, each one of these is a registry of
# validators/extractors/generators to use
VALIDATORS = {'contains': ContainsValidator.parse}
VALIDATOR_TESTS = {'is_dict': test_is_dict}
# Converts to lowercase and tests for equality
COMPARATORS = {'str.eq.lower': lambda a, b: str(a).lower() == str(b).lower()}
EXTRACTORS = {'weirdzo': WeirdzoExtractor.parse}
GENERATORS = {'doubling': parse_generator_doubling}