proxy.t 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Tests for http proxy 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 proxy/)->plan(3);
  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. proxy_pass http://127.0.0.1:8081;
  28. proxy_read_timeout 1s;
  29. }
  30. }
  31. }
  32. EOF
  33. $t->run_daemon(\&http_daemon);
  34. $t->run();
  35. ###############################################################################
  36. like(http_get('/'), qr/SEE-THIS/, 'proxy request');
  37. like(http_get('/multi'), qr/AND-THIS/, 'proxy request with multiple packets');
  38. unlike(http_head('/'), qr/SEE-THIS/, 'proxy head request');
  39. ###############################################################################
  40. sub http_daemon {
  41. my $server = IO::Socket::INET->new(
  42. Proto => 'tcp',
  43. LocalHost => '127.0.0.1:8081',
  44. Listen => 5,
  45. Reuse => 1
  46. )
  47. or die "Can't create listening socket: $!\n";
  48. while (my $client = $server->accept()) {
  49. $client->autoflush(1);
  50. my $headers = '';
  51. my $uri = '';
  52. while (<$client>) {
  53. $headers .= $_;
  54. last if (/^\x0d?\x0a?$/);
  55. }
  56. $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i;
  57. if ($uri eq '/') {
  58. print $client <<'EOF';
  59. HTTP/1.1 200 OK
  60. Connection: close
  61. EOF
  62. print $client "TEST-OK-IF-YOU-SEE-THIS"
  63. unless $headers =~ /^HEAD/i;
  64. } elsif ($uri eq '/multi') {
  65. print $client <<"EOF";
  66. HTTP/1.1 200 OK
  67. Connection: close
  68. TEST-OK-IF-YOU-SEE-THIS
  69. EOF
  70. select undef, undef, undef, 0.1;
  71. print $client 'AND-THIS';
  72. } else {
  73. print $client <<"EOF";
  74. HTTP/1.1 404 Not Found
  75. Connection: close
  76. Oops, '$uri' not found
  77. EOF
  78. }
  79. close $client;
  80. }
  81. }
  82. ###############################################################################