comparison request_id.t @ 916:f2b2633a3fc1

Tests: added tests for request_id variable.
author Andrey Zelenkov <zelenkov@nginx.com>
date Mon, 18 Apr 2016 17:40:08 +0300
parents
children 943a5952ee5e
comparison
equal deleted inserted replaced
915:1ffb16747167 916:f2b2633a3fc1
1 #!/usr/bin/perl
2
3 # (C) Andrey Zelenkov
4 # (C) Nginx, Inc
5
6 # Tests for request_id variable.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http rewrite ssi/)
26 ->write_file_expand('nginx.conf', <<'EOF');
27
28 %%TEST_GLOBALS%%
29
30 daemon off;
31
32 events {
33 }
34
35 http {
36 %%TEST_GLOBALS_HTTP%%
37
38 log_format id $request_id;
39
40 server {
41 listen 127.0.0.1:8080;
42 server_name localhost;
43
44 add_header X-Request-Id $request_id;
45 add_header X-blah blah;
46
47 location / {
48 ssi on;
49 }
50 location /body {
51 return 200 $request_id;
52 }
53 location /log {
54 access_log %%TESTDIR%%/id.log id;
55 return 200;
56 }
57 }
58 }
59
60 EOF
61
62 $t->write_file('index.html', '');
63 $t->write_file('add.html', '<!--#include virtual="/body" -->');
64 $t->try_run('no request_id variable support')->plan(12);
65
66 ###############################################################################
67
68 my ($id1) = http_get('/') =~ qr/^X-Request-Id: (.*)\x0d/m;
69 my ($id2) = http_get('/') =~ qr/^X-Request-Id: (.*)\x0d/m;
70
71 like($id1, qr/^[a-z0-9]{32}$/, 'format id 1');
72 like($id2, qr/^[a-z0-9]{32}$/, 'format id 2');
73
74 isnt($id1, $id2, 'different id');
75
76 # same request
77
78 ($id1, $id2) = http_get('/body')
79 =~ qr/^X-Request-Id: (.*?)\x0d.*\x0d\x0a(.*)/ms;
80
81 like($id1, qr/^[a-z0-9]{32}$/, 'format id 1 - same');
82 like($id2, qr/^[a-z0-9]{32}$/, 'format id 2 - same');
83
84 is($id1, $id2, 'equal id - same');
85
86 # subrequest
87
88 ($id1, $id2) = http_get('/add.html')
89 =~ qr/^X-Request-Id: (.*?)\x0d.*\x0d\x0a(.*)/ms;
90
91 like($id1, qr/^[a-z0-9]{32}$/, 'format id 1 - sub');
92 like($id2, qr/^[a-z0-9]{32}$/, 'format id 2 - sub');
93
94 is($id1, $id2, 'equal id - sub');
95
96 # log
97
98 ($id1) = http_get('/log') =~ qr/^X-Request-Id: (.*)\x0d/m;
99
100 $t->stop();
101
102 $id2 = $t->read_file('/id.log');
103 chomp $id2;
104
105 like($id1, qr/^[a-z0-9]{32}$/, 'format id 1 - log');
106 like($id2, qr/^[a-z0-9]{32}$/, 'format id 2 - log');
107
108 is($id1, $id2, 'equal id - log');
109
110 ###############################################################################