range.t 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Tests for range 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;
  11. ###############################################################################
  12. select STDERR; $| = 1;
  13. select STDOUT; $| = 1;
  14. my $t = Test::Nginx->new()->has(qw/http charset/)->plan(25);
  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. charset_map B A {
  24. 58 59; # X -> Y
  25. }
  26. server {
  27. listen 127.0.0.1:8080;
  28. server_name localhost;
  29. location /t2.html {
  30. charset A;
  31. source_charset B;
  32. }
  33. }
  34. }
  35. EOF
  36. $t->write_file('t1.html',
  37. join('', map { sprintf "X%03dXXXXXX", $_ } (0 .. 99)));
  38. $t->write_file('t2.html',
  39. join('', map { sprintf "X%03dXXXXXX", $_ } (0 .. 99)));
  40. $t->run();
  41. ###############################################################################
  42. my $t1;
  43. $t1 = http_get_range('/t1.html', 'Range: bytes=0-8');
  44. like($t1, qr/206/, 'range request - 206 partial reply');
  45. like($t1, qr/Content-Length: 9/, 'range request - correct length');
  46. like($t1, qr/Content-Range: bytes 0-8\/1000/, 'range request - content range');
  47. like($t1, qr/^X000XXXXX$/m, 'range request - correct content');
  48. $t1 = http_get_range('/t1.html', 'Range: bytes=-10');
  49. like($t1, qr/206/, 'final bytes - 206 partial reply');
  50. like($t1, qr/Content-Length: 10/, 'final bytes - content length');
  51. like($t1, qr/Content-Range: bytes 990-999\/1000/,
  52. 'final bytes - content range');
  53. like($t1, qr/^X099XXXXXX$/m, 'final bytes - correct content');
  54. $t1 = http_get_range('/t1.html', 'Range: bytes=990-');
  55. like($t1, qr/206/, 'final bytes explicit - 206 partial reply');
  56. like($t1, qr/Content-Length: 10/, 'final bytes explicit - content length');
  57. like($t1, qr/Content-Range: bytes 990-999\/1000/,
  58. 'final bytes explicit - content range');
  59. like($t1, qr/^X099XXXXXX$/m, 'final bytes explicit - correct content');
  60. $t1 = http_get_range('/t1.html', 'Range: bytes=990-1990');
  61. like($t1, qr/206/, 'more than length - 206 partial reply');
  62. like($t1, qr/Content-Length: 10/, 'more than length - content length');
  63. like($t1, qr/Content-Range: bytes 990-999\/1000/,
  64. 'more than length - content range');
  65. like($t1, qr/^X099XXXXXX$/m, 'more than length - correct content');
  66. $t1 = http_get_range('/t2.html', 'Range: bytes=990-1990');
  67. like($t1, qr/206/, 'recoded - 206 partial reply');
  68. like($t1, qr/Content-Length: 10/, 'recoded - content length');
  69. like($t1, qr/Content-Range: bytes 990-999\/1000/, 'recoded - content range');
  70. like($t1, qr/^Y099YYYYYY$/m, 'recoded - correct content');
  71. $t1 = http_get_range('/t1.html', 'Range: bytes=0-9, -10, 10-19');
  72. like($t1, qr/206/, 'multipart - 206 partial reply');
  73. like($t1, qr/Content-Type: multipart\/byteranges; boundary=/,
  74. 'multipart - content type');
  75. like($t1, qr/X000XXXXXX/m, 'multipart - content 0-9');
  76. like($t1, qr/^X099XXXXXX\x0d?$/m, 'multipart - content -10 aka 990-999');
  77. like($t1, qr/X001XXXXXX\x0d?$/m, 'multipart - content 10-19');
  78. ###############################################################################
  79. sub http_get_range {
  80. my ($url, $extra) = @_;
  81. return http(<<EOF);
  82. GET $url HTTP/1.1
  83. Host: localhost
  84. Connection: close
  85. $extra
  86. EOF
  87. }
  88. ###############################################################################