Skip to content

Commit

Permalink
resolve some duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
KissPeter committed Nov 16, 2019
1 parent 7024f0b commit e4bd3de
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ addons:
script:
- python fuzzer.py -h
- pip3 install -r test/requirements_for_test.txt
- cd test && pytest --durations=10 --show-capture=stdout -v -rP test.pyy
- cd test && pytest --durations=10 --show-capture=stdout -v -rP test.py
20 changes: 10 additions & 10 deletions apifuzzer/base_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ def __init__(self, name):
self.headers = list()
self.path_variables = list()
self.cookies = list()
self.field_to_param = {
'params': self.params,
'headers':self.headers,
'data': self.data,
'path_variables': self.path_variables,
'cookies': self.cookies
}
"""
Possible paramters from request docs:
:param method: method for the new :class:`Request` object.
Expand All @@ -31,14 +38,7 @@ def compile_template(self):
_method = Static(name='method', value=self.method)
# _method = Static(name='method', value=self.method, encoder=ENC_STR_UTF8)
template = Template(name=self.name, fields=[_url, _method])
if list(self.params):
template.append_fields([Container(name='params', fields=self.params)])
if list(self.headers):
template.append_fields([Container(name='headers', fields=self.headers)])
if list(self.data):
template.append_fields([Container(name='data', fields=self.data)])
if list(self.path_variables):
template.append_fields([Container(name='path_variables', fields=self.path_variables)])
if list(self.cookies):
template.append_fields([Container(name='cookies', fields=self.cookies)])
for name, field in self.field_to_param.items():
if list(field):
template.append_fields([Container(name=name, fields=field)])
return template
17 changes: 8 additions & 9 deletions apifuzzer/fuzzer_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ def compile_headers(self, fuzz_header=None):
_header.update(self.auth_headers)
return _header

def report_add_basic_msg(self, msg):
self.report.set_status(Report.FAILED)
self.logger.warn(msg)
self.report.failed(msg)

def transmit(self, **kwargs):
self.logger.debug('Transmit: {}'.format(kwargs))
try:
Expand Down Expand Up @@ -102,19 +107,13 @@ def transmit(self, **kwargs):
self.report.add('response', _return.content.decode())
status_code = _return.status_code
if not status_code:
self.report.set_status(Report.FAILED)
self.logger.warn('Failed to parse http response code')
self.report.failed('Failed to parse http response code')
self.report_add_basic_msg('Failed to parse http response code')
elif status_code not in self.accepted_status_codes:
self.report.add('parsed_status_code', status_code)
self.report.set_status(Report.FAILED)
self.logger.warn('Return code %s is not in the expected list', status_code)
self.report.failed(('Return code %s is not in the expected list', status_code))
self.report_add_basic_msg(('Return code %s is not in the expected list:', status_code))
return _return
except (RequestException, UnicodeDecodeError, UnicodeEncodeError) as e: # request failure such as InvalidHeader
self.report.set_status(Report.FAILED)
self.logger.warn('Failed to parse http response code, exception occurred')
self.report.failed('Failed to parse http response code, exception occurred')
self.report_add_basic_msg(('Failed to parse http response code, exception occurred: %s', e))

def post_test(self, test_num):
"""Called after a test is completed, perform cleanup etc."""
Expand Down

0 comments on commit e4bd3de

Please sign in to comment.