scgi.t 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Test for scgi 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 SCGI; };
  15. plan(skip_all => 'SCGI not installed') if $@;
  16. my $t = Test::Nginx->new()->has(qw/http scgi/)->plan(4)
  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. scgi_pass 127.0.0.1:8081;
  30. scgi_param SCGI 1;
  31. scgi_param REQUEST_URI $request_uri;
  32. }
  33. }
  34. }
  35. EOF
  36. $t->run_daemon(\&scgi_daemon);
  37. $t->run();
  38. ###############################################################################
  39. like(http_get('/'), qr/SEE-THIS/, 'scgi request');
  40. like(http_get('/redir'), qr/302/, 'scgi redirect');
  41. like(http_get('/'), qr/^3$/m, 'scgi third request');
  42. unlike(http_head('/'), qr/SEE-THIS/, 'no data in HEAD');
  43. ###############################################################################
  44. sub scgi_daemon {
  45. my $server = IO::Socket::INET->new(
  46. Proto => 'tcp',
  47. LocalHost => '127.0.0.1:8081',
  48. Listen => 5,
  49. Reuse => 1
  50. )
  51. or die "Can't create listening socket: $!\n";
  52. my $scgi = SCGI->new($server, blocking => 1);
  53. my $count = 0;
  54. while (my $request = $scgi->accept()) {
  55. $count++;
  56. $request->read_env();
  57. $request->connection()->print(<<EOF);
  58. Location: http://127.0.0.1:8080/redirect
  59. Content-Type: text/html
  60. SEE-THIS
  61. $count
  62. EOF
  63. }
  64. }
  65. ###############################################################################