gzip.t 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Tests for nginx gzip filter module.
  4. ###############################################################################
  5. use warnings;
  6. use strict;
  7. use Test::More;
  8. BEGIN { use FindBin; chdir($FindBin::Bin); }
  9. use lib 'lib';
  10. use Test::Nginx qw/ :DEFAULT :gzip /;
  11. ###############################################################################
  12. select STDERR; $| = 1;
  13. select STDOUT; $| = 1;
  14. my $t = Test::Nginx->new()->has(qw/http proxy gzip/)->plan(8);
  15. $t->write_file_expand('nginx.conf', <<'EOF');
  16. %%TEST_GLOBALS%%
  17. master_process off;
  18. daemon off;
  19. events {
  20. }
  21. http {
  22. %%TEST_GLOBALS_HTTP%%
  23. server {
  24. listen 127.0.0.1:8080;
  25. server_name localhost;
  26. location / {
  27. gzip on;
  28. }
  29. location /proxy/ {
  30. gzip on;
  31. proxy_pass http://127.0.0.1:8080/local/;
  32. }
  33. location /local/ {
  34. gzip off;
  35. alias %%TESTDIR%%/;
  36. }
  37. }
  38. }
  39. EOF
  40. $t->write_file('index.html', 'X' x 64);
  41. $t->run();
  42. ###############################################################################
  43. my $r;
  44. $r = http_gzip_request('/');
  45. like($r, qr/^Content-Encoding: gzip/m, 'gzip');
  46. http_gzip_like($r, qr/^X{64}\Z/, 'gzip content correct');
  47. $r = http_gzip_request('/proxy/');
  48. like($r, qr/^Content-Encoding: gzip/m, 'gzip proxied');
  49. http_gzip_like($r, qr/^X{64}\Z/, 'gzip proxied content');
  50. # Accept-Ranges headers should be cleared
  51. unlike(http_gzip_request('/'), qr/Accept-Ranges/im, 'cleared accept-ranges');
  52. unlike(http_gzip_request('/proxy/'), qr/Accept-Ranges/im,
  53. 'cleared headers from proxy');
  54. # HEAD requests should return correct headers
  55. like(http_gzip_head('/'), qr/Content-Encoding: gzip/, 'gzip head');
  56. unlike(http_head('/'), qr/Content-Encoding: gzip/, 'no gzip head');
  57. ###############################################################################
  58. sub http_gzip_head {
  59. my ($uri) = @_;
  60. return http(<<EOF);
  61. HEAD $uri HTTP/1.1
  62. Host: localhost
  63. Connection: close
  64. Accept-Encoding: gzip
  65. EOF
  66. }
  67. ###############################################################################