in reply to Re^2: Testing with Test::Mock::Tiny::HTTP
in thread Testing with Test::Mock::HTTP::Tiny
yep, good point haukex++ as this is the easiest and most secure by far. Since I have more time:
use strict; use warnings; use HTTP::Tiny; use Test::Mock::HTTP::Tiny; use JSON; my $http = HTTP::Tiny->new; my $resp = $http->get('http://www.way-finder.uk/'); # EDIT: it's captured_data() not mocked_data() see below #my $json_str = eval { JSON::encode_json(Test::Mock::HTTP::Tiny->mocke +d_data()) }; my $json_str = eval { JSON::encode_json(Test::Mock::HTTP::Tiny->captur +ed_data()) }; die "failed to encode json" if $@; open my $fh, '>', 'mock_html.dat'; print $fh $json_str; close $fh;
and
use strict; use warnings; use Test::More; use Test::Mock::HTTP::Tiny; use WWW::Crawl; use JSON; plan tests => 1; $/ = undef; open my $fh, '<', 't/mock_html.dat' or die "Can't open datafile"; my $replay = <$fh>; close $fh; $replay = eval { JSON::decode_json($replay) }; ok(!$@, "parsed JSON replay data") or BAIL_OUT($@); is(ref($replay), 'ARRAY', "parsed JSON replay data is an ARRAY"); die "Nothing to replay" unless $replay; Test::Mock::HTTP::Tiny->set_mocked_data($replay); my $crawl = WWW::Crawl->new( 'timestamp' => 'a', ); my @links = $crawl->crawl('https://www.testing.crawl', \&link); cmp_ok ( scalar @links, '==', 8, 'Correct link count'); sub link { diag ($_[0]); }
note: eval() around JSON subs is because it dies on error last time I checked.
bw, bliako
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Testing with Test::Mock::Tiny::HTTP
by Bod (Parson) on Sep 27, 2023 at 14:39 UTC | |
by bliako (Abbot) on Sep 28, 2023 at 09:22 UTC | |
by Bod (Parson) on Sep 28, 2023 at 11:12 UTC | |
|
Re^4: Testing with Test::Mock::Tiny::HTTP
by Bod (Parson) on Sep 27, 2023 at 20:33 UTC | |
by bliako (Abbot) on Sep 28, 2023 at 09:47 UTC | |
by Bod (Parson) on Sep 29, 2023 at 21:38 UTC | |
by Bod (Parson) on Sep 28, 2023 at 11:08 UTC | |
by bliako (Abbot) on Sep 29, 2023 at 06:43 UTC | |
by Bod (Parson) on Sep 29, 2023 at 20:05 UTC | |
| |
by Bod (Parson) on Sep 29, 2023 at 21:32 UTC |