1
1
2
2
import unittest
3
3
import apache_log_parser
4
+ import datetime
4
5
5
6
class ApacheLogParserTestCase (unittest .TestCase ):
6
7
def test_simple (self ):
@@ -22,5 +23,34 @@ def test_simple(self):
22
23
23
24
self .assertEqual (apache_log_parser .get_fieldnames (format_string ), ('remote_host' , 'pid' , 'time_received' , 'time_us' , 'request_first_line' , 'status' , 'response_bytes_clf' , 'request_header_referer' , 'request_header_user_agent' , 'remote_logname' , 'remote_user' ))
24
25
26
+ def test_pr8 (self ):
27
+ parser = apache_log_parser .make_parser ('%h %{remote}p %v %{local}p %t \" %r\" %>s %b \" %{Referer}i\" \" %{User-Agent}i\" %P %D %{number}n %{SSL_PROTOCOL}x %{SSL_CIPHER}x %k %{UNIQUE_ID}e ' )
28
+ data = parser ('127.0.0.1 50153 mysite.co.uk 443 [28/Nov/2014:10:03:40 +0000] "GET /mypage/this/that?stuff=all HTTP/1.1" 200 5129 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36" 18572 363701 0 TLSv1.01 MY-CYPHER 0 VHhIfKwQGCMAAEiMUIAAAAF ' )
29
+ self .assertEqual (data , {
30
+ 'status' : '200' , 'extension_ssl_protocol' : 'TLSv1.01' , 'request_header_user_agent__browser__family' : 'Chrome' ,
31
+ 'time_us' : '363701' , 'num_keepalives' : '0' , 'request_first_line' : 'GET /mypage/this/that?stuff=all HTTP/1.1' ,
32
+ 'pid' : '18572' , 'response_bytes_clf' : '5129' , 'request_header_user_agent__os__family' : u'Windows 7' ,
33
+ 'request_url' : '/mypage/this/that?stuff=all' , 'request_http_ver' : '1.1' ,
34
+ 'request_header_referer' : '-' , 'server_name' : 'mysite.co.uk' , 'request_header_user_agent__is_mobile' : False ,
35
+ 'request_header_user_agent__browser__version_string' : '37.0.2062' ,
36
+ 'request_header_user_agent' : 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36' ,
37
+ 'note_number' : '0' , 'request_header_user_agent__os__version_string' : '' ,
38
+ 'server_port_local' : '443' , 'request_method' : 'GET' ,
39
+ 'time_received_datetimeobj' : datetime .datetime (2014 , 11 , 28 , 10 , 3 , 40 ),
40
+ 'server_port_remote' : '50153' , 'env_unique_id' : 'VHhIfKwQGCMAAEiMUIAAAAF' ,
41
+ 'time_received_isoformat' : '2014-11-28T10:03:40' , 'remote_host' : '127.0.0.1' ,
42
+ 'extension_ssl_cipher' : 'MY-CYPHER' , 'time_received' : '[28/Nov/2014:10:03:40 +0000]' })
43
+
44
+ parser = apache_log_parser .make_parser ('%A %V %p %P %a \" %r\" \" %{main_call}n\" %{some_time}t %b %>s %D %{UNIQUE_ID}e ' )
45
+ data = parser ('127.0.0.1 othersite 80 25572 192.168.1.100 "GET /Class/method/ HTTP/1.1" "-" 20141128155031 2266 200 10991 VHiZx6wQGCMAAEiBE8kAAAAA:VHiZx6wQGiMAAGPkBnMAAAAH:VHiZx6wQGiMAAGPkBnMAAAAH ' )
46
+ self .assertEqual (data , {
47
+ 'status' : '200' , 'note_main_call' : '-' , 'time_some_time' : '20141128155031' ,
48
+ 'time_us' : '10991' , 'request_http_ver' : '1.1' , 'local_ip' : '127.0.0.1' ,
49
+ 'pid' : '25572' , 'request_first_line' : 'GET /Class/method/ HTTP/1.1' , 'request_method' : 'GET' ,
50
+ 'server_port' : '80' , 'response_bytes_clf' : '2266' , 'server_name2' : 'othersite' ,
51
+ 'request_url' : '/Class/method/' ,
52
+ 'env_unique_id' : 'VHiZx6wQGCMAAEiBE8kAAAAA:VHiZx6wQGiMAAGPkBnMAAAAH:VHiZx6wQGiMAAGPkBnMAAAAH' ,
53
+ 'remote_ip' : '192.168.1.100' })
54
+
25
55
if __name__ == '__main__' :
26
56
unittest .main ()
0 commit comments