The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test::LWP::MockSocket::http - Inject arbitrary data as socket data for LWP::UserAgent

SYNOPSIS

    use Test::LWP::MockSocket::http;
    use LWP::UserAgent;
    #   $LWP_Response is exported by this module
    $LWP_Response = "HTTP/1.0 200 OK\r\n\r\nSome Response Text";
    my $ua = LWP::UserAgent->new();
    $ua->proxy("http", "http://1.2.3.4:56");
    my $http_response = $ua->get("http://www.foo.com/bar.html");
    
    $http_response->code;       #200
    $http_response->content;    # "Some response text"
    $LWP_SocketArgs->{PeerAddr} # '1.2.3.4'

DESCRIPTION

This module, when loaded, mangles some functions in LWP::Protocol::http which will emulate a real socket. LWP is used as normally as much as possible.

Effort has been made to maintain the exact behavior of Net::HTTP and LWP::Protocol::http.

Two variables are exported, $LWP_Response which should contain raw HTTP 'data', and $LWP_SocketArgs which contains a hashref passed to the socket's new constructor. This is helpful for debugging complex LWP::UserAgent subclasses (or wrappers) which modify possible connection settings.

CAVEATS/BUGS

Probably many. This relies on mainly undocumented behavior and features of LWP and is likely to break. I wrote this for testing code which used LWP and its subclasses heavily, but still desired the full functionality of LWP::UserAgent (if you look closely enough, you will see that the same HTTP::Request object which is passed to LWP is not the actual one sent on the wire, and the HTTP::Response object returned by LWP methods is not the same one received on the wire).

ACKNOWLEDGEMENTS

Thanks to mst for helping me with the difficult task of selecting the module name =head1 AUTHOR AND COPYRIGHT

Copyright 2011 M. Nunberg

You may use and distribute this software under the terms of the GNU General Public License Version 2 or higher.