range_flv.t 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 flv/)->plan(12);
  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. flv;
  28. }
  29. }
  30. }
  31. EOF
  32. $t->write_file('t1.flv',
  33. join('', map { sprintf "X%03dXXXXXX", $_ } (0 .. 99)));
  34. $t->run();
  35. ###############################################################################
  36. my $t1;
  37. # FLV has 13 byte header at start.
  38. $t1 = http_get_range('/t1.flv?start=100', 'Range: bytes=0-9');
  39. like($t1, qr/206/, 'first bytes - 206 partial reply');
  40. like($t1, qr/Content-Length: 10/, 'first bytes - correct length');
  41. like($t1, qr/Content-Range: bytes 0-9\/913/, 'first bytes - content range');
  42. like($t1, qr/^FLV.{7}$/m, 'first bytes - correct content');
  43. $t1 = http_get_range('/t1.flv?start=100', 'Range: bytes=-10');
  44. like($t1, qr/206/, 'final bytes - 206 partial reply');
  45. like($t1, qr/Content-Length: 10/, 'final bytes - content length');
  46. like($t1, qr/Content-Range: bytes 903-912\/913/,
  47. 'final bytes - content range');
  48. like($t1, qr/^X099XXXXXX$/m, 'final bytes - correct content');
  49. $t1 = http_get_range('/t1.flv?start=100', 'Range: bytes=0-99');
  50. like($t1, qr/206/, 'multi buffers - 206 partial reply');
  51. like($t1, qr/Content-Length: 100/, 'multi buffers - content length');
  52. like($t1, qr/Content-Range: bytes 0-99\/913/, 'multi buffers - content range');
  53. like($t1, qr/^FLV.{10}X010XXXXXX(X01[1-7]XXXXXX){7}X018XXX$/m,
  54. 'multi buffers - correct content');
  55. ###############################################################################
  56. sub http_get_range {
  57. my ($url, $extra) = @_;
  58. return http(<<EOF);
  59. GET $url HTTP/1.1
  60. Host: localhost
  61. Connection: close
  62. $extra
  63. EOF
  64. }
  65. ###############################################################################