cookie.t 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (C) 2009–2015 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. require 't/test.pl';
  16. package OddMuse;
  17. use Test::More tests => 18;
  18. use utf8;
  19. # Basic usage that broke when I last changed the cookie handling code.
  20. test_page(get_page('username=Alex'), 'Status: 404');
  21. test_page(get_page('action=browse id=Alex'), 'Alex');
  22. # Username
  23. test_page(get_page('action=browse id=HomePage username=Alex'), "\nSet-Cookie: Wiki=username%251eAlex");
  24. test_page(get_page('action=browse id=HomePage username=01234567890123456789012345678901234567890123456789'),
  25. "\nSet-Cookie: Wiki=username%251e01234567890123456789012345678901234567890123456789");
  26. test_page(get_page('action=browse id=HomePage username=01234567890123456789012345678901234567890123456789X'),
  27. 'UserName must be 50 characters or less: not saved');
  28. test_page(get_page('action=browse id=HomePage username=AlexSchroeder'),
  29. "\nSet-Cookie: Wiki=username%251eAlexSchroeder");
  30. test_page(get_page('action=browse id=HomePage username=Alex%20Schroeder'),
  31. "\nSet-Cookie: Wiki=username%251eAlex%20Schroeder");
  32. AppendStringToFile($ConfigFile, "\$FreeLinks = 0;\n");
  33. AppendStringToFile($ConfigFile, "\$WikiLinks = 1;\n");
  34. test_page(get_page('action=browse id=HomePage username=Alex%20Schroeder'),
  35. 'Invalid UserName Alex Schroeder: not saved');
  36. test_page(get_page('action=browse id=HomePage username=AlexSchroeder'),
  37. "\nSet-Cookie: Wiki=username%251eAlexSchroeder");
  38. test_page(get_page('action=browse id=HomePage username=Alex'),
  39. 'Invalid UserName Alex: not saved');
  40. # single words are ok if we switch off $WikiLinks as well!
  41. AppendStringToFile($ConfigFile, "\$WikiLinks = 0;\n");
  42. test_page(get_page('action=browse id=HomePage username=Alex'),
  43. "\nSet-Cookie: Wiki=username%251eAlex");
  44. SKIP: {
  45. eval { require LWP::UserAgent; };
  46. skip "LWP::UserAgent not installed", 7 if $@;
  47. eval { require HTTP::Cookies; };
  48. skip "HTTP::Cookies not installed", 7 if $@;
  49. my $wiki = 'http://127.0.0.1/cgi-bin/wiki.pl';
  50. my $ua = LWP::UserAgent->new;
  51. my $response = $ua->get("$wiki?action=version");
  52. skip("No wiki running at $wiki", 7)
  53. unless $response->is_success;
  54. $ua = LWP::UserAgent->new;
  55. my $cookie = HTTP::Cookies->new;
  56. $ua ->cookie_jar($cookie);
  57. # Set the cookie
  58. $response = $ua->get("$wiki?action=debug;pwd=foo");
  59. ok($response->is_success, 'request the page');
  60. $ua->cookie_jar->as_string =~ /Set-Cookie.*: ([^=]+)=pwd%251efoo/;
  61. my $cookie = $1;
  62. ok($cookie, 'pwd was set in the cookie');
  63. test_page_negative($response->content, 'pwd');
  64. # Change the cookie
  65. $response = $ua->get("$wiki?action=debug;pwd=test");
  66. test_page($ua->cookie_jar->as_string, qq{Set-Cookie.*: $cookie=pwd%251etest});
  67. # Delete the cookie
  68. $response = $ua->get("$wiki?action=debug;pwd=");
  69. test_page($ua->cookie_jar->as_string, qq{Set-Cookie.*: $cookie=""});
  70. # Encoding issues
  71. $response = $ua->get("$wiki?action=rc;username=Alex\%20Schr\%C3\%B6der");
  72. test_page($response->header('Set-Cookie'),
  73. qq{^Wiki=username%251eAlex%20Schr%C3%B6der});
  74. test_page($ua->cookie_jar->as_string,
  75. qq{Set-Cookie.*: $cookie=username%251eAlex%20Schr%C3%B6der});
  76. };