changeset 141:1e1975cd25ef

Tests: error_page and return related tests, dav tests.
author Maxim Dounin <mdounin@mdounin.ru>
date Tue, 02 Nov 2010 06:49:42 +0300
parents 3f246a1be2b0
children d732aaa5f370
files dav.t http-error-page.t rewrite.t
diffstat 3 files changed, 410 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/dav.t
@@ -0,0 +1,129 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Tests for nginx dav module.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+my $t = Test::Nginx->new()->has(qw/http dav/)->plan(11);
+
+$t->write_file_expand('nginx.conf', <<'EOF');
+
+%%TEST_GLOBALS%%
+
+master_process off;
+daemon         off;
+
+events {
+}
+
+http {
+    %%TEST_GLOBALS_HTTP%%
+
+    server {
+        listen       127.0.0.1:8080;
+        server_name  localhost;
+
+        location / {
+            dav_methods PUT DELETE MKCOL COPY MOVE;
+        }
+    }
+}
+
+EOF
+
+$t->run();
+
+###############################################################################
+
+my $r;
+
+$r = http(<<EOF . '0123456789');
+PUT /file HTTP/1.1
+Host: localhost
+Connection: close
+Content-Length: 10
+
+EOF
+
+like($r, qr/201 Created.*(Content-Length|\x0d\0a0\x0d\x0a)/ms, 'put file');
+is(-s $t->testdir() . '/file', 10, 'put file size');
+
+$r = http(<<EOF);
+PUT /file HTTP/1.1
+Host: localhost
+Connection: close
+Content-Length: 0
+
+EOF
+
+like($r, qr/204 No Content/, 'put file again');
+unlike($r, qr/Content-Length|Transfer-Encoding/, 'no length in 204');
+is(-s $t->testdir() . '/file', 0, 'put file again size');
+
+$r = http(<<EOF);
+DELETE /file HTTP/1.1
+Host: localhost
+Connection: close
+Content-Length: 0
+
+EOF
+
+like($r, qr/204 No Content/, 'delete file');
+unlike($r, qr/Content-Length|Transfer-Encoding/, 'no length in 204');
+ok(!-f $t->testdir() . '/file', 'file deleted');
+
+TODO: {
+local $TODO = 'broken in 0.8.32';
+
+# 201 replies contain body, response should indicate it's empty
+# before 0.8.32 chunked was explicitly disabled for 201 replies so
+# connection was just closed (which isn't perfect but worked)
+
+$r = http(<<EOF);
+MKCOL /test/ HTTP/1.1
+Host: localhost
+Connection: close
+
+EOF
+
+like($r, qr/201 Created.*(Content-Length|\x0d\0a0\x0d\x0a)/ms, 'mkcol');
+
+$r = http(<<EOF);
+COPY /test/ HTTP/1.1
+Host: localhost
+Destination: /test-moved/
+Connection: close
+
+EOF
+
+like($r, qr/201 Created.*(Content-Length|\x0d\0a0\x0d\x0a)/ms, 'copy dir');
+
+$r = http(<<EOF);
+MOVE /test/ HTTP/1.1
+Host: localhost
+Destination: /test-moved/
+Connection: close
+
+EOF
+
+like($r, qr/201.*(Content-Length|\x0d\0a0\x0d\x0a)/ms, 'move dir');
+}
+
+###############################################################################
new file mode 100644
--- /dev/null
+++ b/http-error-page.t
@@ -0,0 +1,137 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Tests for error_page directive.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+my $t = Test::Nginx->new()->has(qw/http rewrite/)->plan(7)
+	->write_file_expand('nginx.conf', <<'EOF');
+
+%%TEST_GLOBALS%%
+
+master_process off;
+daemon         off;
+
+events {
+}
+
+http {
+    %%TEST_GLOBALS_HTTP%%
+
+    server {
+        listen       127.0.0.1:8080;
+        server_name  localhost;
+
+        location /redirect200 {
+            error_page 404 =200 http://example.com/;
+            return 404;
+        }
+
+        location /redirect497 {
+            # 497 implies implicit status code change
+            error_page 497 https://example.com/;
+            return 497;
+        }
+
+        location /error302redirect {
+            error_page 302 http://example.com/;
+            return 302 "first";
+        }
+
+        location /error302return302text {
+            error_page 302 /return302text;
+            return 302 "first";
+        }
+
+        location /return302text {
+            return 302 "http://example.com/";
+        }
+
+        location /error302rewrite {
+            error_page 302 /rewrite;
+            return 302 "first";
+        }
+
+        location /rewrite {
+            rewrite ^ http://example.com/;
+        }
+
+        location /error302directory {
+            error_page 302 /directory;
+            return 302 "first";
+        }
+
+        location /directory {
+        }
+
+        location /error302auto {
+            error_page 302 /auto;
+            return 302 "first";
+        }
+
+        location /auto/ {
+            proxy_pass http://127.0.0.1:8081;
+        }
+    }
+}
+
+EOF
+
+mkdir($t->testdir() . '/directory');
+
+$t->run();
+
+###############################################################################
+
+TODO: {
+local $TODO = 'not yet';
+
+# tests for error_page status code change for redirects. problems
+# introduced in 0.8.53.
+
+like(http_get('/redirect200'), qr!HTTP!, 'redirect 200');
+like(http_get('/redirect497'), qr!HTTP/1.1 302!, 'redirect 497');
+
+# various tests to see if old location cleared if we happen to redirect
+# again in error_page 302
+
+like(http_get('/error302redirect'),
+	qr{HTTP/1.1 302(?!.*Location: first).*Location: http://example.com/}ms,
+	'error 302 redirect - old location cleared');
+
+like(http_get('/error302return302text'),
+	qr{HTTP/1.1 302(?!.*Location: first).*Location: http://example.com/}ms,
+	'error 302 return 302 text - old location cleared');
+
+like(http_get('/error302rewrite'),
+	qr{HTTP/1.1 302(?!.*Location: first).*Location: http://example.com/}ms,
+	'error 302 rewrite - old location cleared');
+
+like(http_get('/error302directory'),
+	qr{HTTP/1.1 301(?!.*Location: first).*Location: http://}ms,
+	'error 302 directory redirect - old location cleared');
+
+like(http_get('/error302auto'),
+	qr{HTTP/1.1 301(?!.*Location: first).*Location: http://}ms,
+	'error 302 auto redirect - old location cleared');
+
+}
+
+###############################################################################
--- a/rewrite.t
+++ b/rewrite.t
@@ -21,7 +21,7 @@ use Test::Nginx;
 select STDERR; $| = 1;
 select STDOUT; $| = 1;
 
-my $t = Test::Nginx->new()->has(qw/http rewrite/)->plan(5)
+my $t = Test::Nginx->new()->has(qw/http rewrite/)->plan(17)
 	->write_file_expand('nginx.conf', <<'EOF');
 
 %%TEST_GLOBALS%%
@@ -50,11 +50,74 @@ http {
         location /no {
             rewrite ^ http://example.com/?c=d? redirect;
         }
+
+        location /return204 {
+            return 204;
+        }
+
+        location /return200 {
+            return 200;
+        }
+
+        location /error405return204 {
+            error_page 405 /return204;
+            return 405;
+        }
+
+        location /error405return200 {
+            error_page 405 /return200;
+            return 405;
+        }
+
+        location /file.html {
+        }
+
+        location /return200text {
+            return 200 "text";
+        }
+
+        location /return404text {
+            return 404 "text";
+        }
+
+        location /return302text {
+            return 302 "text";
+        }
+
+        location /error405return200text {
+            error_page 405 /return200text;
+            return 405;
+        }
+
+        location /error302return200text {
+            error_page 302 /return200text;
+            return 302 "text";
+        }
+
+        location /error405return302text {
+            error_page 405 /return302text;
+            return 405;
+        }
+
+        location /error405rewrite {
+            error_page 405 /;
+            return 405;
+        }
+
+        location /error405directory {
+            error_page 405 /directory;
+            return 405;
+        }
+
+        location /directory {
+        }
     }
 }
 
 EOF
 
+mkdir($t->testdir() . '/directory');
+
 $t->run();
 
 ###############################################################################
@@ -71,4 +134,84 @@ like(http_get('/add?a=b'), qr!^Location:
 like(http_get('/no?a=b'), qr!^Location: http://example.com/\?c=d\x0d?$!ms,
 	'no args with args');
 
+like(http_get('/return204'), qr!204 No Content!, 'return 204');
+like(http_get('/return200'), qr!200 OK!, 'return 200');
+
+TODO: {
+local $TODO = 'not yet';
+
+# status code should be 405, and entity body is expected (vs. normal 204
+# replies which doesn't expect to have body); use HTTP/1.1 for test
+# to make problem clear
+
+my $r = http(<<EOF);
+GET /error405return204 HTTP/1.1
+Host: localhost
+Connection: close
+
+EOF
+
+like($r, qr/HTTP\/1.1 405.*(Content-Length|\x0d\0a0\x0d\x0a)/ms,
+	'error 405 return 204');
+
+# the same test, but with return 200.  this doesn't have special
+# handling and returns builtin error page body (the same problem as
+# in /error405return200text below)
+
+like(http_get('/error405return200'), qr/HTTP\/1.1 405(?!.*body)/ms,
+	'error 405 return 200');
+
+}
+
+# tests involving return with two arguments, as introduced in
+# 0.8.42
+
+like(http_get('/return200text'), qr!text\z!, 'return 200 text');
+like(http_get('/return404text'), qr!text\z!, 'return 404 text');
+
+TODO: {
+local $TODO = 'not yet';
+
+like(http_get('/error405return200text'), qr!HTTP/1.1 405.*text\z!ms,
+	'error 405 to return 200 text');
+
+}
+
+# return 302 is somewhat special: it adds Location header instead of
+# body text.  additionally it doesn't sent reply directly (as it's done for
+# other returns since 0.8.42) but instead returns NGX_HTTP_* code
+
+like(http_get('/return302text'), qr!HTTP/1.1 302.*Location: text!ms,
+	'return 302 text');
+
+TODO: {
+local $TODO = 'not yet';
+
+like(http_get('/error302return200text'),
+	qr!HTTP/1.1 302.*Location: text.*text\z!ms,
+	'error 302 return 200 text');
+
+}
+
+TODO: {
+local $TODO = 'not yet';
+
+# in contrast to other return's this shouldn't preserve original status code
+# from error, and the same applies to "rewrite ... redirect" as an error
+# handler; both should in line with e.g. directory redirect as well
+
+like(http_get('/error405return302text'),
+	qr!HTTP/1.1 302.*Location: text!ms,
+	'error 405 return 302 text');
+
+like(http_get('/error405rewrite'),
+	qr!HTTP/1.1 302.*Location: http://example.com/!ms,
+	'error 405 rewrite redirect');
+
+}
+
+like(http_get('/error405directory'),
+	qr!HTTP/1.1 301.*Location: http://!ms,
+	'error 405 directory redirect');
+
 ###############################################################################