NAME

Mock::LWP::UserAgent - Mock version of Mock::LWP::UserAgent

SYNOPSIS

use Mock::LWP::UserAgent;
Mock::LWP::UserAgent->install_mock();

my $ua = LWP::UserAgent->new();
my $resp = $ua->get( $url );	# From the mock object's database.

or, to have the support routines but have a working LWP::UserAgent,

use Mock::LWP::UserAgent;

DESCRIPTION

THIS CLASS IS PRIVATE TO THE Astro-SpaceTrack DISTRIBUTION. It may be modified or revoked at any time, without notice. Documentation is for the benefit of the author.

If install_mock() is called, this class mocks the salient features of the LWP::UserAgent interface. But all responses are canned.

By default the responses are stored in t/data/Mock-LWP-UserAgent/resp.json, but this can be changed by localizing $Mock::LWP::UserAgent::CANNED_RESPONSE_FILE and storing the path to the desired file in it before instantiating LWP::UserAgent.

The canned response file is a JSON representaton of a hash keyed by URL and, within URL, by HTTP method (GET, POST, and so on). The actual responses are four-element arrays that constitute the arguments to HTTP::Response->new().

Assuming the given file can be opened, read, and decoded, any request will be satisfied by an HTTP::Response object generated from the data in the file. If the URL and method do not appear in the file, a 404 response is generated.

METHODS

This class supports the following public method:

install_mock

This static method sets $Astro::SpaceTrack::SPACETRACK_DELAY_SECONDS to zero. It also hot-patches the following LWP::UserAgent methods:

new

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

This static method instantiates the object.

It calls subroutine (not method) __load_data() to populate the canned data.

env_proxy

$ua->env_proxy();

This method does nothing.

get

my $resp = $ua->get( $url );

This method returns the response for a GET request on the given URL. The heavy lifting is done by request().

my $resp = $ua->head( $url );

This method returns the response for a HEAD request on the given URL. The heavy lifting is done by request().

post

my $resp = $ua->post( $url );

This method returns the response for a POST request on the given URL. The heavy lifting is done by request().

put

my $resp = $ua->put( $url );

This method returns the response for a PUT request on the given URL. The heavy lifting is done by request().

request

my $resp = $ua->request( $rqst );

This method returns the response for the given HTTP::Request object from the data loaded by __load_data(). If the given URL and method are not found, the response is a 404 error.

ATTRIBUTES

This class has no public attributes.

SUBROUTINES

This class also has a couple old-school subroutines to manage its data. These are unsupported, and documented solely for the convenience of the author.

__load_data

This subroutine loads data from a JSON file and returns a reference to a two-element hash. Key {path} contains the path to the file, and key {data} contains the data in the file, after it has been decoded from JSON.

The path to the file comes from $Mock::LWP::UserAgent::CANNED_DATA_FILE. If that is undef (which it is by default), t/data/Mock-LWP-UserAgent/resp.json is read.

Optional arguments can be specified as name/value pairs. The following arguments are supported:

optional

If this Boolean argument is true, the file need not exist, and if it does not the returned data hash will be empty.

If this Boolean argument is false, or is not specified, a missing file is an error.

All other arguments are unsupported, and behavior if specified may change without notice.

__modify_data

This subroutine takes four arguments. These are:

1) The data returned by __load_data();
2) The URL being recorded;
3) The method by which the URL was fetched
4) An array reference containing the desired arguments to new() to construct the response.

The response is stored in the data, which are then rewritten to the file.

SEE ALSO

LWP::UserAgent

SUPPORT

Support is by the author. Please file bug reports at https://rt.cpan.org/Public/Dist/Display.html?Name=Astro-SpaceTrack, https://github.com/trwyant/perl-Astro-SpaceTrack/issues/, or in electronic mail to the author.

AUTHOR

Thomas R. Wyant, III wyant at cpan dot org

COPYRIGHT AND LICENSE

Copyright (C) 2024-2025 by Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For more details, see the full text of the licenses in the directory LICENSES.

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.