comparison proxy.t @ 42:7435db149168

Tests: basic proxy tests.
author Maxim Dounin <mdounin@mdounin.ru>
date Sat, 01 Nov 2008 19:39:26 +0300
parents
children 05175c8b88dc
comparison
equal deleted inserted replaced
41:1b3c22a87e5d 42:7435db149168
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 use IO::Select;
15
16 BEGIN { use FindBin; chdir($FindBin::Bin); }
17
18 use lib 'lib';
19 use Test::Nginx;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 my $t = Test::Nginx->new();
27
28 $t->write_file_expand('nginx.conf', <<'EOF');
29
30 master_process off;
31 daemon off;
32
33 events {
34 worker_connections 1024;
35 }
36
37 http {
38 access_log off;
39 root %%TESTDIR%%;
40
41 client_body_temp_path %%TESTDIR%%/client_body_temp;
42 fastcgi_temp_path %%TESTDIR%%/fastcgi_temp;
43 proxy_temp_path %%TESTDIR%%/proxy_temp;
44
45 server {
46 listen localhost:8080;
47 server_name localhost;
48
49 location / {
50 proxy_pass http://localhost:8081;
51 proxy_read_timeout 1s;
52 }
53 }
54 }
55
56 EOF
57
58 $t->run_daemon(\&http_daemon);
59 $t->run();
60
61 ###############################################################################
62
63 like(http_get('/'), qr/SEE-THIS/, 'proxy request');
64 like(http_get('/multi'), qr/AND-THIS/, 'proxy request with multiple packets');
65
66 unlike(http_head('/'), qr/SEE-THIS/, 'proxy head request');
67
68 ###############################################################################
69
70 sub http_daemon {
71 my $server = IO::Socket::INET->new(
72 Proto => 'tcp',
73 LocalHost => '127.0.0.1:8081',
74 Listen => 5,
75 Reuse => 1
76 )
77 or die "Can't create listening socket: $!\n";
78
79 while (my $client = $server->accept()) {
80 $client->autoflush(1);
81
82 my $headers = '';
83 my $uri = '';
84
85 while (<$client>) {
86 $headers .= $_;
87 last if (/^\x0d?\x0a?$/);
88 }
89
90 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i;
91
92 if ($uri eq '/') {
93 print $client <<'EOF';
94 HTTP/1.1 200 OK
95 Connection: close
96
97 EOF
98 print $client "TEST-OK-IF-YOU-SEE-THIS"
99 unless $headers =~ /^HEAD/i;
100
101 } elsif ($uri eq '/multi') {
102
103 print $client <<"EOF";
104 HTTP/1.1 200 OK
105 Connection: close
106
107 TEST-OK-IF-YOU-SEE-THIS
108 EOF
109
110 select undef, undef, undef, 0.1;
111 print $client 'AND-THIS';
112
113 } else {
114
115 print $client <<"EOF";
116 HTTP/1.1 404 Not Found
117 Connection: close
118
119 Oops, '$uri' not found
120 EOF
121 }
122
123 close $client;
124 }
125 }
126
127 ###############################################################################