42
|
1 #!/usr/bin/perl
|
|
2
|
|
3 # (C) Maxim Dounin
|
|
4
|
|
5 # Tests for http proxy module.
|
|
6
|
|
7 ###############################################################################
|
|
8
|
|
9 use warnings;
|
|
10 use strict;
|
|
11
|
|
12 use Test::More tests => 3;
|
|
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();
|
|
25
|
|
26 $t->write_file_expand('nginx.conf', <<'EOF');
|
|
27
|
|
28 master_process off;
|
|
29 daemon off;
|
|
30
|
|
31 events {
|
|
32 worker_connections 1024;
|
|
33 }
|
|
34
|
|
35 http {
|
|
36 access_log off;
|
|
37 root %%TESTDIR%%;
|
|
38
|
|
39 client_body_temp_path %%TESTDIR%%/client_body_temp;
|
|
40 fastcgi_temp_path %%TESTDIR%%/fastcgi_temp;
|
|
41 proxy_temp_path %%TESTDIR%%/proxy_temp;
|
|
42
|
|
43 server {
|
|
44 listen localhost:8080;
|
|
45 server_name localhost;
|
|
46
|
|
47 location / {
|
|
48 proxy_pass http://localhost:8081;
|
|
49 proxy_read_timeout 1s;
|
|
50 }
|
|
51 }
|
|
52 }
|
|
53
|
|
54 EOF
|
|
55
|
|
56 $t->run_daemon(\&http_daemon);
|
|
57 $t->run();
|
|
58
|
|
59 ###############################################################################
|
|
60
|
|
61 like(http_get('/'), qr/SEE-THIS/, 'proxy request');
|
|
62 like(http_get('/multi'), qr/AND-THIS/, 'proxy request with multiple packets');
|
|
63
|
|
64 unlike(http_head('/'), qr/SEE-THIS/, 'proxy head request');
|
|
65
|
|
66 ###############################################################################
|
|
67
|
|
68 sub http_daemon {
|
|
69 my $server = IO::Socket::INET->new(
|
|
70 Proto => 'tcp',
|
|
71 LocalHost => '127.0.0.1:8081',
|
|
72 Listen => 5,
|
|
73 Reuse => 1
|
|
74 )
|
|
75 or die "Can't create listening socket: $!\n";
|
|
76
|
|
77 while (my $client = $server->accept()) {
|
|
78 $client->autoflush(1);
|
|
79
|
|
80 my $headers = '';
|
|
81 my $uri = '';
|
|
82
|
|
83 while (<$client>) {
|
|
84 $headers .= $_;
|
|
85 last if (/^\x0d?\x0a?$/);
|
|
86 }
|
|
87
|
|
88 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i;
|
|
89
|
|
90 if ($uri eq '/') {
|
|
91 print $client <<'EOF';
|
|
92 HTTP/1.1 200 OK
|
|
93 Connection: close
|
|
94
|
|
95 EOF
|
|
96 print $client "TEST-OK-IF-YOU-SEE-THIS"
|
|
97 unless $headers =~ /^HEAD/i;
|
|
98
|
|
99 } elsif ($uri eq '/multi') {
|
|
100
|
|
101 print $client <<"EOF";
|
|
102 HTTP/1.1 200 OK
|
|
103 Connection: close
|
|
104
|
|
105 TEST-OK-IF-YOU-SEE-THIS
|
|
106 EOF
|
|
107
|
|
108 select undef, undef, undef, 0.1;
|
|
109 print $client 'AND-THIS';
|
|
110
|
|
111 } else {
|
|
112
|
|
113 print $client <<"EOF";
|
|
114 HTTP/1.1 404 Not Found
|
|
115 Connection: close
|
|
116
|
|
117 Oops, '$uri' not found
|
|
118 EOF
|
|
119 }
|
|
120
|
|
121 close $client;
|
|
122 }
|
|
123 }
|
|
124
|
|
125 ###############################################################################
|