fastcgi.t 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Test for fastcgi backend.
  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. eval { require FCGI; };
  15. plan(skip_all => 'FCGI not installed') if $@;
  16. my $t = Test::Nginx->new()->has(qw/http fastcgi/)->plan(5)
  17. ->write_file_expand('nginx.conf', <<'EOF');
  18. %%TEST_GLOBALS%%
  19. master_process off;
  20. daemon off;
  21. events {
  22. }
  23. http {
  24. %%TEST_GLOBALS_HTTP%%
  25. server {
  26. listen 127.0.0.1:8080;
  27. server_name localhost;
  28. location / {
  29. fastcgi_pass 127.0.0.1:8081;
  30. fastcgi_param REQUEST_URI $request_uri;
  31. }
  32. }
  33. }
  34. EOF
  35. $t->run_daemon(\&fastcgi_daemon);
  36. $t->run();
  37. ###############################################################################
  38. like(http_get('/'), qr/SEE-THIS/, 'fastcgi request');
  39. like(http_get('/redir'), qr/302/, 'fastcgi redirect');
  40. like(http_get('/'), qr/^3$/m, 'fastcgi third request');
  41. unlike(http_head('/'), qr/SEE-THIS/, 'no data in HEAD');
  42. like(http_get('/stderr'), qr/SEE-THIS/, 'large stderr handled');
  43. ###############################################################################
  44. sub fastcgi_daemon {
  45. my $socket = FCGI::OpenSocket('127.0.0.1:8081', 5);
  46. my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV,
  47. $socket);
  48. my $count;
  49. while( $request->Accept() >= 0 ) {
  50. $count++;
  51. if ($ENV{REQUEST_URI} eq '/stderr') {
  52. warn "sample stderr text" x 512;
  53. }
  54. print <<EOF;
  55. Location: http://127.0.0.1:8080/redirect
  56. Content-Type: text/html
  57. SEE-THIS
  58. $count
  59. EOF
  60. }
  61. FCGI::CloseSocket($socket);
  62. }
  63. ###############################################################################