http_location.t 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Tests for location 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(8)
  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-Location exactlyroot;
  28. return 204;
  29. }
  30. location / {
  31. add_header X-Location root;
  32. return 204;
  33. }
  34. location ^~ /images/ {
  35. add_header X-Location images;
  36. return 204;
  37. }
  38. location ~* \.(gif|jpg|jpeg)$ {
  39. add_header X-Location regex;
  40. return 204;
  41. }
  42. location ~ casefull {
  43. add_header X-Location casefull;
  44. return 204;
  45. }
  46. }
  47. }
  48. EOF
  49. $t->run();
  50. ###############################################################################
  51. like(http_get('/'), qr/X-Location: exactlyroot/, 'exactlyroot');
  52. like(http_get('/x'), qr/X-Location: root/, 'root');
  53. like(http_get('/images/t.gif'), qr/X-Location: images/, 'images');
  54. like(http_get('/t.gif'), qr/X-Location: regex/, 'regex');
  55. like(http_get('/t.GIF'), qr/X-Location: regex/, 'regex with mungled case');
  56. like(http_get('/casefull/t.gif'), qr/X-Location: regex/, 'first regex wins');
  57. like(http_get('/casefull/'), qr/X-Location: casefull/, 'casefull regex');
  58. like(http_get('/CASEFULL/'), qr/X-Location: root/,
  59. 'casefull regex do not match wrong case');
  60. ###############################################################################