blob: 7fd5d66c27799cb7bd64a0cd4a0454dc66ed9dd0 [file] [log] [blame]
Andrey Zelenkovf4b101a2015-09-18 15:52:14 +03001#!/usr/bin/perl
2
3# (C) Andrey Zelenkov
4# (C) Nginx, Inc.
5
6# Tests for log module variables.
7
8###############################################################################
9
10use warnings;
11use strict;
12
13use Test::More;
14
15BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17use lib 'lib';
18use Test::Nginx;
19
20###############################################################################
21
22select STDERR; $| = 1;
23select STDOUT; $| = 1;
24
25my $t = Test::Nginx->new()->has(qw/http rewrite/)->plan(6)
26 ->write_file_expand('nginx.conf', <<'EOF');
27
28%%TEST_GLOBALS%%
29
30daemon off;
31
32events {
33}
34
35http {
36 %%TEST_GLOBALS_HTTP%%
37
38 log_format time_iso8601 '$uri $time_iso8601';
39 log_format time_local '$uri $time_local';
40 log_format msec '$uri $msec';
41 log_format request '$uri $status $request_length $request_time';
42 log_format bytes '$uri $bytes_sent $body_bytes_sent';
43 log_format pipe '$uri $pipe';
44
45 server {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030046 listen 127.0.0.1:8080;
Andrey Zelenkovf4b101a2015-09-18 15:52:14 +030047 server_name localhost;
48
49 location /iso8601 {
50 access_log %%TESTDIR%%/iso8601.log time_iso8601;
51 return 200;
52 }
53
54 location /local {
55 access_log %%TESTDIR%%/local.log time_local;
56 return 200;
57 }
58
59 location /msec {
60 access_log %%TESTDIR%%/msec.log msec;
61 return 200;
62 }
63
64 location /request {
65 access_log %%TESTDIR%%/request.log request;
66 return 200;
67 }
68
69 location /bytes {
70 access_log %%TESTDIR%%/bytes.log bytes;
71 return 200 OK;
72 }
73
74 location /pipe {
75 access_log %%TESTDIR%%/pipe.log pipe;
76 return 200;
77 }
78 }
79}
80
81EOF
82
83$t->run();
84
85###############################################################################
86
87http_get('/iso8601');
88http_get('/local');
89http_get('/msec');
90http_get('/request');
91my $bytes_sent = length http_get('/bytes');
92
93# pipelined requests
94
95http(<<EOF);
96GET /pipe HTTP/1.1
97Host: localhost
98
99GET /pipe HTTP/1.1
100Host: localhost
101Connection: close
102
103EOF
104
105$t->stop();
106
107my $log = $t->read_file('iso8601.log');
108like($log, qr!/iso8601 \d{4}-\d\d-\d\dT\d\d:\d\d:\d\d[+-]\d\d:\d\d!,
109 'time_iso8601');
110
111$log = $t->read_file('local.log');
112like($log, qr!/local \d\d/[A-Z][a-z]{2}/\d{4}:\d\d:\d\d:\d\d [+-]\d{4}!,
113 'time_local');
114
115$log = $t->read_file('msec.log');
116like($log, qr!/msec [\d\.]+!, 'msec');
117
118$log = $t->read_file('request.log');
119like($log, qr!/request 200 39 [\d\.]+!, 'request');
120
121$log = $t->read_file('bytes.log');
122is($log, "/bytes $bytes_sent 2\n", 'bytes sent');
123
124$log = $t->read_file('pipe.log');
125is($log, "/pipe .\n/pipe p\n", 'pipe');
126
127###############################################################################