Actions Status

NAME

Test::WWW::Stub - Block and stub specified URL for LWP

SYNOPSIS

# External http(s) access via LWP is blocked by just declaring 'use Test::WWW::Stub';
# Note that 'require Test::WWW::Stub' or 'use Test::WWW::Stub ()' doesn't block external access.
use Test::WWW::Stub;

my $ua = LWP::UserAgent->new;

my $stubbed_res = [ 200, [], ['okay'] ];

{
    my $guard = Test::WWW::Stub->register(q<http://example.com/TEST>, $stubbed_res);

    is $ua->get('http://example.com/TEST')->content, 'okay';
}
isnt $ua->get('http://example.com/TEST')->content, 'okay';

{
    # registering in void context doesn't create guard.
    Test::WWW::Stub->register(q<http://example.com/HOGE/>, $stubbed_res);

    is $ua->get('http://example.com/HOGE')->content, 'okay';
}
is $ua->get('http://example.com/HOGE')->content, 'okay';

{
    # You can also use regexp for uri
    my $guard = Test::WWW::Stub->register(qr<\A\Qhttp://example.com/MATCH/\E>, $stubbed_res);

    is $ua->get('http://example.com/MATCH/hogehoge')->content, 'okay';
}

{
    # you can unstub and allow external access temporary
    my $unstub_guard = Test::WWW::Stub->unstub;

    # External access occurs!!
    ok $ua->get('http://example.com');
}

my $last_req = Test::WWW::Stub->last_request; # Plack::Request
is $last_req->uri, 'http://example.com/MATCH/hogehoge';

Test::WWW::Stub->requested_ok('GET', 'http://example.com/TEST'); # passes

DESCRIPTION

Test::WWW::Stub is a helper module to block external http(s) request and stub some specific requests in your test.

Because this modules uses LWP::Protocol::PSGI internally, you don't have to modify target codes using LWP::UserAgent.

METHODS

LICENSE

Copyright (C) Hatena Co., Ltd.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Asato Wakisaka asato.wakisaka@gmail.com

Original implementation written by suzak.