def test_source_bad_formats(recwarn):
    with pytest.raises(ValueError):
        with apache.ApacheSource('', log_format='%b %B'):
            pass
    with pytest.raises(ValueError):
        with apache.ApacheSource('', log_format='%Q %x'):
            pass
    with pytest.raises(ValueError):
        with apache.ApacheSource('', log_format='%C'):
            pass
    with pytest.raises(ValueError):
        with apache.ApacheSource('', log_format='%{foo}p'):
            pass
    with pytest.raises(ValueError):
        with apache.ApacheSource('', log_format='%{rid}P'):
            pass
    with pytest.raises(ValueError):
        with apache.ApacheSource('', log_format='%{%H%:%M:%S}t'):
            pass  #  ^ Extraneous percent
    with apache.ApacheSource(MULTIPLE_REMOTE_HOSTS.splitlines(True),
                             log_format=apache.COMBINED) as source:
        for row in source:
            break
    assert recwarn.pop(apache.ApacheWarning)
    recwarn.clear()
    with apache.ApacheSource(INVALID_REMOTE_HOST.splitlines(True),
                             log_format=apache.COMBINED) as source:
        for row in source:
            break
    assert recwarn.pop(apache.ApacheWarning)
def test_source_date_formats():
    with apache.ApacheSource(
            EXAMPLE_04.splitlines(True),
            log_format="%{%Y-%m-%dT%H:%M:%S%z}t %H %m %U%q %>s %O") as source:
        row = None
        for count, row in enumerate(source):
            if count == 0:
                assert row.time == dt.datetime('2004-03-08 00:56:39')
                assert row.method == 'GET'
                assert row.protocol == 'HTTP/1.0'
                assert row.url_stem == dt.url(
                    '/twiki/bin/view/Sandbox/WebHome')
                assert row.url_query == dt.url('?rev=1.6')
                assert row.status == 200
                assert row.bytes_sent == 8545
            elif count == 1:
                assert row.time == dt.datetime('2004-03-07 22:01:53')
                assert row.method == 'HEAD'
                assert row.protocol == 'HTTP/1.1'
                assert row.url_stem == dt.url('/razor.html')
                assert row.url_query is None
                assert row.status == 302
                assert row.bytes_sent == 2869
        assert row
        assert count == 1
def test_source_combined():
    with apache.ApacheSource(EXAMPLE_02.splitlines(True),
                             log_format=apache.COMBINED) as source:
        row = None
        for count, row in enumerate(source):
            if count == 0:
                assert row.remote_host == dt.hostname('78.86.48.95')
                assert row.ident is None
                assert row.remote_user is None
                assert row.time == dt.DateTime(2011, 10, 27, 23, 0, 5)
                assert row.request == dt.Request(
                    'GET', dt.url('/template/images/ITSheader.jpg'),
                    'HTTP/1.1')
                assert row.status == 200
                assert row.size == 14745
                assert row.req_Referer is None
                assert row.req_User_Agent == 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; byond_4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.2; OfficeLiveConnector.1.5; OfficeLivePatch.1.3; .NET4.0E; .NET4.0C)'
            elif count == 1:
                assert row.remote_host == dt.hostname('217.129.225.117')
                assert row.ident is None
                assert row.remote_user is None
                assert row.time == dt.DateTime(2011, 10, 27, 23, 0, 7)
                assert row.request == dt.Request('GET',
                                                 dt.url('/images/spacer.gif'),
                                                 'HTTP/1.1')
                assert row.status == 200
                assert row.size == 43
                assert row.req_Referer == dt.url(
                    'http://eprints.lse.ac.uk/33718/')
                assert row.req_User_Agent == 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pt-BR; rv:1.9.2.23) Gecko/20110920 Firefox/3.6.23'
            else:
                assert False
        assert row
        assert count == 1
def test_source_common():
    with apache.ApacheSource(EXAMPLE_01.splitlines(True)) as source:
        row = None
        for count, row in enumerate(source):
            if count == 0:
                assert row.remote_host == dt.hostname('64.242.88.10')
                assert row.ident is None
                assert row.remote_user is None
                assert row.time == dt.DateTime(2004, 3, 8, 0, 56, 39)
                assert row.request == dt.Request(
                    'GET', dt.url('/twiki/bin/view/Sandbox/WebHome?rev=1.6'),
                    'HTTP/1.1')
                assert row.status == 200
                assert row.size == 8545
            elif count == 1:
                assert row.remote_host == dt.hostname('lordgun.org')
                assert row.ident is None
                assert row.remote_user == 'foo'
                assert row.time == dt.DateTime(2004, 3, 8, 1, 1, 53)
                assert row.request == dt.Request('GET', dt.url('/razor.html'),
                                                 'HTTP/1.0')
                assert row.status == 302
                assert row.size == 2869
            else:
                assert False
        assert row
        assert count == 1
def test_source_field_names():
    with apache.ApacheSource(
            EXAMPLE_03.splitlines(True),
            log_format="%{local}p,%{remote}p %{pid}P") as source:
        row = None
        for count, row in enumerate(source):
            assert row.local_port == [49600, 65000, 12345, 123][count]
            assert row.remote_port == [80, 80, 443, 443][count]
            assert row.pid == [1000, 2000, 65000, 100][count]
        assert row
        assert count == 3