webmention.t 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright (C) 2019 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 => 21;
  18. use LWP::UserAgent;
  19. use XML::LibXML;
  20. add_module('webmention.pl');
  21. AppendStringToFile($ConfigFile, <<'EOT');
  22. $CommentsPrefix = 'Comments_on_';
  23. $FooterNote = '<p>Author: <a rel="author" href="https://alexschroeder.ch/">Alex Schroeder</a></p>';
  24. EOT
  25. $CommentsPrefix = 'Comments_on_';
  26. like(get_page(''), qr/webmention/,
  27. "Webmention link for default URL");
  28. like(get_page('HomePage'), qr/webmention/,
  29. "Webmention link for homepage");
  30. unlike(get_page('Comments_on_HomePage'), qr/webmention/,
  31. "No webmention link for comment pages");
  32. unlike(get_page('action=history id=HomePage'), qr/webmention/,
  33. "No webmention link for history action");
  34. unlike(get_page('action=browse id=HomePage revision=1'), qr/webmention/,
  35. "No webmention link for browse action with revision");
  36. $UsePathInfo = 1;
  37. # This test is is going to use two servers in addition to this script, but in
  38. # actual fact we are all going to share the data directory.
  39. test_page(update_page('Target', 'This is the test page.'), 'This is the test page');
  40. # Server 1 is going to be the webmention server.
  41. start_server();
  42. # Check whether the child is up and running
  43. my $ua = LWP::UserAgent->new;
  44. my $response = $ua->get("$ScriptName?action=version");
  45. ok($response->is_success, "There is a wiki running at $ScriptName");
  46. like($response->decoded_content, qr/\bwebmention\.pl/, "The server has the webmention extension installed");
  47. # Now that we have the webmention server running, we need to get the URL of the
  48. # test page, including its port.
  49. my $target_url = ScriptUrl('Target');
  50. # Verify that the target exists via external request
  51. $response = $ua->get($target_url);
  52. ok($response->is_success, "Target URL response");
  53. like($response->decoded_content, qr/This is the test page/, "Target URL decoded");
  54. # Create the Source page before starting the next server (so that it knows about
  55. # the new page)
  56. test_page(update_page('Source', "Link to $target_url"), 'Link to');
  57. # Server 2 is going to be the source server.
  58. start_server(2);
  59. # Check whether the child is up and running (with a new $ScriptName!)
  60. $response = $ua->get("$ScriptName?action=version");
  61. ok($response->is_success, "There is a wiki running at $ScriptName");
  62. # New script name means we can now get the source_url.
  63. my $source_url = ScriptUrl('Source');
  64. # Verify that the source exists via external request
  65. $response = $ua->get($source_url);
  66. ok($response->is_success, "Source URL response");
  67. like($response->decoded_content, qr/Link to/, "Source URL decoded");
  68. like($response->decoded_content, qr/$target_url/, "Source page links to Target page");
  69. # Find the Webmention URL
  70. $response = $ua->get($target_url);
  71. ok($response->is_success, "Target URL response");
  72. like($response->decoded_content, qr/rel="webmention"/, "Target page has webmention link");
  73. # Parse target page
  74. my $parser = XML::LibXML->new(recover => 2);
  75. my $dom = $parser->load_html(string => $response->decoded_content);
  76. my $webmention = $dom->findvalue('//link[@rel="webmention"]/@href');
  77. $response = $ua->post($webmention, { source => $source_url, target => $target_url });
  78. ok($response->is_success, 'Got webmention response: ' . $response->message);
  79. $page = get_page('Comments_on_Target');
  80. test_page($page, 'Webmention:', $source_url);
  81. xpath_test($page, '//a[@class="url https outside"][@href="https://alexschroeder.ch/"][text()="Alex Schroeder"]');