Exemple #1
0
def test_eq_true():
    """Test that two LogParser objects are equal."""
    line = '175.156.126.209 - - [31/Jan/2017:22:36:45 +0800] "GET /assets/css/'
    line += 'new/prettyPhoto.css?cache_breaker=1.0.27012017 HTTP/1.1" 200 '
    line += '27463 "http://gobudgetair.com/index.php" "Mozilla/5.0 (iPhone; '
    line += 'CPU iPhone OS 10_2_1 like Mac OS X) AppleWebKit/602.4.6 (KHTML, '
    line += 'like Gecko) Version/10.0 Mobile/14D27 Safari/602.1"'
    lp1 = LogParser(line)
    lp2 = LogParser(line)
    assert lp1 == lp2
Exemple #2
0
def test_eq_false():
    """Test that two LogParser objects are not equal."""
    line1 = '175.156.126.209 - - [31/Jan/2017:22:36:45 +0800] "GET '
    line1 += '/assets/css/new/prettyPhoto.css?cache_breaker=1.0.27012017 HTTP/'
    line1 += '1.1" 200 27463 "http://gobudgetair.com/index.php" "Mozilla/5.0 '
    line1 += '(iPhone; CPU iPhone OS 10_2_1 like Mac OS X) '
    line1 += 'AppleWebKit/602.4.6 (KHTML, like Gecko) Version/10.0 Mobile/'
    line1 += '14D27 Safari/602.1"'

    # Tweak the timestamp (22:36:45 to 22:36:46) to get a difference.
    line2 = '175.156.126.209 - - [31/Jan/2017:22:36:46 +0800] "GET '
    line2 += '/assets/css/new/prettyPhoto.css?cache_breaker=1.0.27012017 HTTP/'
    line2 += '1.1" 200 27463 "http://gobudgetair.com/index.php" "Mozilla/5.0 '
    line2 += '(iPhone; CPU iPhone OS 10_2_1 like Mac OS X) '
    line2 += 'AppleWebKit/602.4.6 (KHTML, like Gecko) Version/10.0 Mobile/'
    line2 += '14D27 Safari/602.1"'

    lp1 = LogParser(line1)
    lp2 = LogParser(line2)
    assert lp1 != lp2
Exemple #3
0
def test_diff_type_false():
    """Compare a LogParser object to an objet of a different type."""
    line1 = '175.156.126.209 - - [31/Jan/2017:22:36:45 +0800] "GET '
    line1 += '/assets/css/new/prettyPhoto.css?cache_breaker=1.0.27012017 HTTP/'
    line1 += '1.1" 200 27463 "http://gobudgetair.com/index.php" "Mozilla/5.0 '
    line1 += '(iPhone; CPU iPhone OS 10_2_1 like Mac OS X) '
    line1 += 'AppleWebKit/602.4.6 (KHTML, like Gecko) Version/10.0 Mobile/'
    line1 += '14D27 Safari/602.1"'

    lp1 = LogParser(line1)
    assert lp1 != "hello world"
Exemple #4
0
def test_str(node):
    """Test the str attribute.

    Parameters
    ----------
    node : dict
        A dictionary object containing a test string and expected
        results for various tests.
    """
    none_str = str(LogParser('bad line'))
    lp = LogParser(node['linein'],
                   timezone=node['timezone'],
                   dts_format=node['dts_format'])
    test_result = str(lp)
    if lp.ipaddress is None:
        benchmark = none_str
    else:
        benchmark = node['str']
    print(f"timezone: {node['timezone']}")
    print(f"  dts_format: {node['dts_format']}\n")
    print(f"received:\n{test_result}\n")
    print(f"expected:\n{node['str']}")
    assert test_result == benchmark
def test_requestline(node):
    """Test requestline.

    Parameters
    ----------
    node : dict
        A dictionary object containing a test string and expected
        results for various tests.
    """
    lp = LogParser(node['linein'],
                   timezone=node['timezone'],
                   dts_format=node['dts_format'])
    test_result = lp.requestline
    benchmark = node['requestline']
    assert test_result == benchmark
Exemple #6
0
def test_timestamp(node):
    """Test the timestamp attribute.

    Parameters
    ----------
    node : dict
        A dictionary object containing a test string and expected
        results for various tests.
    """
    lp = LogParser(node['linein'],
                   timezone=node['timezone'],
                   dts_format=node['dts_format'])
    test_result = lp.timestamp
    benchmark = node['timestamp']
    print(f" timezone: {node['timezone']}")
    print(f"dts_format: {node['dts_format']}")
    assert test_result == benchmark
Exemple #7
0
def test_datasize(node):
    """Test datasize.

    Parameters
    ----------
    node : dict
        A dictionary object containing a test string and expected
        results for various tests.
    """
    lp = LogParser(node['linein'],
                   timezone=node['timezone'],
                   dts_format=node['dts_format'])
    test_result = lp.datasize
    try:
        benchmark = int(node['datasize'])
    except TypeError:
        benchmark = node['datasize']
    assert test_result == benchmark
Exemple #8
0
def test_mangled_field():
    """Test malformed input."""
    lp = LogParser(42)
    test_result = lp.ipaddress
    benchmark = None
    assert test_result == benchmark
Exemple #9
0
def test_assign_stat_code():
    """Test assignment of a status code."""
    lp = LogParser(42)
    lp.statuscode = 42
    assert lp.statuscode == 42
Exemple #10
0
def test_assign_uid():
    """Test assignment of a userid."""
    lp = LogParser(42)
    lp.userid = '*****@*****.**'
    assert lp.userid == '*****@*****.**'
Exemple #11
0
def test_assign_ip():
    """Test assignment of an ipaddress."""
    lp = LogParser(42)
    lp.ipaddress = '192.168.1.1'
    assert lp.ipaddress == '192.168.1.1'
Exemple #12
0
def test_assign_referrer():
    """Test assignment of a referrer."""
    lp = LogParser(42)
    lp.referrer = '-'
    assert lp.referrer == '-'
Exemple #13
0
def test_assign_uagent():
    """Test assignment of a useragent."""
    lp = LogParser(42)
    lp.useragent = 'Mozilla/4.0'
    assert lp.useragent == 'Mozilla/4.0'
Exemple #14
0
def test_assign_data_size():
    """Test assignment of a datasize field."""
    lp = LogParser(42)
    lp.datasize = 42
    assert lp.datasize == 42
Exemple #15
0
def test_non_str():
    """Test inputs that are non-strings."""
    lp = LogParser(42)
    test_result = lp.ipaddress
    benchmark = None
    assert test_result == benchmark
Exemple #16
0
def test_assign_uname():
    """Test assignment of a username."""
    lp = LogParser(42)
    lp.username = '******'
    assert lp.username == 'StarLord'
Exemple #17
0
def test_assign_req_line():
    """Test assignment of a request line."""
    lp = LogParser(42)
    lp.requestline = 'GET /images/puce.gif HTTP/1.1'
    assert lp.requestline == 'GET /images/puce.gif HTTP/1.1'
Exemple #18
0
def test_assign_tstamp():
    """Test assignment of a timestamp."""
    lp = LogParser(42)
    lp.timestamp = '24/Mar/2009:18:07:16 +0100'
    assert lp.timestamp == '24/Mar/2009:18:07:16 +0100'