Mercurial > hg > nginx-tests
comparison access_log.t @ 394:8d436291c09b
Tests: added tests for the "access_log" directive.
author | Vladimir Homutov <vl@nginx.com> |
---|---|
date | Fri, 18 Apr 2014 17:26:52 +0400 |
parents | |
children | be98c162f8bc |
comparison
equal
deleted
inserted
replaced
393:3c9aeeb09ac8 | 394:8d436291c09b |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Nginx, Inc. | |
4 | |
5 # Tests for access_log. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 | |
14 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
15 | |
16 use lib 'lib'; | |
17 use Test::Nginx; | |
18 | |
19 ############################################################################### | |
20 | |
21 select STDERR; $| = 1; | |
22 select STDOUT; $| = 1; | |
23 | |
24 my $t = Test::Nginx->new()->has(qw/http rewrite/) | |
25 ->write_file_expand('nginx.conf', <<'EOF'); | |
26 | |
27 %%TEST_GLOBALS%% | |
28 | |
29 daemon off; | |
30 | |
31 events { | |
32 } | |
33 | |
34 http { | |
35 %%TEST_GLOBALS_HTTP%% | |
36 | |
37 log_format test "$uri:$status"; | |
38 | |
39 server { | |
40 listen 127.0.0.1:8080; | |
41 server_name localhost; | |
42 | |
43 location /combined { | |
44 access_log %%TESTDIR%%/combined.log; | |
45 return 200 OK; | |
46 | |
47 location /combined/off { | |
48 access_log off; | |
49 return 200 OK; | |
50 } | |
51 } | |
52 | |
53 location /filtered { | |
54 access_log %%TESTDIR%%/filtered.log test | |
55 if=$arg_logme; | |
56 return 200 OK; | |
57 } | |
58 | |
59 location /complex { | |
60 access_log %%TESTDIR%%/complex.log test | |
61 if=$arg_logme$arg_logmetoo; | |
62 return 200 OK; | |
63 } | |
64 | |
65 location /compressed { | |
66 access_log %%TESTDIR%%/compressed.log test | |
67 gzip buffer=1m flush=100ms; | |
68 return 200 OK; | |
69 } | |
70 | |
71 location /multi { | |
72 access_log %%TESTDIR%%/multi1.log test; | |
73 access_log %%TESTDIR%%/multi2.log test; | |
74 return 200 OK; | |
75 } | |
76 | |
77 location /varlog { | |
78 access_log %%TESTDIR%%/${arg_logname} test; | |
79 return 200 OK; | |
80 } | |
81 } | |
82 } | |
83 | |
84 EOF | |
85 | |
86 $t->try_run('no access_log if')->plan(8); | |
87 | |
88 ############################################################################### | |
89 | |
90 http_get('/combined'); | |
91 http_get('/combined/off'); | |
92 | |
93 http_get('/filtered'); | |
94 http_get('/filtered/empty?logme='); | |
95 http_get('/filtered/zero?logme=0'); | |
96 http_get('/filtered/good?logme=1'); | |
97 http_get('/filtered/work?logme=yes'); | |
98 | |
99 http_get('/complex'); | |
100 http_get('/complex/one?logme=1'); | |
101 http_get('/complex/two?logmetoo=1'); | |
102 http_get('/complex/either1?logme=A&logmetoo=B'); | |
103 http_get('/complex/either2?logme=A'); | |
104 http_get('/complex/either3?logmetoo=B'); | |
105 http_get('/complex/either4?logme=0&logmetoo=0'); | |
106 http_get('/complex/neither?logme=&logmetoo='); | |
107 | |
108 http_get('/compressed'); | |
109 | |
110 http_get('/multi'); | |
111 | |
112 http_get('/varlog'); | |
113 http_get('/varlog?logname='); | |
114 http_get('/varlog?logname=0'); | |
115 http_get('/varlog?logname=filename'); | |
116 | |
117 | |
118 select undef, undef, undef, 0.1; | |
119 | |
120 # verify that "gzip" parameter turns on compression | |
121 | |
122 my $log; | |
123 | |
124 SKIP: { | |
125 eval { require IO::Uncompress::Gunzip; }; | |
126 skip("IO::Uncompress::Gunzip not installed", 1) if $@; | |
127 | |
128 my $gzipped = read_file($t, 'compressed.log'); | |
129 IO::Uncompress::Gunzip::gunzip(\$gzipped => \$log); | |
130 is($log, "/compressed:200\n", 'compressed log - flush time'); | |
131 } | |
132 | |
133 # now verify all other logs | |
134 | |
135 $t->stop(); | |
136 | |
137 | |
138 # verify that by default, 'combined' format is used, 'off' disables logging | |
139 | |
140 $log = read_file($t, 'combined.log'); | |
141 like($log, | |
142 qr!^\Q127.0.0.1 - - [\E .* | |
143 \Q] "GET /combined HTTP/1.0" 200 2 "-" "-"\E$!x, | |
144 'default log format'); | |
145 | |
146 # verify that log filtering works | |
147 | |
148 $log = read_file($t, 'filtered.log'); | |
149 is($log, "/filtered/good:200\n/filtered/work:200\n", 'log filtering'); | |
150 | |
151 | |
152 # verify "if=" argument works with complex value | |
153 | |
154 my $exp_complex = <<'EOF'; | |
155 /complex/one:200 | |
156 /complex/two:200 | |
157 /complex/either1:200 | |
158 /complex/either2:200 | |
159 /complex/either3:200 | |
160 /complex/either4:200 | |
161 EOF | |
162 | |
163 $log = read_file($t, 'complex.log'); | |
164 is($log, $exp_complex, 'if with complex value'); | |
165 | |
166 | |
167 # multiple logs in a same location | |
168 | |
169 $log = read_file($t, 'multi1.log'); | |
170 is($log, "/multi:200\n", 'multiple logs 1'); | |
171 | |
172 # same content in the second log | |
173 | |
174 $log = read_file($t, 'multi2.log'); | |
175 is($log, "/multi:200\n", 'multiple logs 2'); | |
176 | |
177 | |
178 # test log destinations with variables | |
179 | |
180 $log = read_file($t, '0'); | |
181 is($log, "/varlog:200\n", 'varlog literal zero name'); | |
182 | |
183 $log = read_file($t, 'filename'); | |
184 is($log, "/varlog:200\n", 'varlog good name'); | |
185 | |
186 ############################################################################### | |
187 | |
188 sub read_file { | |
189 my ($t, $file) = @_; | |
190 my $path = $t->testdir() . '/' . $file; | |
191 | |
192 open my $fh, '<', $path or return "$!"; | |
193 local $/; | |
194 my $content = <$fh>; | |
195 close $fh; | |
196 return $content; | |
197 } | |
198 | |
199 ############################################################################### |