summaryrefslogtreecommitdiff
blob: 4cf8f567547aed6fca35281ae307fdd21899001f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
From bea092aae37300053b971a12b6e37b6104df41f9 Mon Sep 17 00:00:00 2001
From: Nikolay Mishin <mi@ya.ru>
Date: Thu, 22 Oct 2015 02:59:04 +0400
Subject: Rework 01-find.t

Rework test to not rely on external web resources by mocking the setup
with a HTTP::Server::Simple derived webserver

Bug: https://github.com/btrott/Feed-Find/pull/2
Signed-off-by: Gary Greene <greeneg@tolharadys.net>
Signed-off-by: Kent Fredric <kentnl@gentoo.org>
---
 t/01-find.t                | 57 ++++++++++++++++++++++++++++++++------
 t/htdocs/anchors-only.html |  9 ++++++
 2 files changed, 58 insertions(+), 8 deletions(-)
 create mode 100644 t/htdocs/anchors-only.html

diff --git a/t/01-find.t b/t/01-find.t
index 58549b5..22d0048 100644
--- a/t/01-find.t
+++ b/t/01-find.t
@@ -1,20 +1,61 @@
+use CGI::Application::Server;
+use Test::HTTP::Server::Simple;
+
+package My::WebServer {
+    use base qw/Test::HTTP::Server::Simple CGI::Application::Server/;
+}
+
+package main;
+
 use strict;
-use Test::More tests => 4;
+use Test::More tests => 5;
 use Feed::Find;
 use LWP::UserAgent;
 
-use constant BASE => 'http://stupidfool.org/perl/feeds/';
+my $port = $ENV{CGI_APP_SERVER_TEST_PORT} || 40000 + int(rand(10000));
+
+my $s = My::WebServer->new($port);
+$s->document_root('./t/htdocs');
+
+my $url_root = $s->started_ok("start up my web server");
+
+# generate our anchors-only.html file to get the URL correct in the links
+my $anchor_html = <<"END";
+<html>
+  <head>
+    <link rel="alternate" title="my feed"
+      href="$url_root/ok.xml" type="application/xml" />
+  <head>
+  <body>
+    <a href="$url_root/ok.xml" type="application/xml">my feed</a>
+  </body>
+</html>
+END
 
-my(@feeds);
+open(my $fh, ">./t/htdocs/anchors-only.html") or
+  die "Cannot open file\n";
+print $fh <<"END";
+<html>
+  <head>
+    <link rel="alternate" title="my feed"
+      href="$url_root/ok.xml" type="application/xml" />
+  <head>
+  <body>
+    <a href="$url_root/ok.xml" type="application/xml">my feed</a>
+  </body>
+</html>
+END
+close $fh;
 
-@feeds = Feed::Find->find(BASE . 'anchors-only.html');
+my @feeds = ();
+@feeds = Feed::Find->find("$url_root/anchors-only.html");
 is(scalar @feeds, 1);
-is($feeds[0], BASE . 'ok.xml');
+is($feeds[0], "$url_root/ok.xml");
 
 my $ua = LWP::UserAgent->new;
 $ua->env_proxy;
-my $req = HTTP::Request->new(GET => BASE . 'anchors-only.html');
+my $req = HTTP::Request->new(GET => "$url_root/anchors-only.html");
 my $res = $ua->request($req);
-@feeds = Feed::Find->find_in_html(\$res->content, BASE . 'anchors-only.html');
+@feeds = Feed::Find->find_in_html(\$res->content, "$url_root/anchors-only.html");
 is(scalar @feeds, 1);
-is($feeds[0], BASE . 'ok.xml');
+is($feeds[0], "$url_root/ok.xml");
diff --git a/t/htdocs/anchors-only.html b/t/htdocs/anchors-only.html
new file mode 100644
index 0000000..79f1df8
--- /dev/null
+++ b/t/htdocs/anchors-only.html
@@ -0,0 +1,9 @@
+<html>
+  <head>
+    <link rel="alternate" title="my feed"
+      href="http://localhost:45578/ok.xml" type="application/xml" />
+  <head>
+  <body>
+    <a href="http://localhost:45578/ok.xml" type="application/xml">my feed</a>
+  </body>
+</html>
-- 
2.28.0