Skip to content

Commit 3481680

Browse files
committed
Add tests
1 parent e299d8e commit 3481680

File tree

6 files changed

+632
-0
lines changed

6 files changed

+632
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/t/servroot

.travis.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
sudo: false
2+
language: c
3+
compiler: gcc
4+
dist: trusty
5+
6+
cache:
7+
directories:
8+
- perl5
9+
- dl
10+
11+
env:
12+
global:
13+
- TESTNGINX_VER=12152a5
14+
- PATH=$TRAVIS_BUILD_DIR/bin:$TRAVIS_BUILD_DIR/nginx/objs:$PATH
15+
matrix:
16+
- NGINX_VERSION=1.9.15
17+
- NGINX_VERSION=1.11.13
18+
- NGINX_VERSION=1.12.2
19+
- NGINX_VERSION=1.13.8
20+
21+
before_install:
22+
- curl --version
23+
- mkdir -p {bin,dl}
24+
- if [ ! -f dl/nginx-${NGINX_VERSION}.tar.gz ]; then curl -o dl/nginx-${NGINX_VERSION}.tar.gz -L http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz; fi
25+
- if [ ! -f dl/test-nginx-${TESTNGINX_VER}.tar.gz ]; then curl -o dl/test-nginx-${TESTNGINX_VER}.tar.gz -L "https://github.com/openresty/test-nginx/archive/${TESTNGINX_VER}.tar.gz"; fi
26+
- if [ ! -f dl/cpanm ]; then curl -o dl/cpanm https://cpanmin.us/; chmod +x dl/cpanm; fi
27+
- cp dl/cpanm bin/cpanm
28+
- tar -zxvf dl/nginx-${NGINX_VERSION}.tar.gz && mv nginx-${NGINX_VERSION} nginx
29+
- cpanm --notest --local-lib=perl5 local::lib && eval $(perl -I perl5/lib/perl5/ -Mlocal::lib)
30+
- cpanm --notest dl/test-nginx-${TESTNGINX_VER}.tar.gz
31+
- cpanm --notest Test::File
32+
- cpanm --notest URI::Query
33+
- cd nginx
34+
- ./configure --with-http_v2_module --with-http_ssl_module --add-module=..
35+
- make
36+
- cd ..
37+
38+
script:
39+
- prove -r t

t/error_handling.t

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use strict;
2+
use warnings;
3+
4+
use File::Basename qw(dirname);
5+
6+
use lib dirname(__FILE__) . "/lib";
7+
8+
use Test::Nginx::Socket tests => 2;
9+
use Test::More;
10+
use Test::Nginx::UploadModule;
11+
12+
no_long_string();
13+
no_shuffle();
14+
run_tests();
15+
16+
__DATA__
17+
=== TEST 1: invalid content-range
18+
--- config
19+
location /upload/ {
20+
upload_pass @upstream;
21+
upload_resumable on;
22+
upload_set_form_field "upload_tmp_path" "$upload_tmp_path";
23+
upload_cleanup 400 404 499 500-505;
24+
}
25+
--- more_headers
26+
X-Content-Range: bytes 0-3/4
27+
X-Progress-ID: 0000000001
28+
Session-ID: 0000000001
29+
Content-Type: text/plain
30+
Content-Disposition: form-data; name="file"; filename="test.txt"\r
31+
--- request eval
32+
qq{POST /upload/
33+
testing}
34+
--- error_code: 416
35+
--- extra_tests eval
36+
use Test::File qw(file_not_exists_ok);
37+
sub {
38+
my $block = shift;
39+
file_not_exists_ok(
40+
"${ENV{TEST_NGINX_UPLOAD_PATH}}/store/1/0000000001", $block->name . '- tmp file deleted');
41+
}

