ntail 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ require 'helper'
2
+
3
+ class TestHttpMethod < Test::Unit::TestCase
4
+
5
+ context "ntail" do
6
+
7
+ context "(with Sickill::Rainbow enabled)" do
8
+
9
+ setup do
10
+ Sickill::Rainbow.enabled = true
11
+ end
12
+
13
+ should "color-code the HTTP method" do
14
+
15
+ # read-only methods are never color-coded...
16
+ log_line = random_log_line(:http_method => 'GET')
17
+ assert_equal "GET", log_line.to_http_method_s
18
+
19
+ # methods that change state are ALWAYS color-coded if Rainbow is enabled...
20
+ log_line = random_log_line(:http_method => 'POST')
21
+ assert_not_equal "POST", log_line.to_http_method_s
22
+ assert_equal "POST".inverse, log_line.to_http_method_s
23
+
24
+ end
25
+
26
+ end
27
+
28
+ context "(with Sickill::Rainbow disabled)" do
29
+
30
+ setup do
31
+ Sickill::Rainbow.enabled = false
32
+ end
33
+
34
+ should "NOT color-code the HTTP method" do
35
+
36
+ # read-only methods are never color-coded...
37
+ log_line = random_log_line(:http_method => 'GET')
38
+ assert_equal "GET", log_line.to_http_method_s
39
+
40
+ # methods that change state are NOT color-coded if Rainbow is disabled...
41
+ log_line = random_log_line(:http_method => 'POST')
42
+ assert_equal "POST", log_line.to_http_method_s
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,77 @@
1
+ require 'helper'
2
+
3
+ class TestHttpReferer < Test::Unit::TestCase
4
+
5
+ context "ntail" do
6
+
7
+ setup do
8
+ @http_referer = "http://example.com/index.html"
9
+ @log_line = random_log_line(:http_referer => @http_referer)
10
+ end
11
+
12
+ teardown do
13
+ # undo any changes the test may have made
14
+ NginxTail::LogLine.reset_internal_referers
15
+ end
16
+
17
+ should "have empty list of internal referers without configuration" do
18
+ assert NginxTail::LogLine.internal_referers.empty?
19
+ end
20
+
21
+ should "correctly identify the default/unknown HTTP referer" do
22
+ unknown_referer = NginxTail::HttpReferer::UNKNOWN_REFERER
23
+ log_line = random_log_line(:http_referer => unknown_referer)
24
+
25
+ assert NginxTail::LogLine.unknown_referer?(unknown_referer)
26
+ assert !NginxTail::LogLine.internal_referer?(unknown_referer)
27
+ assert !NginxTail::LogLine.external_referer?(unknown_referer)
28
+
29
+ assert log_line.unknown_referer?
30
+ end
31
+
32
+ should "not allow the default/unknown HTTP referer to be added" do
33
+ assert_raise RuntimeError do
34
+ NginxTail::LogLine.add_internal_referer(NginxTail::HttpReferer::UNKNOWN_REFERER)
35
+ end
36
+ end
37
+
38
+ should "have non-empty list of internal referers after configuration" do
39
+ NginxTail::LogLine.add_internal_referer(first_referer = /http:\/\/my_website\.com/)
40
+ assert_equal 1, NginxTail::LogLine.internal_referers.size
41
+ assert NginxTail::LogLine.internal_referers.include?(first_referer)
42
+
43
+ NginxTail::LogLine.add_internal_referer(second_referer = /http:\/\/www.my_website\.com/)
44
+ assert_equal 2, NginxTail::LogLine.internal_referers.size
45
+ assert NginxTail::LogLine.internal_referers.include?(second_referer)
46
+ end
47
+
48
+ should "recognize an internal referer after configuration" do
49
+ assert !NginxTail::LogLine.internal_referer?(@http_referer)
50
+ assert NginxTail::LogLine.external_referer?(@http_referer)
51
+ assert !@log_line.internal_referer?
52
+ assert @log_line.external_referer?
53
+
54
+ NginxTail::LogLine.add_internal_referer(/http:\/\/example\.com/)
55
+
56
+ assert NginxTail::LogLine.internal_referer?(@http_referer)
57
+ assert !NginxTail::LogLine.external_referer?(@http_referer)
58
+ assert @log_line.internal_referer?
59
+ assert !@log_line.external_referer?
60
+ end
61
+
62
+ should "parse and format the unknownHTTP referer" do
63
+ http_referer = NginxTail::HttpReferer::UNKNOWN_REFERER
64
+ assert_equal http_referer, NginxTail::LogLine.to_referer_s(http_referer)
65
+ end
66
+
67
+ should "parse and format HTTP referer into a host string" do
68
+ # directly via the helper function
69
+ to_referer_s = NginxTail::LogLine.to_referer_s(@http_referer)
70
+ assert_equal "example.com", to_referer_s
71
+ # parsed from a raw log line
72
+ assert_equal "example.com", @log_line.to_referer_s
73
+ end
74
+
75
+ end
76
+
77
+ end
@@ -0,0 +1,29 @@
1
+ require 'helper'
2
+
3
+ class TestHttpUserAgent < Test::Unit::TestCase
4
+
5
+ context "ntail" do
6
+
7
+ should "correctly identify search bot user agent" do
8
+ search_bot_user_agent = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
9
+ # directly via the helper function
10
+ assert NginxTail::LogLine.search_bot?(search_bot_user_agent)
11
+ # parsed from a raw log line
12
+ log_line = random_log_line(:http_user_agent => search_bot_user_agent)
13
+ assert log_line.search_bot?
14
+ assert log_line.to_agent.search_bot?
15
+ end
16
+
17
+ should "correctly identify non-bot user agent" do
18
+ non_bot_user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
19
+ # directly via the helper function
20
+ assert !NginxTail::LogLine.search_bot?(non_bot_user_agent)
21
+ # parsed from a raw log line
22
+ log_line = random_log_line(:http_user_agent => non_bot_user_agent)
23
+ assert !log_line.search_bot?
24
+ assert !log_line.to_agent.search_bot?
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,45 @@
1
+ require 'helper'
2
+
3
+ class TestKnownIpAddresses < Test::Unit::TestCase
4
+
5
+ def teardown
6
+ # undo any changes the test may have made
7
+ NginxTail::LogLine.reset_known_ip_addresses
8
+ end
9
+
10
+ should "have empty list of known IP addresses without configuration" do
11
+ assert NginxTail::LogLine.known_ip_addresses.empty?
12
+ end
13
+
14
+ should "have non-empty list of known IP addresses after configuration" do
15
+ NginxTail::LogLine.add_known_ip_address(first_ip_address = random_ip_address)
16
+ assert_equal 1, NginxTail::LogLine.known_ip_addresses.size
17
+ assert NginxTail::LogLine.known_ip_addresses.include?(first_ip_address)
18
+
19
+ NginxTail::LogLine.add_known_ip_address(second_ip_address = random_ip_address)
20
+ assert_equal 2, NginxTail::LogLine.known_ip_addresses.size
21
+ assert NginxTail::LogLine.known_ip_addresses.include?(second_ip_address)
22
+ end
23
+
24
+ should "avoid duplicates in list of known IP addresses" do
25
+ NginxTail::LogLine.add_known_ip_address(known_ip_address = random_ip_address)
26
+ assert_equal 1, NginxTail::LogLine.known_ip_addresses.size
27
+
28
+ NginxTail::LogLine.add_known_ip_address(known_ip_address)
29
+ assert_equal 1, NginxTail::LogLine.known_ip_addresses.size
30
+ end
31
+
32
+ should "recognize a known IP address after configuration" do
33
+ remote_address = random_ip_address
34
+ log_line = random_log_line(:remote_addr => remote_address)
35
+
36
+ assert !NginxTail::LogLine.known_ip_address?(remote_address)
37
+ assert !log_line.known_ip_address?
38
+
39
+ NginxTail::LogLine.add_known_ip_address(remote_address)
40
+
41
+ assert NginxTail::LogLine.known_ip_address?(remote_address)
42
+ assert log_line.known_ip_address?
43
+ end
44
+
45
+ end
@@ -0,0 +1,45 @@
1
+ require 'helper'
2
+
3
+ class TestLocalIpAddresses < Test::Unit::TestCase
4
+
5
+ def teardown
6
+ # undo any changes the test may have made
7
+ NginxTail::LogLine.reset_local_ip_addresses
8
+ end
9
+
10
+ should "have empty list of known IP addresses without configuration" do
11
+ assert NginxTail::LogLine.local_ip_addresses.empty?
12
+ end
13
+
14
+ should "have non-empty list of known IP addresses after configuration" do
15
+ NginxTail::LogLine.add_local_ip_address(first_ip_address = local_ip_address)
16
+ assert_equal 1, NginxTail::LogLine.local_ip_addresses.size
17
+ assert NginxTail::LogLine.local_ip_addresses.include?(first_ip_address)
18
+
19
+ NginxTail::LogLine.add_local_ip_address(second_ip_address = local_ip_address)
20
+ assert_equal 2, NginxTail::LogLine.local_ip_addresses.size
21
+ assert NginxTail::LogLine.local_ip_addresses.include?(second_ip_address)
22
+ end
23
+
24
+ should "avoid duplicates in list of known IP addresses" do
25
+ NginxTail::LogLine.add_local_ip_address(local_ip_address = local_ip_address)
26
+ assert_equal 1, NginxTail::LogLine.local_ip_addresses.size
27
+
28
+ NginxTail::LogLine.add_local_ip_address(local_ip_address)
29
+ assert_equal 1, NginxTail::LogLine.local_ip_addresses.size
30
+ end
31
+
32
+ should "recognize a known IP address after configuration" do
33
+ remote_address = local_ip_address
34
+ log_line = random_log_line(:remote_addr => remote_address)
35
+
36
+ assert !NginxTail::LogLine.local_ip_address?(remote_address)
37
+ assert !log_line.local_ip_address?
38
+
39
+ NginxTail::LogLine.add_local_ip_address(remote_address)
40
+
41
+ assert NginxTail::LogLine.local_ip_address?(remote_address)
42
+ assert log_line.local_ip_address?
43
+ end
44
+
45
+ end
@@ -0,0 +1,51 @@
1
+ require 'helper'
2
+
3
+ class TestLogLine < Test::Unit::TestCase
4
+
5
+ context "NginxTail::LogLine" do
6
+
7
+ should "initialize itself correctly from a parsable log line" do
8
+ raw_line = random_raw_line
9
+ log_line = NginxTail::LogLine.new(raw_line)
10
+ assert_equal raw_line, log_line.raw_line
11
+ assert log_line.parsable
12
+ end
13
+
14
+ should "initialize itself correctly from a non-parsable log line" do
15
+ raw_line = "foo bar blegga"
16
+ log_line = NginxTail::LogLine.new(raw_line)
17
+ assert_equal raw_line, log_line.raw_line
18
+ assert !log_line.parsable
19
+ end
20
+
21
+ should "implement non-abbreviated alias for $remote_addr" do
22
+ remote_addr = random_ip_address
23
+ log_line = random_log_line(:remote_addr => remote_addr)
24
+ assert_equal remote_addr, log_line.remote_addr
25
+ assert_equal remote_addr, log_line.remote_address
26
+ end
27
+
28
+ should "implement a getter method for each (sub-)component" do
29
+ (NginxTail::LogLine::COMPONENTS + NginxTail::LogLine::SUBCOMPONENTS).each do |component|
30
+ getter_method = component.to_s
31
+ assert NginxTail::LogLine.instance_methods.include?(getter_method), "getter '#{getter_method}' should exist"
32
+ end
33
+ end
34
+
35
+ should "NOT implement a setter method for any (sub-)component" do
36
+ (NginxTail::LogLine::COMPONENTS + NginxTail::LogLine::SUBCOMPONENTS).each do |component|
37
+ setter_method = component.to_s + "="
38
+ assert !NginxTail::LogLine.instance_methods.include?(setter_method), "setter '#{setter_method}' should NOT exist"
39
+ end
40
+ end
41
+
42
+ should "include an extension module for each (sub-)component" do
43
+ (NginxTail::LogLine::COMPONENTS + NginxTail::LogLine::SUBCOMPONENTS).each do |component|
44
+ ntail_module = NginxTail::LogLine.component_to_ntail_module(component)
45
+ assert NginxTail::LogLine.included_modules.include?(ntail_module), "module '#{ntail_module.name}' should be included"
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,38 @@
1
+ require 'helper'
2
+
3
+ class TestRemoteAddr < Test::Unit::TestCase
4
+
5
+ context "ntail" do
6
+
7
+ setup do
8
+ @remote_addr = "192.0.32.10"
9
+ @log_line = random_log_line(:remote_addr => @remote_addr)
10
+ end
11
+
12
+ should "convert the request's remote address into a host string" do
13
+ # directly via the helper function
14
+ to_host_s = NginxTail::LogLine.to_host_s(@remote_addr)
15
+ assert_equal "www.example.com", to_host_s
16
+ # parsed from a raw log line
17
+ assert_equal "www.example.com", @log_line.to_host_s
18
+ end
19
+
20
+ should "convert the request's remote address into a country string" do
21
+ # directly via the helper function
22
+ to_country_s = NginxTail::LogLine.to_country_s(@remote_addr)
23
+ assert_equal "United States", to_country_s
24
+ # parsed from a raw log line
25
+ assert_equal "United States", @log_line.to_country_s
26
+ end
27
+
28
+ should "convert the request's remote address into a city string" do
29
+ # directly via the helper function
30
+ to_city_s = NginxTail::LogLine.to_city_s(@remote_addr)
31
+ assert_equal "Marina Del Rey", to_city_s
32
+ # parsed from a raw log line
33
+ assert_equal "Marina Del Rey", @log_line.to_city_s
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,65 @@
1
+ require 'helper'
2
+
3
+ class TestRemoteUser < Test::Unit::TestCase
4
+
5
+ def teardown
6
+ # undo any changes the test may have made
7
+ NginxTail::LogLine.reset_authenticated_users
8
+ end
9
+
10
+ should "have empty list of authenticated users without configuration" do
11
+ assert NginxTail::LogLine.authenticated_users.empty?
12
+ end
13
+
14
+ should "correctly identify the default/unknown remote user" do
15
+ unknown_remote_user = NginxTail::RemoteUser::UNKNOWN_REMOTE_USER
16
+ log_line = random_log_line(:remote_user => unknown_remote_user)
17
+
18
+ assert NginxTail::LogLine.unknown_remote_user?(unknown_remote_user)
19
+ assert !NginxTail::LogLine.remote_user?(unknown_remote_user)
20
+ assert !NginxTail::LogLine.authenticated_user?(unknown_remote_user)
21
+
22
+ assert log_line.unknown_remote_user?
23
+ end
24
+
25
+ should "not allow the default/unknown remote user to be added" do
26
+ assert_raise RuntimeError do
27
+ NginxTail::LogLine.add_authenticated_user(NginxTail::RemoteUser::UNKNOWN_REMOTE_USER)
28
+ end
29
+ end
30
+
31
+ should "have non-empty list of authenticated users after configuration" do
32
+ NginxTail::LogLine.add_authenticated_user(first_remote_user = "john_doe")
33
+ assert_equal 1, NginxTail::LogLine.authenticated_users.size
34
+ assert NginxTail::LogLine.authenticated_users.include?(first_remote_user)
35
+
36
+ NginxTail::LogLine.add_authenticated_user(second_referer = "jane_doe")
37
+ assert_equal 2, NginxTail::LogLine.authenticated_users.size
38
+ assert NginxTail::LogLine.authenticated_users.include?(second_referer)
39
+ end
40
+
41
+ should "avoid duplicates in list of authenticated users" do
42
+ NginxTail::LogLine.add_authenticated_user(authenticated_user = "john_doe")
43
+ assert_equal 1, NginxTail::LogLine.authenticated_users.size
44
+
45
+ NginxTail::LogLine.add_authenticated_user(authenticated_user)
46
+ assert_equal 1, NginxTail::LogLine.authenticated_users.size
47
+ end
48
+
49
+ should "recognize a remote user after configuration" do
50
+ remote_user = "john_doe"
51
+ log_line = random_log_line(:remote_user => remote_user)
52
+
53
+ assert NginxTail::LogLine.remote_user?(remote_user)
54
+ assert !NginxTail::LogLine.authenticated_user?(remote_user)
55
+ assert log_line.remote_user?
56
+ assert !log_line.authenticated_user?
57
+
58
+ NginxTail::LogLine.add_authenticated_user(remote_user)
59
+ assert NginxTail::LogLine.remote_user?(remote_user)
60
+ assert NginxTail::LogLine.authenticated_user?(remote_user)
61
+ assert log_line.remote_user?
62
+ assert log_line.authenticated_user?
63
+ end
64
+
65
+ end
@@ -0,0 +1,26 @@
1
+ require 'helper'
2
+
3
+ class TestRequest < Test::Unit::TestCase
4
+
5
+ context "ntail" do
6
+
7
+ should "do something reasonable for bad requests" do
8
+
9
+ log_line = bad_request_log_line
10
+ assert_equal "-", log_line.request
11
+
12
+ assert_nil log_line.http_method
13
+ assert_nil log_line.uri
14
+ assert_nil log_line.http_version
15
+
16
+ assert_equal "", log_line.to_http_method_s
17
+ assert_equal "-", log_line.to_uri_s
18
+ assert_equal "", log_line.to_http_version_s
19
+
20
+ assert_equal "-", log_line.to_request_s
21
+
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,26 @@
1
+ require 'helper'
2
+
3
+ class TestStatus < Test::Unit::TestCase
4
+
5
+ context "ntail" do
6
+
7
+ should "correctly identify the nginx 499 status code" do
8
+ status = NginxTail::Status::NGINX_MAGIC_STATUS # 499
9
+ # directly via the helper function
10
+ assert !NginxTail::LogLine.information_status?(status)
11
+ assert !NginxTail::LogLine.success_status?(status)
12
+ assert !NginxTail::LogLine.redirect_status?(status)
13
+ assert !NginxTail::LogLine.client_error_status?(status)
14
+ assert !NginxTail::LogLine.server_error_status?(status)
15
+ # parsed from a raw log line
16
+ log_line = random_log_line(:status => NginxTail::Status::NGINX_MAGIC_STATUS)
17
+ assert !log_line.information_status?
18
+ assert !log_line.success_status?
19
+ assert !log_line.redirect_status?
20
+ assert !log_line.client_error_status?
21
+ assert !log_line.server_error_status?
22
+ end
23
+
24
+ end
25
+
26
+ end