File: 06-network-fallback.t

package info (click to toggle)
libtest-lwp-useragent-perl 0.036-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 428 kB
  • sloc: perl: 530; makefile: 10
file content (114 lines) | stat: -rw-r--r-- 4,820 bytes parent folder | download
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
110
111
112
113
114
use strict;
use warnings;

use Test::More 0.88;

BEGIN {
    if (not $ENV{RELEASE_TESTING}
        and ($ENV{NO_NETWORK_TESTING}
             or (not $ENV{AUTHOR_TESTING} and not $ENV{AUTOMATED_TESTING} and not $ENV{EXTENDED_TESTING})))
    {
        plan skip_all => 'these tests use the network: unset NO_NETWORK_TESTING and set EXTENDED_TESTING, AUTHOR_TESTING or AUTOMATED_TESTING to run';
    }
}

# if tests are getting to this point and then skip due to not being able to
# reach this site, we know they are not setting NO_NETWORK_TESTING as they should.
use if !$ENV{RELEASE_TESTING}, 'Test::RequiresInternet', 'httpbin.org' => 80;

use if $ENV{AUTHOR_TESTING}, 'Test::Warnings';
use Test::LWP::UserAgent;
use HTTP::Request::Common;
use URI;

# I mostly POST to this URL, rather than using GET, so as to not process the
# "302 Redirect" response.
my $redirect_url = 'http://nghttp2.org/httpbin/redirect-to?url=http%3A%2F%2Fhttpbin.org%2Fget';

# allow LWP::UserAgent to carp about unknown constructor arguments
$^W = 1;

{
    my $useragent = Test::LWP::UserAgent->new;
    my $useragent2 = Test::LWP::UserAgent->new;

    ok(!Test::LWP::UserAgent->network_fallback, 'network_fallback not set globally');
    ok(!$useragent->network_fallback, 'network_fallback not enabled for the instance');
    ok(!$useragent2->network_fallback, 'network_fallback not enabled for the other instance');

    test_send_request('no mappings', $useragent, POST($redirect_url), '404');


    $useragent->network_fallback(1);
    ok($useragent->network_fallback, 'network_fallback enabled for the instance');

    test_send_request('network_fallback on instance', $useragent, POST($redirect_url), '302');
    test_send_request('no network_fallback on other instance', $useragent2, POST($redirect_url), '404');

    $useragent->network_fallback(0);
    ok(!$useragent->network_fallback, 'network_fallback disabled for the instance');
    test_send_request('no network_fallback on instance', $useragent, POST($redirect_url), '404');
    test_send_request('no network_fallback on other instance', $useragent2, POST($redirect_url), '404');
}

{
    my $useragent = Test::LWP::UserAgent->new;
    my $useragent2 = Test::LWP::UserAgent->new;

    $useragent->network_fallback(1);
    ok($useragent->network_fallback, 'network_fallback enabled for the instance');

    Test::LWP::UserAgent->network_fallback(1);
    ok(Test::LWP::UserAgent->network_fallback, 'network_fallback set globally');
    ok($useragent->network_fallback, 'network_fallback enabled for the instance');
    ok($useragent->network_fallback, 'network_fallback enabled for the other instance');

    test_send_request('network_fallback on other instance', $useragent2, POST($redirect_url), '302');
    test_send_request('network_fallback, with redirect', $useragent2, GET($redirect_url), '200');

    Test::LWP::UserAgent->network_fallback(0);
    ok($useragent->network_fallback, 'network_fallback still enabled for the instance');
    ok(!$useragent2->network_fallback, 'network_fallback not enabled for the other instance');

    test_send_request('network_fallback instance flag still remains', $useragent, POST($redirect_url), '302');
    test_send_request('global network_fallback clearable', $useragent2, POST($redirect_url), '404');
}

{
    my $useragent = Test::LWP::UserAgent->new;
    my $useragent2 = Test::LWP::UserAgent->new;

    my $host = URI->new($redirect_url)->host;
    $useragent->map_network_response($host);
    ok(!$useragent->network_fallback, 'network_fallback not enabled for the instance');
    test_send_request('network response mapped on instance', $useragent, POST($redirect_url), '302');
    test_send_request('network response not mapped on other instance', $useragent2, POST($redirect_url), '404');

    Test::LWP::UserAgent->map_network_response($host);
    test_send_request('network response mapped globally', $useragent2, POST($redirect_url), '302');
    Test::LWP::UserAgent->unmap_all;
}

{
    my $useragent = Test::LWP::UserAgent->new(network_fallback => 1);
    my $useragent2 = Test::LWP::UserAgent->new;

    ok(!Test::LWP::UserAgent->network_fallback, 'network_fallback not set globally');
    ok($useragent->network_fallback, 'network_fallback enabled for the instance');
    ok(!$useragent2->network_fallback, 'network_fallback not enabled for the other instance');

    test_send_request('network_fallback on instance', $useragent, POST($redirect_url), '302');
    test_send_request('network_fallback on other instance', $useragent2, POST($redirect_url), '404');
}

sub test_send_request
{
    my ($name, $useragent, $request, $expected_code) = @_;

    note "\n$name";

    local $Test::Builder::Level = $Test::Builder::Level + 1;
    is($useragent->request($request)->code, $expected_code, $name);
}

done_testing;