http_server_name.t 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Tests for server_name selection.
  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 rewrite/)->plan(9)
  15. ->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. add_header X-Server $server_name;
  28. return 204;
  29. }
  30. }
  31. server {
  32. listen 127.0.0.1:8080;
  33. server_name www.example.com;
  34. location / {
  35. add_header X-Server $server_name;
  36. return 204;
  37. }
  38. }
  39. server {
  40. listen 127.0.0.1:8080;
  41. server_name ~^EXAMPLE\.COM$;
  42. location / {
  43. add_header X-Server $server_name;
  44. return 204;
  45. }
  46. }
  47. server {
  48. listen 127.0.0.1:8080;
  49. server_name ~^(?P<name>.+)\Q.example.com\E$;
  50. location / {
  51. add_header X-Server $server_name;
  52. add_header X-Match $name;
  53. return 204;
  54. }
  55. }
  56. server {
  57. listen 127.0.0.1:8080;
  58. server_name "~^(?<name>www\p{N}+)\.example\.com$";
  59. location / {
  60. add_header X-Server $server_name;
  61. add_header X-Match $name;
  62. return 204;
  63. }
  64. }
  65. }
  66. EOF
  67. $t->run();
  68. ###############################################################################
  69. sub http_server($) {
  70. my ($host) = @_;
  71. return http(<<EOF);
  72. GET / HTTP/1.0
  73. Host: $host
  74. EOF
  75. }
  76. ###############################################################################
  77. like(http_server('xxx'), qr/X-Server: localhost/, 'default');
  78. like(http_server('www.example.com'), qr/\QX-Server: www.example.com/,
  79. 'www.example.com');
  80. like(http_server('WWW.EXAMPLE.COM'), qr/\QX-Server: www.example.com/,
  81. 'www.example.com uppercase');
  82. like(http_server('example.com'), qr/\QX-Server: ~^EXAMPLE\.COM$/,
  83. 'example.com regex');
  84. like(http_server('EXAMPLE.COM'), qr/\QX-Server: ~^EXAMPLE\.COM$/,
  85. 'example.com regex uppercase');
  86. like(http_server('blah.example.com'), qr/X-Match: blah/,
  87. '(P<name>.*).example.com named capture');
  88. like(http_server('BLAH.EXAMPLE.COM'), qr/X-Match: blah/,
  89. '(P<name>.*).example.com named capture uppercase');
  90. like(http_server('www01.example.com'), qr/X-Match: www01/,
  91. '\p{N} in named capture');
  92. like(http_server('WWW01.EXAMPLE.COM'), qr/X-Match: www01/,
  93. '\p{N} in named capture uppercase');
  94. ###############################################################################