comparison image_filter.t @ 398:077ffeac825c

Tests: image filter tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Wed, 14 May 2014 12:18:38 +0400
parents
children 2711722a85c3
comparison
equal deleted inserted replaced
397:847ea345becb 398:077ffeac825c
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for image filter module.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 use Socket qw/CRLF/;
16
17 BEGIN { use FindBin; chdir($FindBin::Bin); }
18
19 use lib 'lib';
20 use Test::Nginx;
21
22 ###############################################################################
23
24 select STDERR; $| = 1;
25 select STDOUT; $| = 1;
26
27 eval { require GD; };
28 plan(skip_all => 'GD not installed') if $@;
29
30 my $t = Test::Nginx->new()->has(qw/http proxy map image/)->plan(39)
31 ->write_file_expand('nginx.conf', <<'EOF');
32
33 %%TEST_GLOBALS%%
34
35 daemon off;
36
37 events {
38 }
39
40 http {
41 %%TEST_GLOBALS_HTTP%%
42
43 map $arg_w $w {
44 "" '-';
45 default $arg_w;
46 }
47 map $arg_h $h {
48 "" '-';
49 default $arg_h;
50 }
51
52 server {
53 listen 127.0.0.1:8080;
54 server_name localhost;
55
56 location /size {
57 image_filter size;
58 alias %%TESTDIR%%/;
59 }
60
61 location /test {
62 image_filter test;
63 alias %%TESTDIR%%/;
64
65 location /test/off {
66 image_filter off;
67 alias %%TESTDIR%%/;
68 }
69 }
70
71 location /resize {
72 image_filter resize 10 12;
73 alias %%TESTDIR%%/;
74 }
75 location /resize1 {
76 image_filter resize 10 -;
77 alias %%TESTDIR%%/;
78 }
79 location /resize2 {
80 image_filter resize - 12;
81 alias %%TESTDIR%%/;
82 }
83 location /resize_var {
84 image_filter resize $w $h;
85 alias %%TESTDIR%%/;
86 }
87
88 location /rotate {
89 image_filter rotate 90;
90 alias %%TESTDIR%%/;
91 }
92 location /rotate_var {
93 image_filter rotate $arg_r;
94 alias %%TESTDIR%%/;
95 }
96
97 location /crop {
98 image_filter crop 60 80;
99 alias %%TESTDIR%%/;
100 }
101 location /crop_var {
102 image_filter crop $arg_w $arg_h;
103 alias %%TESTDIR%%/;
104 }
105 location /crop_rotate {
106 image_filter crop $arg_w $arg_h;
107 image_filter rotate $arg_r;
108 alias %%TESTDIR%%/;
109 }
110 location /resize_rotate {
111 image_filter resize $w $h;
112 image_filter rotate $arg_r;
113 alias %%TESTDIR%%/;
114
115 location /resize_rotate/resize {
116 image_filter resize 10 12;
117 alias %%TESTDIR%%/;
118 }
119 }
120
121 location /interlaced {
122 image_filter resize 10 12;
123 image_filter_interlace on;
124 alias %%TESTDIR%%/;
125 }
126
127 location /nontransparent {
128 image_filter resize 10 12;
129 image_filter_transparency off;
130 alias %%TESTDIR%%/;
131 }
132
133 location /quality {
134 image_filter resize 10 12;
135 image_filter_jpeg_quality 50;
136 alias %%TESTDIR%%/;
137 }
138 location /quality_var {
139 image_filter resize 10 12;
140 image_filter_jpeg_quality $arg_q;
141 alias %%TESTDIR%%/;
142
143 location /quality_var/quality {
144 image_filter_jpeg_quality 60;
145 alias %%TESTDIR%%/;
146 }
147 }
148
149 location /buffer {
150 image_filter test;
151 image_filter_buffer 1k;
152 alias %%TESTDIR%%/;
153 }
154 location /proxy_buffer {
155 image_filter rotate 90;
156 image_filter_buffer 20;
157 proxy_pass http://127.0.0.1:8081/;
158 proxy_buffering off;
159 proxy_buffer_size 512;
160 }
161 }
162 }
163
164 EOF
165
166
167 my $im = new GD::Image(100, 120);
168 my $white = $im->colorAllocate(255, 255, 255);
169 my $black = $im->colorAllocate(0, 0, 0);
170
171 $im->transparent($white);
172 $im->rectangle(0, 0, 99, 99, $black);
173
174 $t->write_file('jpeg', $im->jpeg);
175 $t->write_file('gif', $im->gif);
176 $t->write_file('png', $im->png);
177 $t->write_file('txt', 'SEE-THIS');
178
179 $t->run_daemon(\&http_daemon, $t->testdir());
180 $t->run()->waitforsocket('127.0.0.1:8081');
181
182 ###############################################################################
183
184 like(http_head('/test/gif'), qr/200 OK/, 'test');
185 like(http_head('/test/gif'), qr!Content-Type: image/gif!, 'test content-type');
186 like(http_get('/test/txt'), qr/415 Unsupported/, 'test fail');
187 like(http_get('/test/off/txt'), qr/SEE-THIS/, 'off');
188
189 is(http_get_body('/size/txt'), '{}' . CRLF, 'size wrong type');
190 like(http_head('/size/txt'), qr!Content-Type: application/json!,
191 'size content-type');
192 like(http_get('/size/jpeg'), qr/"width": 100/, 'size width');
193 like(http_get('/size/jpeg'), qr/"height": 120/, 'size height');
194 like(http_get('/size/jpeg'), qr/"type": "jpeg"/, 'size jpeg');
195 like(http_get('/size/gif'), qr/"type": "gif"/, 'size gif');
196 like(http_get('/size/png'), qr/"type": "png"/, 'size png');
197
198 is(gif_size('/resize/gif'), '10 12', 'resize');
199 is(gif_size('/resize1/gif'), '10 12', 'resize 1');
200 is(gif_size('/resize2/gif'), '10 12', 'resize 2');
201
202 is(gif_size('/resize_var/gif?w=10&h=12'), '10 12', 'resize var');
203 is(gif_size('/resize_var/gif?w=10'), '10 12', 'resize var 1');
204 is(gif_size('/resize_var/gif?h=12'), '10 12', 'resize var 2');
205
206 is(gif_size('/rotate/gif?r=90'), '120 100', 'rotate');
207 is(gif_size('/rotate_var/gif?r=180'), '100 120', 'rotate var 1');
208 is(gif_size('/rotate_var/gif?r=270'), '120 100', 'rotate var 2');
209
210 $im = GD::Image->newFromGifData(http_get_body('/gif'));
211 is($im->interlaced, 0, 'gif interlaced off');
212 is($im->transparent, 0, 'gif transparent white');
213
214 $im = GD::Image->newFromGifData(http_get_body('/interlaced/gif'));
215 is($im->interlaced, 1, 'gif interlaced on');
216
217 $im = GD::Image->newFromGifData(http_get_body('/nontransparent/gif'));
218 is($im->transparent, -1, 'gif transparent loss');
219
220 $im = GD::Image->newFromPngData(http_get_body('/png'));
221 is($im->interlaced, 0, 'png interlaced off');
222 is($im->transparent, 0, 'png transparent white');
223
224 # this test produces libpng warning on STDERR:
225 # "Interlace handling should be turned on when using png_read_image"
226
227 SKIP: {
228 skip 'can wedge nginx with SIGPIPE', 1 unless $ENV{TEST_NGINX_UNSAFE};
229
230 $im = GD::Image->newFromPngData(http_get_body('/interlaced/png'));
231 is($im->interlaced, 1, 'png interlaced on');
232
233 }
234
235 $im = GD::Image->newFromPngData(http_get_body('/nontransparent/png'));
236 is($im->transparent, -1, 'png transparent loss');
237
238 like(http_get('/resize/jpeg'), qr/quality = 75/, 'quality default');
239 like(http_get('/quality/jpeg'), qr/quality = 50/, 'quality');
240 like(http_get('/quality_var/jpeg?q=40'), qr/quality = 40/, 'quality var');
241 like(http_get('/quality_var/quality/jpeg?q=40'), qr/quality = 60/,
242 'quality nested');
243
244 is(gif_size('/crop/gif'), '60 80', 'crop');
245 is(gif_size('/crop_var/gif?w=10&h=20'), '10 20', 'crop var');
246 is(gif_size('/crop_rotate/gif?w=5&h=6&r=90'), '5 5', 'rotate before crop');
247 is(gif_size('/resize_rotate/gif?w=5&h=6&r=90'), '6 5', 'rotate after resize');
248 is(gif_size('/resize_rotate/resize/gif??w=5&h=6&r=90'), '10 12',
249 'resize rotate nested');
250
251 like(http_get('/buffer/jpeg'), qr/415 Unsupported/, 'small buffer');
252 isnt(http_get('/proxy_buffer/jpeg'), undef, 'small buffer proxy');
253
254 ###############################################################################
255
256 sub gif_size {
257 join ' ', unpack("x6S2", http_get_body(@_));
258 }
259
260 sub http_get_body {
261 my ($uri) = @_;
262
263 return undef if !defined $uri;
264
265 my $text = http_get($uri);
266
267 if ($text !~ /(.*?)\x0d\x0a?\x0d\x0a?(.*)/ms) {
268 return undef;
269 }
270
271 return $2;
272 }
273
274 ###############################################################################
275
276 # serve static files without Content-Length
277
278 sub http_daemon {
279 my ($root) = @_;
280
281 my $server = IO::Socket::INET->new(
282 Proto => 'tcp',
283 LocalHost => '127.0.0.1',
284 LocalPort => 8081,
285 Listen => 5,
286 Reuse => 1
287 )
288 or die "Can't create listening socket: $!\n";
289
290 local $SIG{PIPE} = 'IGNORE';
291
292 while (my $client = $server->accept()) {
293 $client->autoflush(1);
294
295 my $headers = '';
296 my $uri = '';
297
298 while (<$client>) {
299 $headers .= $_;
300 last if (/^\x0d?\x0a?$/);
301 }
302
303 next if $headers eq '';
304 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i;
305
306 open my $fh, '<', $root . $uri or next;
307 local $/;
308 my $data = <$fh>;
309 close $fh;
310
311 print $client <<EOF;
312 HTTP/1.1 200 OK
313 Connection: close
314
315 $data
316 EOF
317
318 } continue {
319 close $client;
320 }
321 }
322
323 ###############################################################################