t/lib/Test/Nginx/UploadModule.pm

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package Test::Nginx::UploadModule;
2+
use v5.10.1;
3+
use strict;
4+
use warnings;
5+
6+
my $PORT = $ENV{TEST_NGINX_UPSTREAM_PORT} ||= 12345;
7+
$ENV{TEST_NGINX_UPLOAD_PATH} ||= '/tmp/upload';
8+
9+
10+
use base 'Exporter';
11+
12+
use Test::Nginx::Socket;
13+
use Test::Nginx::Util qw($RunTestHelper);
14+
use Test::File qw(file_contains_like);
15+
use Test::More;
16+
17+
use File::Path qw(rmtree mkpath);
18+
use Test::Nginx::UploadModule::TestServer;
19+
20+
21+
my ($server_pid, $server);
22+
23+
sub kill_tcp_server() {
24+
$server->shutdown if defined $server;
25+
undef $server;
26+
kill INT => $server_pid if defined $server_pid;
27+
undef $server_pid;
28+
}
29+
30+
sub make_upload_paths {
31+
mkpath("${ENV{TEST_NGINX_UPLOAD_PATH}}/stats");
32+
for (my $i = 0; $i < 10; $i++) {
33+
mkpath("${ENV{TEST_NGINX_UPLOAD_PATH}}/store/$i");
34+
}
35+
}
36+
37+
add_cleanup_handler(sub {
38+
kill_tcp_server();
39+
rmtree($ENV{TEST_NGINX_UPLOAD_PATH});
40+
});
41+
42+
my $OldRunTestHelper = $RunTestHelper;
43+
44+
my @ResponseChecks = ();
45+
46+
my $old_check_response_headers = \&Test::Nginx::Socket::check_response_headers;
47+
48+
sub new_check_response_headers ($$$$$) {
49+
my ($block, $res, $raw_headers, $dry_run, $req_idx, $need_array) = @_;
50+
$old_check_response_headers->(@_);
51+
if (!$dry_run) {
52+
for my $check (@ResponseChecks) {
53+
$check->(@_);
54+
}
55+
}
56+
}
57+
58+
$RunTestHelper = sub ($$) {
59+
if (defined $server) {
60+
$OldRunTestHelper->(@_);
61+
} else {
62+
defined (my $pid = fork()) or bail_out "Can't fork: $!";
63+
if ($pid == 0) {
64+
$Test::Nginx::Util::InSubprocess = 1;
65+
if (!defined $server) {
66+
$server = Test::Nginx::UploadModule::TestServer->new({port=>$PORT});
67+
$server->run();
68+
exit 0;
69+
}
70+
} else {
71+
$server_pid = $pid;
72+
no warnings qw(redefine);
73+
Test::Nginx::UploadModule::TestServer::wait_for_port($PORT, \&bail_out);
74+
*Test::Nginx::Socket::check_response_headers = \&new_check_response_headers;
75+
76+
$OldRunTestHelper->(@_);
77+
78+
*Test::Nginx::Socket::check_response_headers = &$old_check_response_headers;
79+
80+
kill_tcp_server();
81+
}
82+
}
83+
};
84+
85+
my $default_http_config = <<'_EOC_';
86+
upstream upload_upstream_server {
87+
server 127.0.0.1:$TEST_NGINX_UPSTREAM_PORT;
88+
}
89+
90+
log_format custom '$remote_addr - $remote_user [$time_local] '
91+
'"$request" $status $body_bytes_sent '
92+
'"$http_referer" "$http_user_agent" $request_time';
93+
_EOC_
94+
95+
96+
my $default_config = <<'_EOC_';
97+
location @upstream {
98+
internal;
99+
proxy_pass http://upload_upstream_server;
100+
}
101+
upload_store $TEST_NGINX_UPLOAD_PATH/store 1;
102+
upload_state_store $TEST_NGINX_UPLOAD_PATH/stats;
103+
104+
access_log $TEST_NGINX_SERVER_ROOT/logs/access.log custom;
105+
_EOC_
106+
107+
# Set default configs, create upload directories, and add extra_tests blocks to @ResponseChecks
108+
add_block_preprocessor(sub {
109+
my $block = shift;
110+
111+
make_upload_paths();
112+
113+
if (!defined $block->http_config) {
114+
$block->set_value('http_config', $default_http_config);
115+
} else {
116+
$block->set_value('http_config', $default_http_config . $block->http_config);
117+
}
118+
if (defined $block->config) {
119+
$block->set_value('config', $default_config . $block->config);
120+
}
121+
if (defined $block->extra_tests) {
122+
if (ref $block->extra_tests ne 'CODE') {
123+
bail_out('extra_tests should be a subroutine, instead found ' . $block->extra_tests);
124+
}
125+
126+
push(@ResponseChecks, $block->extra_tests);
127+
}
128+
});
129+
130+
# Add 'upload_file_like' block check
131+
add_response_body_check(sub {
132+
my ($block, $body, $req_idx, $repeated_req_idx, $dry_run) = @_;
133+
134+
if ($dry_run) {
135+
return;
136+
}
137+
138+
my $num_requests = (ref $block->request eq 'ARRAY') ? scalar @{$block->request} : 1;
139+
my $final_request = ($req_idx == ($num_requests - 1));
140+
if ($final_request && defined $block->upload_file_like) {
141+
my $ref_type = (ref $block->upload_file_like);
142+
if (ref $block->upload_file_like ne 'Regexp') {
143+
bail_out("upload_file_like block must be a regex pattern");
144+
}
145+
my $test_name = $block->name . " - upload file check";
146+
if ($body =~ /upload_tmp_path = ([^\n]+)$/) {
147+
file_contains_like($1, $block->upload_file_like, $test_name);
148+
} else {
149+
bail_out("upload_tmp_path information not found in response");
150+
}
151+
}
152+
return $block;
153+
});
154+
155+
1;

0 commit comments

Comments
 (0)