Skip to content

Commit

Permalink
Merge 37f63ca into 7c9526c
Browse files Browse the repository at this point in the history
  • Loading branch information
mrname committed Apr 3, 2016
2 parents 7c9526c + 37f63ca commit 550cbf3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ easily produced using the public methods of ``HarParser`` and ``HarPage``::
# It also has methods for filtering assets #
# Get a collection of entries that were images in the 2XX status code range #
entries = har_page.filter_entries(content_type='image.*', status_code='2.*')
# This method can filter by:
# * content_type ('application/json' for example)
# * status_code ('200' for example)
# * request_type ('GET' for example)
# * http_version ('HTTP/1.1' for example)
# It will use a regex by default, but you can also force a literal string match by passing regex=False

# Get the size of the collection we just made #
collection_size = har_page.get_total_size(entries)
Expand Down
24 changes: 23 additions & 1 deletion haralyzer/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ def match_request_type(self, entry, request_type, regex=True):
else:
return entry['request']['method'] == request_type

@staticmethod
def match_http_version(entry, http_version, regex=True):
"""
Helper function that returns entries with a request type
matching the given `request_type` argument.
:param entry: entry object to analyze
:param request_type: ``str`` of request type to match
:param regex: ``bool`` indicating whether to use a regex or string match
"""
response_version = entry['response']['httpVersion']
if regex:
return re.search(http_version, response_version,
flags=re.IGNORECASE) is not None
else:
return response_version == http_version

def match_status_code(self, entry, status_code, regex=True):
"""
Helper function that returns entries with a status code matching
Expand Down Expand Up @@ -269,13 +286,14 @@ def _get_asset_load(self, asset_type):
content_type=self.asset_types[asset_type])

def filter_entries(self, request_type=None, content_type=None,
status_code=None, regex=True):
status_code=None, http_version=None, regex=True):
"""
Returns a ``list`` of entry objects based on the filter criteria.
:param request_type: ``str`` of request type (i.e. - GET or POST)
:param content_type: ``str`` of regex to use for finding content type
:param status_code: ``int`` of the desired status code
:param http_version: ``str`` of HTTP version of request
:param regex: ``bool`` indicating whether to use regex or exact match.
"""
results = []
Expand All @@ -287,6 +305,7 @@ def filter_entries(self, request_type=None, content_type=None,
* The request type using self._match_request_type()
* The content type using self._match_headers()
* The HTTP response status code using self._match_status_code()
* The HTTP version using self._match_headers()
Oh lords of python.... please forgive my soul
"""
Expand All @@ -302,6 +321,9 @@ def filter_entries(self, request_type=None, content_type=None,
if status_code is not None and not p.match_status_code(
entry, status_code, regex=regex):
valid_entry = False
if http_version is not None and not p.match_http_version(
entry, http_version, regex=regex):
valid_entry = False

if valid_entry:
results.append(entry)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ def test_match_status_code(har_data):
assert not har_parser.match_status_code(entry, '201', regex=False)


def test_http_version(har_data):
"""
Tests the ability of the parser to match status codes.
"""
init_data = har_data('humanssuck.net.har')
har_parser = HarParser(init_data)

entry = har_data('single_entry.har')

# TEST THE REGEX FEATURE FIRST #
assert har_parser.match_http_version(entry, '.*1.1')
assert not har_parser.match_http_version(entry, '.*2')
# TEST LITERAL STRING MATCH #
assert har_parser.match_http_version(entry, 'HTTP/1.1', regex=False)
assert not har_parser.match_http_version(entry, 'HTTP/2.0', regex=False)

def test_create_asset_timeline(har_data):
"""
Tests the asset timeline function by making sure that it inserts one object
Expand Down

0 comments on commit 550cbf3

Please sign in to comment.