xref: /lighttpd1.4/tests/core-condition.t (revision 2e0edb85)
1#!/usr/bin/env perl
2BEGIN {
3	# add current source dir to the include-path
4	# we need this for make distcheck
5	(my $srcdir = $0) =~ s,/[^/]+$,/,;
6	unshift @INC, $srcdir;
7}
8
9use strict;
10use IO::Socket;
11use Test::More tests => 25;
12use LightyTest;
13
14my $tf = LightyTest->new();
15my $t;
16
17$ENV{"env_test"} = "good_env";
18
19$tf->{CONFIGFILE} = 'condition.conf';
20ok($tf->start_proc == 0, "Starting lighttpd") or die();
21
22$t->{REQUEST}  = ( <<EOF
23GET /index.html HTTP/1.0
24Host: www.example.org
25EOF
26 );
27$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_1" } ];
28ok($tf->handle_http($t) == 0, 'config deny');
29
30$t->{REQUEST}  = ( <<EOF
31GET /index.html HTTP/1.0
32Host: test1.example.org
33EOF
34 );
35$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_2" } ];
36ok($tf->handle_http($t) == 0, '2nd child of chaining');
37
38$t->{REQUEST}  = ( <<EOF
39GET /index.html HTTP/1.0
40Host: test2.example.org
41EOF
42 );
43$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_3" } ];
44ok($tf->handle_http($t) == 0, '3rd child of chaining');
45
46$t->{REQUEST}  = ( <<EOF
47GET /index.html HTTP/1.0
48Host: test3.example.org
49EOF
50 );
51$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_5" } ];
52ok($tf->handle_http($t) == 0, 'nesting');
53
54$t->{REQUEST}  = ( <<EOF
55GET /subdir/index.html HTTP/1.0
56Host: test4.example.org
57EOF
58 );
59$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_7" } ];
60ok($tf->handle_http($t) == 0, 'url subdir');
61
62$t->{REQUEST}  = ( <<EOF
63GET /subdir/../css/index.html HTTP/1.0
64Host: test4.example.org
65EOF
66 );
67$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_6" } ];
68ok($tf->handle_http($t) == 0, 'url subdir with path traversal');
69
70$t->{REQUEST}  = ( <<EOF
71GET / HTTP/1.0
72EOF
73 );
74$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Server' => 'lighttpd-1.4.x' } ];
75ok($tf->handle_http($t) == 0, 'condition: handle if before else branches');
76
77$t->{REQUEST}  = ( <<EOF
78GET /show/other/server-tag HTTP/1.0
79EOF
80 );
81$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Server' => 'special tag' } ];
82ok($tf->handle_http($t) == 0, 'condition: handle if before else branches #2');
83
84
85## config includes
86
87$t->{REQUEST}  = ( "GET /index.html HTTP/1.0\r\nHost: www.example.org\r\n" );
88$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_1" } ];
89ok($tf->handle_http($t) == 0, 'basic test');
90
91my $myvar = "good";
92my $server_name = "test.example.org";
93my $mystr = "string";
94$mystr .= "_append";
95my $tests = {
96    "include"        => "/good_include",
97      "concat"         => "/good_" . "concat",
98      "servername1"    => "/good_" . $server_name,
99      "servername2"    => $server_name . "/good_",
100      "servername3"    => "/good_" . $server_name . "/",
101      "var.myvar"      => "/good_var_myvar" . $myvar,
102      "myvar"          => "/good_myvar" . $myvar,
103      "env"            => "/" . $ENV{"env_test"},
104
105    "number1"        => "/good_number" . "1",
106      "number2"        => "1" . "/good_number",
107      "array_append"   => "/good_array_append",
108      "string_append"  => "/good_" . $mystr,
109      "number_append"  => "/good_" . "2",
110
111    "include_shell"  => "/good_include_shell_" . "456"
112};
113
114foreach my $test (keys %{ $tests }) {
115	my $expect = $tests->{$test};
116	$t->{REQUEST}  = ( <<EOF
117GET /$test HTTP/1.0
118Host: $server_name
119EOF
120 );
121	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => $expect } ];
122	ok($tf->handle_http($t) == 0, $test);
123}
124
125ok($tf->stop_proc == 0, "Stopping lighttpd");
126